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 re | |
def stripe_form(data): | |
"""perform a stripe nested-data form-encoding""" | |
form = [] | |
def _form2(pre, val): | |
if type(val) not in (list, dict): | |
form.append((pre, val)) | |
return |
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 sys, gc, collections | |
import psutil | |
def size_modules(): | |
module_size = collections.defaultdict(int) | |
for obj in gc.get_objects(): | |
if not hasattr(obj, "__module__"): | |
continue | |
if not isinstance(obj.__module__, str): |
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 _glom(spec, target, scope): | |
scope[CHILD_ERRORS] = [] | |
try: | |
... | |
except Exception as e: | |
scope.parent[CHILD_ERRORS].append((scope, e)) | |
raise | |
else: |
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 relativity import M2M | |
def generations(m2m): | |
""" | |
m2m is {dependent: dependes-on} | |
returns [{item, item, ...}, {item, item, ...}, ...] in topologically sorted order | |
""" | |
m2m = m2m.copy() | |
generations = [] | |
# first generation is things that have no dependencies |
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 ChainMap | |
def glom(target, spec): | |
return Glom(spec, target).glom(ChainMap()) | |
class Glom(object): | |
""" | |
rather than state living inside glom() methods, the recursive |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
m2m = M2M() | |
ch = M2MChain() | |
gr = M2MGraph() | |
# TRANSFORMING BETWEEN TYPES | |
chain(*ch.m2ms) # kind of a copy | |
chain(ch) # also results in copy |
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 collections | |
import functools | |
def log_methods(cls): | |
def __init__(self, *a, **kw): | |
cls.__init__(self, *a, **kw) | |
self._log = collections.deque(maxlen=100) | |
def logit(method): |
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
""" | |
experimenting with increasingly general notions of indexing | |
""" | |
from relativity import M2M | |
class IndexedTriple(object): | |
""" | |
keeps track of "left" to "right" pairs | |
""" |
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 timeit | |
>>> LIST = [None] * 10 | |
>>> countrange = lambda: [1 for i in range(len(LIST))] | |
>>> countdirect = lambda: [1 for e in LIST] | |
>>> timeit.timeit(countrange, number=1000) * 1000 | |
0.8033999999952357 | |
>>> timeit.timeit(countdirect, number=1000) * 1000 | |
0.5680000000012342 | |
>>> LIST = [None] * 100 | |
>>> timeit.timeit(countrange, number=1000) * 1000 |