Last active
December 10, 2015 16:49
-
-
Save dcramer/4463756 to your computer and use it in GitHub Desktop.
Signals with decorator syntax in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from django.dispatch import Signal | |
class BetterSignal(Signal): | |
def connect(self, receiver=None, **kwargs): | |
""" | |
Support decorator syntax: | |
>>> @signal.connect(sender=type) | |
>>> def my_receiver(**kwargs): | |
>>> pass | |
""" | |
def wrapped(func): | |
return super(BetterSignal, self).connect(func, **kwargs) | |
if receiver is None: | |
return wrapped | |
return wraps(receiver)(wrapped(receiver)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment