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
# 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 |
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 __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'], |
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 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) |
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
# 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({ |
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 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)) |
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 __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) |
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
(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' |
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
import string | |
class FormatKeyError(Exception): | |
def __init__(self, key): | |
self.key = key | |
class OptionalFormatter(string.Formatter): |
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 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, |
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
class Album(Model): | |
... | |
class Track(Model): | |
album = ForeignKey(Album) | |
class AlbumSerializer(HyperlinkedModelSerializer): | |
... | |
class TrackTest(APITestCase): |
OlderNewer