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 CappedDict(collections.OrderedDict): | |
default_max_size = 50 | |
def __init__(self, *args, **kwargs): | |
self.max_size = kwargs.pop('max_size', self.default_max_size) | |
super(CappedDict, self).__init__(*args, **kwargs) | |
def __setitem__(self, key, val): | |
if key not in self: | |
max_size = self.max_size-1 # so the dict is sized properly after adding a key | |
self._prune_dict(max_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
class CappedDict(collections.OrderedDict): | |
default_max_size = 50 | |
def __init__(self, *args, **kwargs): | |
self.max_size = kwargs.pop('max_size', self.default_max_size) | |
super(CappedDict, self).__init__(*args, **kwargs) | |
def __setitem__(self, key, val): | |
if key not in self: | |
max_size = self.max_size-1 # so the dict is sized properly after adding a key | |
self._prune_dict(max_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
thread_args = (func_arg, inner_func, (outer_func, outer_arg1, outer_arg2),) | |
functoolz.thread_first(*thread_args) | |
# expands to | |
# outer_func(inner_func(func_arg), outer_arg1, outer_arg2) | |
functoolz.thread_last(*thread_args) | |
# expands to | |
# outer_func(outer_arg1, outer_arg2, inner_func(func_arg)) |
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 group_sequential_numbers(data): | |
# In [4]: list(group_sequential_numbers((2,3,4,5,5,66,6,7,7,7,8,8,99,9))) | |
# Out[4]: [[2, 3, 4, 5], [5], [66], [6, 7], [7], [7, 8], [8], [99], [9]] | |
from operator import itemgetter | |
from itertools import groupby | |
groups = [] | |
for k, g in groupby(enumerate(data), lambda (i,x):i-x): | |
yield map(itemgetter(1), g) |
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_arg_indexes(*args): | |
# In [8]: get_arg_indexes('zero_and_three', 'one', 'two', 'zero_and_three', 'four_and_five','four_and_five') | |
# Out[8]: {'four_and_five': [4, 5], 'one': [1], 'two': [2], 'zero_and_three': [0, 3]} | |
return {k:[i for i, test_key in enumerate(keys) if test_key==k] for k in keys} |
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 multi_delete(list_, *indexes): | |
# http://stackoverflow.com/questions/497426/deleting-multiple-elements-from-a-list | |
for index in sorted(list(indexes), reverse=True): | |
del list_[index] | |
return list_ | |
def indices(lst, element): | |
# http://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list | |
result = [] | |
offset = -1 |
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 strip_bad_unicode(okay_unicode_string): | |
return ''.join(map(lambda char: char.encode('latin1').decode('utf8', 'ignore'), okay_unicode_string)) |
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
# ----------------- | |
# MIXINS: FORCE BP MAPPERS TO WORK FOR US | |
# ----------------- | |
class AllowMissingObjectAttrsMixin(object): | |
def _getattr_inner(self, *args, **kwargs): | |
try: | |
return super(AllowMissingObjectAttrsMixin, self)._getattr_inner(*args, **kwargs) |
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 csv | |
class PGCopyCSVSQLStatement(object): | |
delimiter = ',' | |
template = "COPY {table_name}({column_sequence}) FROM STDIN WITH DELIMITER '{delimiter}' CSV {optional_header}" | |
def __init__(self, | |
table_name=None, | |
column_sequence=None, | |
delimiter=',', | |
has_header=None, |
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
DEFAULT_DELIM = '.' | |
NOPREFIX_PREFIX = '__NOPREFIX__' | |
import itertools | |
import collections | |
def is_non_string_sequence(seq): | |
return isinstance(seq, collections.Sequence) and not isinstance(seq, basestring) | |
def is_dict(d): | |
return isinstance(d, collections.Mapping) or hasattr(d, 'iteritems') and not isinstance(d, basestring) |