I hereby claim:
- I am dplepage on github.
- I am dplepage (https://keybase.io/dplepage) on keybase.
- I have a public key ASD2DPrx9syvhZ4imHgZCbx4lT5HDzHfpU1mXAc2-W9_yQo
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
| Hammerin' Hank | |
| Panque | |
| Dusty | |
| Bedrock | |
| Retriever | |
| Eggs | |
| Buzz | |
| Big Boy | |
| Beeg Boy | |
| Rico |
| def overlay(d1, d2): | |
| for key, value in d2.items(): | |
| if isinstance(value, dict) and isinstance(d1.get(key), dict): | |
| overlay(d1[key], value) | |
| else: | |
| d1[key] = value |
| byte_aware_dfy = GraphDispatcher([dictify]) | |
| byte_aware_udfy = GraphDispatcher([undictify]) | |
| @byte_aware_dfy.when(Bytes) | |
| def df_bytes(dispgraph, value, **kwargs): | |
| return value.decode('utf-8') | |
| @byte_aware_udfy.when(Bytes) | |
| def udf_bytes(dispgraph, value, **kwargs): | |
| return value.encode('utf-8') |
| class Resolver(object): | |
| def __init__(self, fn = lambda x:x): | |
| self._fn = fn | |
| def __getattr__(self, attr): | |
| return Resolver(lambda x: getattr(self._fn(x), attr)) | |
| def __getitem__(self, key): | |
| return Resolver(lambda x: self._fn(x)[key]) |
| import logbook | |
| class LogRequestIDMiddleware(object): | |
| def __init__(self, application): | |
| self.application = application | |
| def make_processor(self, request_id): | |
| @logbook.Processor | |
| def processor(log_record): | |
| log_record.extra['request_id'] = request_id |
| import inspect, ast | |
| from itertools import islice, chain, cycle | |
| def iter_n(iterator, n, default=None): | |
| return islice(chain(iterator, cycle([default])), n) | |
| def unpack(sequence, default=None): | |
| stack = inspect.stack() | |
| try: | |
| frame = stack[1][0] |
| import bisect | |
| from collections import namedtuple | |
| import datetime as dt | |
| Event = namedtuple("Event", ('timestamp', 'value')) | |
| class Timeline(object): | |
| def __init__(self, initial=None, events=(), clock=None): | |
| if clock is None: | |
| clock = dt.datetime |
The goal is to make a namedtuple subclass with helper functions (which obviously are read-only, because it's a namedtuple). Simply creating a namedtuple and subclassing it doesn't work, as the subclass gets __dict__ and other mutable things that ruin the __slots__ awesomeness of namedtuple. So instead, this uses ActuallyCalls (from actually.py, https://gist.github.com/dplepage/5095902) to create a new namedtuple class and add your functions to it.
| from collections import namedtuple | |
| from actually import ActuallyCalls | |
| class CaseClasses(object): | |
| '''Class that converts all lists/tuples in the class into namedtuple classes.''' | |
| class __metaclass__(type): | |
| def __new__(cls, name, supers, kwargs): | |
| for name, value in kwargs.items(): | |
| if isinstance(value, (list, tuple)): | |
| kwargs[name] = namedtuple(name, value) |