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:

@dplepage
dplepage / gist:2024199
Created March 12, 2012 19:33
Routing decorator for flask subclassing
import flask
class RoutingData(object):
def __init__(self, args, kwargs):
super(RoutingData, self).__init__()
self.args = args
self.kwargs = kwargs
def route(*args, **kwargs):
def wrap(fn):
@dplepage
dplepage / gist:2024129
Created March 12, 2012 19:26
Subclassing Flask
import flask
class HelloApp(flask.Flask):
def __init__(self, import_name):
super(HelloApp, self).__init__(import_name)
self.route("/hello")(self.hello)
def hello(self):
return "Hello, world!"
Hammerin' Hank
Panque
Dusty
Bedrock
Retriever
Eggs
Buzz
Big Boy
Beeg Boy
Rico
@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)
@dplepage
dplepage / actually.py
Last active December 14, 2015 13:48
Python metahack that allows you to use class-creation syntax to actually call arbitrary functions. This is useful for e.g. creating anonymous functions as arguments to other functions.
def ActuallyCalls(fn, namearg=None):
class __ActuallyCalls(object):
class __metaclass__(type):
def __new__(cls, name, supers, kwargs):
if name == '__ActuallyCalls':
return type.__new__(cls, name, supers, kwargs)
kwargs.pop('__module__', None)
if namearg:
kwargs[namearg] = name
return fn(**kwargs)