Skip to content

Instantly share code, notes, and snippets.

Keybase proof

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:

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])
@dplepage
dplepage / log_request_id.py
Last active December 31, 2015 16:17
Request-id middleware sketch
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
@dplepage
dplepage / unpack.py
Created December 4, 2013 20:29
Fun with inspect and ast. I do not recommend using this code in production.
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
@dplepage
dplepage / README.rst
Last active December 21, 2015 09:48
Helper for creating more robust immutable objects.

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.

Obvious caveats:
  • If you subclass one of these classes, you'll have the same problem as with subclassing namedtuple directly
  • Don't specify methods like __new__ that will conflict with namedtuple's members
@dplepage
dplepage / case.py
Last active December 17, 2015 05:58
Case classes for Python.Depends on actually.py (< https://gist.github.com/dplepage/5095902 >)
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)