Skip to content

Instantly share code, notes, and snippets.

@alexclare
Created March 16, 2015 15:38
Show Gist options
  • Save alexclare/c7eb9f0053704fb52f3a to your computer and use it in GitHub Desktop.
Save alexclare/c7eb9f0053704fb52f3a to your computer and use it in GitHub Desktop.
common.py
'common, fun python functions that I seem to rewrite everywhere'
import itertools, sys
# stolen from itertools recipes
def chunk(iterable, batch_size, fillvalue=None):
'breaks up iterator into chunks of batch_size'
args = [iter(iterable)] * batch_size
return itertools.izip_longest(fillvalue=fillvalue, *args)
def safe_chunk(iterable, batch_size):
'like chunk(...) but filters out leftover null values'
return (itertools.ifilter(lambda x: x, members)
for members in chunk(iterable, batch_size, fillvalue=None))
def info(msg):
print >> sys.stderr, '[INFO] {}'.format(msg)
def warn(msg):
print >> sys.stderr, '[WARN] {}'.format(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment