Skip to content

Instantly share code, notes, and snippets.

@squeaky-pl
Created September 14, 2016 15:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squeaky-pl/eccd621018283e057a1c5567c38fdc72 to your computer and use it in GitHub Desktop.
Save squeaky-pl/eccd621018283e057a1c5567c38fdc72 to your computer and use it in GitHub Desktop.
registry = {
'db': 'my database',
'redis': 'my redis',
'settings': 'my settings'
}
def inject(func):
def wrapper(*args, **kwargs):
injected = {k: registry[dep] for k, dep in func.__annotations__.items()}
bound = partial(func, **injected)
return bound(*args, **kwargs)
return wrapper
@inject
def my_func(value, fancy_db: 'db', useless_redis: 'redis'):
print(fancy_db, useless_redis, value)
my_func(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment