Skip to content

Instantly share code, notes, and snippets.

@rokujyouhitoma
Created June 22, 2018 04:34
Show Gist options
  • Save rokujyouhitoma/138ee2791df66e4679177edb98aa3dc5 to your computer and use it in GitHub Desktop.
Save rokujyouhitoma/138ee2791df66e4679177edb98aa3dc5 to your computer and use it in GitHub Desktop.
context decorator
# 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