Created
October 21, 2013 03:43
-
-
Save sp3c73r2038/7078350 to your computer and use it in GitHub Desktop.
call function by its name
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
import sys | |
if sys.version_info[0] == 3: | |
xrange = range | |
import functools | |
registered_functions = {} | |
def register_function(params): | |
ps = params.split(',') | |
def wrapper(func): | |
def template(*args): | |
if len(args) != len(ps): | |
raise TypeError("%s() takes exactly %d positional argument" | |
" (%d given)" %\ | |
(func.__name__, len(ps), len(args))) | |
kwargs = {} | |
for i in xrange(len(ps)): | |
kwargs[ps[i]] = args[i] | |
return func(**kwargs) | |
registered_functions[func.__name__] = functools.update_wrapper(template, func) | |
return wrapper | |
@register_function('sp_id,endpoint,env') | |
def sp_up(sp_id, env, endpoint): | |
print("sp_id: %s" % sp_id) | |
print("endpoint: %s" % endpoint) | |
print("env: %s" % env) | |
target = 'sp.up' | |
params = ['foo', '127.0.0.1:1002', 'dev'] | |
method = target.replace('.', '_') | |
method = registered_functions.get(method, None) | |
if method: | |
method(*params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment