Skip to content

Instantly share code, notes, and snippets.

@imankulov
Created April 18, 2012 11:26
Show Gist options
  • Save imankulov/2412992 to your computer and use it in GitHub Desktop.
Save imankulov/2412992 to your computer and use it in GitHub Desktop.
Registrar decorator
"""
Registrar decorator can be used to add objects (function and classes) to the
registry.
>>> reg = Registrar()
>>> @reg('foo')
... def foo():
... print 'foo'
...
>>> @reg('bar')
... def bar():
... print 'bar'
...
>>> reg.registry
{'foo': <function foo at 0x...>, 'bar': <function bar at 0x...>}
>>> reg.registry['foo']()
foo
>>> reg.registry['bar']()
bar
"""
class Registrar(object):
def __init__(self):
self.registry = {}
def __call__(self, key):
def wrapper(obj):
self.registry[key] = obj
return obj
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment