Created
March 16, 2015 15:38
-
-
Save alexclare/c7eb9f0053704fb52f3a to your computer and use it in GitHub Desktop.
common.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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