Skip to content

Instantly share code, notes, and snippets.

View judy2k's full-sized avatar

Mark Smith judy2k

View GitHub Profile
@judy2k
judy2k / monkey-patch.py
Created March 16, 2017 09:55
Monkey Patching Functions
# So, a bit like mocking/patching, it depends on how the thing you're patching is imported *where it's used*.
# If the code that uses it is in a module called `client`, you'd want to do one of the following:
# If client.py looks like this:
from django.things import victim
def useful_function():
victim(blah)
@judy2k
judy2k / audience.md
Last active April 16, 2017 18:43
Refactoring Talk Outline

This talk is aimed at intermediate Python developers who are interested in developing & maintaining Python libraries. The audience should understand core Python reasonably well, and be aware of things like the property decorator and what "dunder-methods" are.

The audience will learn how to structure their project and their code in ways that will allow them to make changes later without breaking their users' code. Hopefully they'll come away thinking about the structure of their library code in a clearer way.

@judy2k
judy2k / magic_precedence.py
Created September 1, 2016 15:56
Demonstrates that data descriptors have precedence over the instance dict.
class NonDataDescriptor(object):
def __get__(self, *args, **kwargs):
return 'magic'
class DataDescriptor(object):
def __get__(self, *args, **kwargs):
return 'magic'
def __set__(self, *args, **kwargs):
pass
@judy2k
judy2k / test_transfer_dict.py
Last active August 18, 2016 08:14
A handy function for copying properties from one dict to another with optional key mapping. Small amount of code, and well tested.
from transfer_dict import transfer_dict
SOURCE = {
'a': 'thing',
'key': 'value',
}
def test_basic():
dest = transfer_dict(SOURCE)
assert dest == SOURCE
@judy2k
judy2k / argparse_call.py
Created April 10, 2016 10:45
argparse -> function call
# Please note this function is a sketch and there will be (plenty of) edge cases
# where it won't do the right thing.
def whitelist_dict(d, whitelist):
"""
Neat function to extract arguments from a dict, d, that are acceptable for
calling a target function.
`whitelist` can be either a set of acceptable parameter names, or a callable,
which will have its parameters inspected.
@judy2k
judy2k / next_weekday.py
Created January 9, 2016 11:54
Find the next weekday after the given date.
from datetime import date, timedelta
def next_weekday(d):
wd = d.weekday()
return d + timedelta(days=1 if wd < 4 else 7 - wd)
@judy2k
judy2k / do-stuff-with-found-files.bash
Last active September 28, 2015 11:08
Bash tricks and tips
# The following allows working with files with spaces in the names that come back from find:
find . -name 'names-with-spaces-maybe' -print0 | while read -d $'\0' file; do
do-stuff-with "$file"
done
@judy2k
judy2k / gradually_worse_pi.py
Created August 20, 2015 08:53
A module to hide in a codebase. Each time math.pi is requested its value will change by a tiny amount, gradually drifting away from the correct value.
import sys
import math as _math
__doc__ = _math.__doc__
class NewMath(object):
_pi = _math.pi
@property
def pi(self):
@judy2k
judy2k / list_branches.sh
Created March 13, 2015 10:11
List git branches
for f in `ls`; do
if [ -d $f/.git ]; then
fb=$(git --git-dir=$f/.git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* //")
echo $f: $fb
fi
done
def call_me(names=[]):
names.append('Stupid')
print ' '.join(names)
for _ in range(10):
call_me()