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
def chunked_list(lst, size): | |
""" | |
Iterates over chunks of a list | |
""" | |
for i in xrange(0, len(lst), size): | |
yield lst[i:i+size] |
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
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): |
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
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()). | |
""" |
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
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 |
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
# 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." |
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
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. |
NewerOlder