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:
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): |
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 |
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) |
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) |