Skip to content

Instantly share code, notes, and snippets.

@thepaul
thepaul / ParallelBatcher.py
Created January 13, 2011 19:08
Do Twisted Deferred-producing jobs, X at a time
# ParallelBatcher.py
#
# Job pipeline for Twisted Matrix
# the paul 2011
#
# Sort of goes between defer.DeferredList and plain Deferred chaining.
# When you have lots of jobs to do which take time (most likely because
# they have to wait on some network action) but you don't want to do
# them all at the same time (maybe the remote network action is CPU- or
# bandwidth-intensive and you want to avoid overloading the remote
@thepaul
thepaul / gist:660680
Created November 3, 2010 01:52
index records by first field and output according to that index. original need called for commas, but set VSEP to the empty string if you just want separation of multiple values with OFS.
awk '{x[$1]=x[$1]?(x[$1] VSEP OFS $2):$2}END{for(e in x){print e,x[e]}}' VSEP=,
@thepaul
thepaul / socket_info.py
Created October 9, 2010 23:04
output info about socket objects
import socket
from subprocess import check_output
from sys import stdout
from os import getpid
def info_about_socket(s, out=stdout):
"""
Write some information about the status and state of a socket object
to the file-like object 'out' (stdout, by default).
@thepaul
thepaul / have_pidfile.py
Created October 9, 2010 22:12
pidfile context manager
import os
import contextmanager
@contextlib.contextmanager
def have_pidfile(fname):
f = open(fname, 'w')
f.write('%d\n' % os.getpid())
f.flush()
s = os.fstat(f.fileno())
dev, ino = s.st_dev, s.st_ino
@thepaul
thepaul / export2csv.py
Created June 22, 2009 22:41
export random database stuff to csv
# export2csv.py
#
# export random database stuff to csv
from __future__ import with_statement
import csv
def export2csv(cursor, outf):
"""
cursor should have an executed query already