Skip to content

Instantly share code, notes, and snippets.

def chunked_list(lst, size):
"""
Iterates over chunks of a list
"""
for i in xrange(0, len(lst), size):
yield lst[i:i+size]
@russelldavis
russelldavis / expand_with_args.py
Created May 4, 2013 17:07
For each set of args, makes a copy of the function with the args bound, and with the repr of the args appended to the name. Assigns that to the calling frame's locals. Adapted from http://stackoverflow.com/a/4455312/278488. Meant for use with unit tests, to create multiple tests from a parameterized method.
def expand_with_args(*parameters, **kwargs):
"""
For each set of args, makes a copy of the function with the args bound, and
with the repr of the args appended to the name. Assigns that to the calling
frame's locals. Adapted from http://stackoverflow.com/a/4455312/278488.
Meant for use with unit tests, to create multiple tests from a parameterized
method.
"""
def tuplify(x):
if not isinstance(x, tuple):
@russelldavis
russelldavis / check_teed_output.py
Last active December 9, 2015 23:18
Like check_output, but also tees the output to a file object specified by the stdout kwarg. Passing in stdout=sys.stdout allows you to run the process with normal output (like check_call()) but also get the output as the return value (like check_output()).
from subprocess import Popen, PIPE, CalledProcessError
def check_teed_output(*popenargs, **kwargs):
"""
Like check_output, but also tees the output to a file object specified
by the stdout kwarg. Passing in stdout=sys.stdout allows you to run the
process with normal output (like check_call()) but also get the output
as the return value (like check_output()).
"""
@russelldavis
russelldavis / timezone.py
Created November 2, 2012 00:09
Convert between UTC and local time without third party tz libraries
def utc_to_local(dt):
"""
Converts a naive datetime from UTC to local time, returning a new naive
datetime.
"""
return datetime.fromtimestamp(calendar.timegm(dt.utctimetuple()))
def local_to_utc(dt):
"""
Converts a naive datetime from local to UTC time, returning a new naive
@russelldavis
russelldavis / time_offset.py
Created October 31, 2012 20:32
Context manager for patching python date and time functions for unit tests
# Required because datetime.date is a builtin type that can't be modified
class MockDate(date):
"A wrapper for date that can be mocked for testing."
def __new__(cls, *args, **kwargs):
return date.__new__(date, *args, **kwargs)
# Required because datetime.datetime is a builtin type that can't be modified
class MockDateTime(datetime):
"A wrapper for datetime that can be mocked for testing."
@russelldavis
russelldavis / copy_mtime
Created October 30, 2012 18:07
Workaround for truncation when writing file timestamps in Python
def copy_mtime(src, dest):
# Don't use os.utime() or shutil.copystat() -- these both truncate the
# time due to python's float not having as much resolution as the
# filesystem -- see http://ciaranm.wordpress.com/2009/11/15/this-week-in-python-stupidity-os-stat-os-utime-and-sub-second-timestamps/
#
# If the truncation during reading (from os.stat()) was the same as the
# truncation during writing, the problem would be transparent here (but
# would still affect other non-truncating code), however python takes the
# problem to a new level and truncates an extra decimal place during write.
# Either way, it's best to avoid the write truncation altogether.