Created
April 18, 2012 11:26
-
-
Save imankulov/2412992 to your computer and use it in GitHub Desktop.
Registrar decorator
This file contains hidden or 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
""" | |
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