This file contains hidden or 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
| # coding: utf-8 | |
| # In[258]: | |
| import datetime | |
| import operator | |
| import arrow | |
This file contains hidden or 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 distribute_time(dt, count=-1, step=1, unit='hours', round_time=True): | |
| """ | |
| Examples: | |
| In [01]: | |
| list(distribute_time(dt, -3)) | |
| Out[01]: | |
| [datetime.datetime(2016, 1, 26, 22, 0), | |
| datetime.datetime(2016, 1, 26, 21, 0), |
This file contains hidden or 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 re | |
| def make_glob(*parts): | |
| """ | |
| Reverse glob: make a 'glob_pattern_string*' from a list of strings ['glob_pattern_string_1', 'not_a_match', 'glob_pattern_string_1', 'glob_pattern_string_3'] | |
| """ | |
| def match_parts(partial_string): | |
| rx = re.compile(partial_string+'.*') | |
| for part in parts: | |
| try: |
This file contains hidden or 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
| """ | |
| Pop off extensions by name or by number to pop off | |
| pull_extensions('filenamewith.ext.csv.gz', 2) # 'filenamewith.ext' | |
| pull_extensions('filenamewith.ext.csv.gz', 'csv.gz') # 'filenamewith.ext' | |
| pull_extensions('filenamewith.ext.csv.gz', 'csv') # 'filenamewith.ext.gz' | |
| """ | |
| import itertools | |
| def remove_from_end(list_, val): |
This file contains hidden or 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 split_many(astring, *split_on_chars, **kwargs): | |
| """ | |
| Split a string on many values. | |
| :param split_on_chars: string to split | |
| :type split_on_chars: basestring | |
| :param split_on_chars: chars to split on | |
| :type split_on_chars: tuple | |
| :param kwargs: keep_empty: keeps empty strings in result, which happens with two adjacent separators in a string |
This file contains hidden or 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
| # BUILD A MONGO QUERY WITH DJANGO QUERY STYLE | |
| # THIS WORKS FOR BASIC QUERIES, BUT IS MISSING SEVERAL OPERATORS | |
| # | |
| # In [1083]: | |
| # birds = ME(color__in=('yellow', 'orange',), name='Big Bird') | |
| # feathers = ME(feathers__count__gt=1000) | |
| # In [1084]: | |
| # print birds.expression | |
| # print feathers.expression |
This file contains hidden or 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 operator | |
| def get_class_attr_names(c): | |
| if not getattr(mycal, 'mro', None): | |
| return [] | |
| return set(reduce(operator.or_, map(set, map(dir, c.mro()[1:])))) ^ set(dir(c)) |
This file contains hidden or 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 urlparse | |
| import os | |
| class Path(unicode): | |
| def __init__(self, path): | |
| self.parsed_url = urlparse.urlparse(path) | |
| self.split = os.path.split(self.parsed_url.path) | |
| self.splitext = os.path.splitext(self.parsed_url.path) |
This file contains hidden or 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 BoilClass(object): | |
| ''' | |
| Print out class definition code. Does not create a class, just writes the code for you to edit. | |
| ''' | |
| INDENT = ' '*4 | |
| def __init__(self, classname, *attrs, **kwargs): | |
| bases=kwargs.get('bases') or ('object',) | |
| if kwargs.get('init_newlines'): | |
| kwargs['init_sep'] = ',\n' |
This file contains hidden or 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) |