Created
June 13, 2017 11:00
-
-
Save barryrowlingson/92165ee303ccbb297c23e411d59ff0cf to your computer and use it in GitHub Desktop.
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 | |
# create an empty dict for the registry. | |
registry = {} | |
# a decorator that adds a dict entry 'path': handler_function to the | |
# registry | |
def register(path): | |
def qregister(handler_function): | |
registry[path] = handler_function | |
def func_wrapper(request): | |
return handler_function(request) | |
return wraps(handler_function)(func_wrapper) | |
return qregister | |
# register this function at path /here | |
@register("/here") | |
def handler(request): | |
print "Handling with request string ",request | |
# and register this at /there | |
@register("/there") | |
def handler2(request): | |
print "Handler 2, with req = ",request | |
# simple registered function caller with no error handling: | |
def call_registered_function(path, request): | |
print "calling function registered at ",path | |
f = registry[path] | |
print f | |
f.__call__(request) | |
# and test | |
call_registered_function("/here","hello world") | |
call_registered_function("/there","/here2/bar/baz") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment