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
| ''' | |
| Dependency-Injection lite. | |
| The input is key-values of constructors, the output | |
| is key-values of constructed values. | |
| Keys in the arguments to this functions are distributed | |
| to the arguments as values are constructed. | |
| IDIOMS: | |
| To pass a constant value: keys which are not callable | |
| will be left as-is. | |
| To pass a callable as a value: simply wrap it in a lambda. |
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
| ''' | |
| Given two modules that want to reference each other in a way that would | |
| otherwise create an import loop, one mechanism for handling this is | |
| a deferred import. | |
| def foo(): | |
| import bar | |
| bar.bar() |
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 itertools, math | |
| def pairwise_walk(items): | |
| ''' | |
| given a list of items, return a list of instructions of which | |
| ordered pairs of relationships to add or remove, in order to | |
| go over every possible set of pairwise relationships existing | |
| or not existing while only adding or removing one at a time |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
| ''' | |
| implementation of some linear algebra in SQL | |
| ''' | |
| import sqlite3 | |
| import time | |
| import weakref | |
| _MISSING = object() |
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 ComplementSet(object): | |
| ''' | |
| inverse of a normal set -- everything except the set is in there | |
| ''' | |
| def __init__(self, missing): | |
| assert type(missing) in (set, frozenset) | |
| self.missing = missing | |
| def __repr__(self): return 'ComplementSet({' + ', '.join([repr(e) for e in self.missing]) + '})' |
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 glom import glom, T, S | |
| class ScopeSpecs(object): | |
| ''' | |
| given a dict of specs, when evaluating | |
| evaluate each of those specs against the same target, | |
| then evalaute the child spec with the results of | |
| those specs in the corresponding keys of the scope | |
| so the child can see them |
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 copy | |
| def closest_round_division(numbers, fractions): | |
| ''' | |
| Given a list of numbers and a list of fractions, this function performs a more | |
| sophisticated version of: | |
| [[round(n * f) for f in fractions] for n in numbers] | |
| Rather than rounding each number individually, the algorithm rounds in order that | |
| the sum of each row and the sum of each column both remain within 1 of their exact |
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 glom | |
| class If(object): | |
| def __init__(self, cond, if_branch, else_branch=None): | |
| self.cond, self.if_branch, self.else_branch = cond, if_branch, else_branch | |
| def glomit(self, target, scope): | |
| g = lambda spec: scope[glom.glom](target, spec, scope) | |
| if g(self.cond): |
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 binascii | |
| class Bits(object): | |
| ''' | |
| An immutable bit-string or bit-array object. | |
| Provides list-like access to bits as bools, | |
| as well as bitwise masking and shifting operators. |