Skip to content

Instantly share code, notes, and snippets.

@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.
@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 / 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 / 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 / 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):
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 / git-up.sh
Last active December 20, 2015 20:09
Pulls in updates from the remote tracking branch of the specified branch (defaults to current branch), rebasing them on top of the current branch and preserving merge commits.
#!/bin/bash
set -e
orig_head=$(git rp HEAD)
branch=${1:-$(git rev-parse --abbrev-ref HEAD)}
remote=$(git config "branch.$branch.remote")
tracking_branch=$(git rev-parse --abbrev-ref --symbolic-full-name $branch@{u})
git fetch $remote
git rebase --preserve-merges $tracking_branch

Please publicly post the following Gist, and name it keybase.md:

Keybase proof

I hereby claim:

  • I am russelldavis on github.
  • I am russell (https://keybase.io/russell) on keybase.
  • I have a public key whose fingerprint is 1FD0 A33F 28EF 63E0 C007 1661 2A16 F6C3 DC27 6AAC
@russelldavis
russelldavis / otask.sh
Created March 27, 2014 23:33
Helpers for creating maniphest tasks (requires https://github.com/bloomberg/phabricator-tools)
#!/bin/bash
exec arcyon task-create "$@" -p normal --format-url --projects Product $PROJ | tee >(pbcopy)
@russelldavis
russelldavis / git-co.sh
Created April 29, 2014 23:07
A better git-checkout
#!/bin/bash
#####
# Fix the ridiculous ambiguity between branches and files in git-checkout.
# The new default behavior is to check out branches only.
# To check out a file, you must precede the filename with "--".
#####
found=false
for arg in "$@"; do
if [[ $arg == "--" ]]; then