Skip to content

Instantly share code, notes, and snippets.

@bobpoekert
Created May 13, 2011 06:24
Show Gist options
  • Save bobpoekert/970080 to your computer and use it in GitHub Desktop.
Save bobpoekert/970080 to your computer and use it in GitHub Desktop.
The @after decorator
from twisted.internet.defer import Deferred
def noop(*args):
return args
class proxy(object):
def __init__(self, fn):
self.defer = Deferred()
self.fn = fn
def __call__(self, *args, **kwargs):
self.defer.callback(fn(*args, **kwargs))
class after(object):
def __init__(self, step1, key=noop):
self.step1 = step1
self.key = key
def __call__(self, step2):
def step0(*args, **kwargs):
res = proxy(step2)
self.step1(*self.key(*args), **kwargs).addCallbacks(res, callbackArgs=args, callbackKeywords=kwargs)
return proxy.defer
step0.__name__ = "after(%s, %s)" % (self.step1.__name__, step2.__name__)
return step0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment