Created
September 7, 2012 10:50
-
-
Save myme/3665062 to your computer and use it in GitHub Desktop.
DI?
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
def _import(dep): | |
print "importing %s" % dep | |
imported = None | |
exec "import %s; imported = %s" % (dep, dep) | |
return imported | |
def inject(*deps): | |
def wrapper(fn): | |
loaded_deps = {} | |
def loader(*args, **kwargs): | |
if not len(loaded_deps) and len(deps): | |
for dep in deps: | |
loaded_deps[dep] = _import(dep) | |
kwargs['deps'] = loaded_deps | |
fn(*args, **kwargs) | |
return loader | |
return wrapper | |
if __name__ == '__main__': | |
@inject('os') | |
def fn(arg1, kwarg1='bar', deps={}): | |
os = deps['os'] | |
print 'fn:', os.curdir, arg1, kwarg1 | |
fn('foo') | |
fn('baz', kwarg1='quux') |
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
eplemos-dev :: try-bin/python/di » python di.py | |
importing os | |
fn: . foo bar | |
fn: . baz quux |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment