Skip to content

Instantly share code, notes, and snippets.

@dcramer
Last active December 10, 2015 16:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcramer/4463756 to your computer and use it in GitHub Desktop.
Save dcramer/4463756 to your computer and use it in GitHub Desktop.
Signals with decorator syntax in Django
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