Skip to content

Instantly share code, notes, and snippets.

@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)
@dplepage
dplepage / README
Created November 27, 2012 19:11
Put an editor URL in Werkzeug's debugger pages
This quick hack wraps the filenames in the Werkzeug debugger with a link that opens them in my editor.
In my particular case I have Sublime Text and SublHandler on OS X (urls below), so my links point to subl://open/?url=<file url>, but you can easily change this for any other URL handler.
Sublime Text: http://www.sublimetext.com/
SublHandler (for OS X): https://github.com/asuth/subl-handler
@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!"