Skip to content

Instantly share code, notes, and snippets.

@cablehead
Created July 6, 2016 17:07
Show Gist options
  • Select an option

  • Save cablehead/7fc6d015a3a0c58d7ffdf50df9aaed0d to your computer and use it in GitHub Desktop.

Select an option

Save cablehead/7fc6d015a3a0c58d7ffdf50df9aaed0d to your computer and use it in GitHub Desktop.
import functools
def common(accepts):
def wrap(f):
f.__doc__ += """
*token* i take a token
"""
d = {}
exec("""
def __(self, token=None):
params = {}
if token:
params["token"] = token
else:
params["token"] = self.agent
f(self, params)
""", {'f': f}, d)
functools.update_wrapper(d['__'], f)
return d['__']
return wrap
class C(object):
agent = "agent default"
@common(["token"])
def bar(self, params):
"""
My docstring!
"""
print params
c = C()
print c.bar
print c.bar.__doc__
c.bar()
c.bar(token="hi")
@cablehead
Copy link
Author

$ python ~/tmp/params.py 
<bound method C.bar of <__main__.C object at 0x103d92850>>

        My docstring!

        *token* i take a token

{'token': 'agent default'}
{'token': 'hi'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment