I made myself a simple event system in python, and I found that the way I fire events was pretty much the same every time: either at the end of the call, or in front of it. It seems like it would be nice to have as a decorator. Here is the code I'm using:
from functools import wraps def fires(event): """ Returns a decorater that causes an `Event` to fire immediately before the decorated function is called """ def beforeDecorator(f): """Fires the event before the function executes""" @wraps(f) def wrapped(*args, **kargs): event.fire(*args, **kargs) return f(*args, **kargs) return wrapped def afterDecorator(f): """Fires the event after the function executes""" @wraps(f) def wrapped(*args, **kargs): result = f(*args, **kargs) event.fire(*args, **kargs) return result return wrapped
With this code, I can successfully write this:
@fires(myEvent) def foo(y): return y*y print func(2)
And everything works. The problem occurs when I try to write this:
@fires(myEvent).onceComplete def foo(y): return y*y print func(2)
This gives me a syntax error. Is there any special syntax for complex decorators? Does the parser remain after the first set of parentheses?
source share