Skip to content

Instantly share code, notes, and snippets.

@buchanae
buchanae / gist:4237214
Created December 7, 2012 22:56
Writing to a file (python)
# This is my preferred way to use print
# and makes my code compatible with future versions
# of python. More info here:
# http://docs.python.org/2/library/functions.html#print
from __future__ import print_function
# http://docs.python.org/2/library/functions.html#open
# http://effbot.org/zone/python-with-statement.htm
with open('output.txt', 'w') as file_handle:
# notice I didn't add a newline at the end, print() does that for you
@buchanae
buchanae / gist:4237556
Last active October 13, 2015 18:27
Representing a tree and doing a pre-order traversal
from __future__ import print_function
# The tree:
# a
# b c
# d e f g
# h i j k l m n o
graph = {
'a': ['b', 'c'],
from collections import defaultdict
def splitter(lines, keyfunc, valuefunc):
d = defaultdict(list)
for line in lines:
cols = line.split()
key = keyfunc(cols)
value = valuefunc(cols)
d[key].append(value)
# maps names to OTU numbers
name_to_OTU = {
'name_1': 23,
'RIO12.3_44008': 93,
}
# contrived data to represent what you emailed
# this represents one sample's counts,
# e.g. the "('MID - OLD12.5',..." line
per_sample_counts = Counter({
@buchanae
buchanae / gist:5448778
Created April 24, 2013 01:00
jsonpickle fails to decode namedtuple
from collections import namedtuple
import jsonpickle
Foo = namedtuple('Foo', 'bar')
foo = Foo('bar')
encoded = jsonpickle.encode(foo)
decoded = jsonpickle.decode(encoded)
assert isinstance(decoded, Foo), 'decoded has type {}'.format(type(decoded))
from __future__ import print_function
otu_list = ['otu1', 'otu2', 'otu15']
d = {'nice name': {'otu1': 24, 'otu15': 57}}
for name, otu_counts in d.items():
cols = []
for otu_key in otu_list:
count = otu_counts.get(otu_key, 0)
(jeans)jeans$ pip install pysam
Downloading/unpacking pysam
Downloading pysam-0.7.4.tar.gz (1.4MB): 1.4MB downloaded
Running setup.py egg_info for package pysam
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.34.tar.gz
Extracting in /var/folders/vU/vURgXIhFG6O8EVY7ATHbn++++TQ/-Tmp-/tmpE5Lp4T
Now working in /var/folders/vU/vURgXIhFG6O8EVY7ATHbn++++TQ/-Tmp-/tmpE5Lp4T/distribute-0.6.34
Building a Distribute egg in /Users/abuchanan/.virtualenvs/jeans/build/pysam
/Users/abuchanan/.virtualenvs/jeans/build/pysam/distribute-0.6.34-py2.7.egg
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'use_2to3'
@buchanae
buchanae / OptionalFormatter.py
Created June 18, 2013 04:32
Python string formatter that allows optional keyword placeholders
import string
class FormatKeyError(Exception):
def __init__(self, key):
self.key = key
class OptionalFormatter(string.Formatter):
@buchanae
buchanae / gist:6105943
Created July 29, 2013 17:22
Get Python class by name
def get_class(name):
# Reflection: try to get the exception class
# Try to split the name on dots,
# in case it contains a module path
parts = name.split('.')
if len(parts) == 1:
# If it didn't have a module path,
@buchanae
buchanae / gist:7653736
Created November 26, 2013 05:07
Question for django rest framework group regarding fully qualified reverse URL when using APIClient
class Album(Model):
...
class Track(Model):
album = ForeignKey(Album)
class AlbumSerializer(HyperlinkedModelSerializer):
...
class TrackTest(APITestCase):