Skip to content

Instantly share code, notes, and snippets.

@mloberg mloberg/batch.py
Created Mar 2, 2018

Embed
What would you like to do?
Python Tools
from itertools import islice, chain
def batch(iterable, size):
iterator = iter(iterable)
for first in iterator:
yield chain([first], islice(iterator, size - 1))
import os
import pickle
def cache(function):
def wrapper(*args, **kwargs):
key = '.cache/%s-%s' % (function.__name__, ','.join(args))
if os.path.exists(key):
with open(key, 'rb') as f:
return pickle.load(f)
result = function(*args, **kwargs)
path, filename = os.path.split(key)
os.makedirs(path, exist_ok=True)
with open(key, 'wb') as f:
pickle.dump(result, f)
return result
return wrapper
@cache
def expensive_call():
"""Some expensive function call"""
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.