Created
June 22, 2018 04:34
-
-
Save rokujyouhitoma/138ee2791df66e4679177edb98aa3dc5 to your computer and use it in GitHub Desktop.
context decorator
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
# See: https://blog.amedama.jp/entry/2015/10/16/224740 | |
import inspect | |
def context(session = Session): | |
def _decorator(func): | |
def __decorator(*args, **kwargs): | |
name = "context" | |
value = "some thing" | |
try: | |
args_names = inspect.getargspec(func)[0] | |
target_pos = args_names.index(name) | |
args_list = list(args) | |
args_list.insert(target_pos, value) | |
ret = func(*args_list, **kwargs) | |
finally: | |
return ret | |
return __decorator | |
return _decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment