Skip to content

Instantly share code, notes, and snippets.

@thepaul
thepaul / runnable-pyc
Created January 2, 2013 20:23
make byte-compiled python code into an executable
#!/usr/bin/env python
#
# runnable-pyc
usage_info = \
"""runnable-pyc - make compiled Python code into an executable file
Usage: runnable-pyc <pycfile> [<outputfile>]
If no outputfile is specified, and pycfile ends in '.pyc', then the default
@thepaul
thepaul / levenshtein.py
Created January 6, 2013 01:00
Levenshtein distance between two sequences
def _LevenshteinDistance(str1, i, len1, str2, j, len2, cache):
key = (i, len1, j, len2)
if key in cache:
return cache[key]
if len1 == 0:
return len2
if len2 == 0:
return len1
@thepaul
thepaul / forwarding.py
Created January 9, 2013 00:16
Simple cross-protocol forwarder (Twisted). Run under twistd -y with FWD_LISTEN and FWD_CONNECT set according to the rules for serverFromString (http://twistedmatrix.com/documents/current/api/twisted.internet.endpoints.html#serverFromString) and clientFromString (http://twistedmatrix.com/documents/current/api/twisted.internet.endpoints.html#clien…
import os
from twisted.internet import protocol, reactor, endpoints, error
from twisted.application import service
from twisted.python import log
class Forwarder(protocol.Protocol):
def __init__(self):
self.peer = None
self.peer_queue = []
@thepaul
thepaul / unused_port.py
Created December 28, 2013 02:55
choose a port number unused for TCP on all local inet interfaces (or on a particular interface). use sock_type=socket.SOCK_DGRAM for an unused UDP port instead of TCP.
import socket
import contextlib
def choose_unused_port(interface='0.0.0.0', sock_type=socket.SOCK_STREAM):
with contextlib.closing(socket.socket(socket.AF_INET, sock_type)) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((interface, 0))
return s.getsockname()[1]
@thepaul
thepaul / gist:8588668
Last active January 9, 2016 18:30
send python stdlib logging output through the Twisted logging system
import logging
from twisted.python import log, failure
class TwistedLogHandler(logging.Handler):
"""
Sends Python stdlib logging output through the Twisted logging system.
"""
def __init__(self, twistedlogpublisher=None):
@thepaul
thepaul / SanerConfigParser.py
Created May 13, 2014 22:42
# the ConfigParser.*ConfigParser family is just comically terrible
import ConfigParser
class SanerConfigParser(ConfigParser.SafeConfigParser):
def __init__(self, defaults=None, dict_type=dict, allow_no_value=False):
ConfigParser.SafeConfigParser.__init__(self, dict_type=dict_type,
allow_no_value=allow_no_value)
self.mydefaults = defaults or {}
no_default = object()
@thepaul
thepaul / ssh_tunnel_control.py
Created May 14, 2014 20:32
ssh tunnel up/down
import contextlib
import os
import subprocess
import tempfile
class SshTunnelDefinition:
def __init__(self, host, user=None, executable='ssh', localforwards=(),
remoteforwards=(), port=None, options=None):
self.executable = executable
@thepaul
thepaul / gist:aec592cb62294e587ef8
Created June 17, 2015 22:33
things in the Python standard library which use super()
argparse (all classes)
abc.ABCMeta
collections.Counter
fractions.Fraction
plistlib.Dict and plistlib.Plist
random.Random
unittest.TextTestResult and unittest.FunctionTestCase
weakref.KeyedRef
zipfile.ZipExtFile
ctypes.py_object
@thepaul
thepaul / gist:0e611da8b4fadec94568
Created July 9, 2015 21:29
wait for all subprocesses in a list to exit
import signal
def wait_for_subprocs(procs, cb=lambda proc: 0):
# do-nothing handler for SIGCHLD, just so it's something other than SIG_DFL.
# otherwise, Python won't interrupt syscalls.
oldhandler = signal.signal(signal.SIGCHLD, lambda *_: None)
try:
while procs:
signal.pause()
aliveprocs = []
diff --git a/tools/dockerz/Dockerfile b/tools/dockerz/Dockerfile
new file mode 100644
index 0000000..0eafb35
--- /dev/null
+++ b/tools/dockerz/Dockerfile
@@ -0,0 +1,17 @@
+FROM ubuntu:trusty
+MAINTAINER <[email protected]>
+
+RUN apt-get -y update \