Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
# from https://raw.githubusercontent.com/Anorov/PySocks/cd6844dfc4dbe4550e30d9271d8d29cca23e3702/sockshandler.py
"""
SocksiPy + urllib2 handler
version: 0.2
author: e<e@tr0ll.in>
This module provides a Handler which you can use with urllib2 to allow it to tunnel your connection through a socks.sockssocket socket, with out monkey patching the original socket...
"""
#!/usr/bin/env python
"""Measure relative performance of "count overlapping substrings" functions.
http://hashcode.ru/questions/404985/python-%D1%85%D0%B8%D1%82%D1%80%D0%BE%D0%B5-%D0%B2%D1%85%D0%BE%D0%B6%D0%B4%D0%B5%D0%BD%D0%B8%D0%B5-%D0%BF%D0%BE%D0%B4%D1%81%D1%82%D1%80%D0%BE%D0%BA%D0%B8-%D0%B2-%D1%81%D1%82%D1%80%D0%BE%D0%BA%D1%83
"""
import re
def count_overlapping_substrings(haystack, needle):
count = 0
i = -1
@zed
zed / run-script-on-alert-pipe.py
Last active March 30, 2023 18:28
React to snort alert
#!/usr/bin/env python
"""Run `snort -A console` command using a pipe.
Warning! Alerts are delayed until snort's stdout buffer is flushed.
"""
from __future__ import print_function
from subprocess import Popen, PIPE, STDOUT
snort_process = Popen(['snort', '-A', 'console', '-c', 'snort.conf'],
stdout=PIPE, stderr=STDOUT, bufsize=1,
#!/usr/bin/env python
"""Test POSIX definition of "seconds since Epoch".
Assert that posix_time(), calendar.timegm(), and timegm() (mktime() in
UTC) behave the same. Check `datetime.timestamp()` and the explicit
formula (d - epoch) too.
"""
import calendar
import itertools
@zed
zed / 0_reuse_code.js
Last active August 29, 2015 14:21
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
#!/usr/bin/env python
"""
http://ru.stackoverflow.com/q/427088/23044
"""
import grako # $ pip install grako
grammar_ebnf = """
record = type name teacher auditory $ ;
type = "pr." | "lab." | "c." ;
name = { word }+ ;
@zed
zed / binary16.py
Last active July 30, 2022 00:19
IEEE 754 floating-point binary16
#!/usr/bin/env python
# -*- coding: utf-8 -*-
r"""Support for IEEE 754 half-precision binary floating-point format: binary16.
>>> [binary16(f) for f in [0.006534, -.1232]] == [b'\xb0\x1e', b'\xe2\xaf']
True
"""
from __future__ import division
import struct
from math import copysign, frexp, isinf, isnan, trunc
#!/usr/bin/env python
"""Parallel version of
http://stackoverflow.com/a/32741930/
"""
import collections
import os
import select
import signal
import subprocess
import threading
@zed
zed / .gitignore
Last active September 4, 2019 16:38
next leap second IERS-OP web service client
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
@zed
zed / leapseconds.py
Last active September 23, 2025 11:05
leap seconds
#!/usr/bin/env python
"""Get TAI-UTC difference in seconds for a given time using tzdata.
i.e., find the number of seconds that must be added to UTC to compute
TAI for any timestamp at or after the given time[1].
>>> from datetime import datetime
>>> import leapseconds
>>> leapseconds.dTAI_UTC_from_utc(datetime(2005, 1, 1))
datetime.timedelta(seconds=32)