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
| # noinspection PyPep8Naming | |
| class classproperty(object): | |
| def __init__(self, function, name=None): | |
| self.__name__ = name or function.__name__ | |
| self._class_property = function | |
| self._class_property_is_lazy = False | |
| self._cached_class_property_result = None | |
| self._instance_property = None | |
| self._instance_property_is_lazy = False | |
| self._cached_instance_property_result = None |
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 identity(x): | |
| return x | |
| class unique(object): | |
| def __init__(self, xs, predicate=identity): | |
| """ :type xs: Iterable """ | |
| self._xs = xs | |
| self._predicate = predicate |
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
| from functools import partial | |
| class partialmethod(partial): | |
| def __get__(self, instance, owner): | |
| if instance is None: | |
| return self | |
| return partial(self.func, instance, | |
| *(self.args or ()), **(self.keywords or {})) |
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
| from django.db import models | |
| # Given the following classes: | |
| class Foo(models.Model): | |
| name = models.TextField() | |
| class Bar(models.Model): | |
| name = models.TextField() | |
| foos = models.ManyToManyField(Foo) |
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
| <html> | |
| <head> | |
| <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| /** | |
| * A better version of setInterval. Usage: | |
| * until([ | |
| * { action: function() { $('#foo').length > 1; }, | |
| * cleanup: function() { $('#foo').remove(); }, | |
| * interval: 1000 } |
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
| from collections import Iterable | |
| from itertools import chain | |
| def left_join(xss, yss, on=None): | |
| """ | |
| :type xss: Iterable[Iterable] | |
| :type yss: Iterable[Iterable] | |
| :type on: Iterable[int] | (Iterable, Iterable) -> bool | |
| :rtype: generator[tuple[list]] | |
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
| #/bin/bash | |
| set -e | |
| if [ -z "$(which brew)" ]; then | |
| echo "This script requires Homebrew." | |
| exit | |
| fi | |
| if [ -z "$VIRTUAL_ENV" ]; then |
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
| from functools import wraps | |
| from django.core.cache import cache | |
| def cached(key, timeout=None): | |
| """Decorator to cache result of function call for timeout seconds if the | |
| result is not None. | |
| """ | |
| def wrapper(f): | |
| @wraps(f) |
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 excel_tuple(address): | |
| """ | |
| Takes an Excel address and converts it to a tuple. Used as a helper for | |
| looking up (row, column) to pass into sheet.cell(row, col). | |
| >>> assert excel_tuple('A1') == (0, 0) | |
| >>> assert excel_tuple('Z10') == (9, 25) | |
| >>> assert excel_tuple('AAA999') == (998, 702) |
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 M(type): | |
| def __new__(mcs, name, bases, attrs): | |
| if bases: | |
| b = attrs.pop('B', None) | |
| if b: | |
| bs = tuple(s for s in (c.__dict__.get('B') for c in bases) if s) | |
| attrs['B'] = type('B', bs, b.__dict__) | |
| return super(M, mcs).__new__(mcs, name, bases, attrs) | |