Skip to content

Instantly share code, notes, and snippets.

@jdavis
Last active August 29, 2015 14:06
Show Gist options
  • Save jdavis/0e56e56e8cc955bc5fbc to your computer and use it in GitHub Desktop.
Save jdavis/0e56e56e8cc955bc5fbc to your computer and use it in GitHub Desktop.
Provides: {'do_something': <function do_something at 0x102f217d0>, 'do_something_else': <function strange_function_name at 0x102f2b8c0>}
Keybindings: {'t[2, 1]': <function command_t at 0x102f2b9b0>}
do something
doing something else 1337
provides = {}
keybindings = {}
class Modifiers:
shift = 1
meta = 2
def api_provides(name=None):
def decorator(f):
# Allow optional naming of what it provides
fname = name or f.func_name
provides[fname] = f
# Optional wrapping of the function for a normal decorator
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
return wrapper
return decorator
def keybinding(key, modifiers):
def decorator(f):
# A terrible key value, just doing this for demonstration
keybindings[str(key) + str(modifiers)] = f
# Optional wrapping of the function for a normal decorator
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
return wrapper
return decorator
class Test(object):
def __init__(self):
pass
@api_provides()
def do_something(self):
print 'do something'
@api_provides('do_something_else')
def strange_function_name(self, x):
print 'doing something else', x
@keybinding('t', [Modifiers.meta, Modifiers.shift])
def command_t(self):
print 'called command + shift + t'
def api_call(method, *args, **kwargs):
return provides[method](*args, **kwargs)
def keypress(keys):
# You get the picture =]
pass
def main():
t = Test()
print 'Provides:', provides
print 'Keybindings:', keybindings
api_call('do_something', t)
api_call('do_something_else', t, 1337)
keypress('M-T')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment