Skip to content

Instantly share code, notes, and snippets.

@vlcinsky
Created March 18, 2014 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlcinsky/9631795 to your computer and use it in GitHub Desktop.
Save vlcinsky/9631795 to your computer and use it in GitHub Desktop.
Create annotated function preserving original function intact
""" A study, how to create plac annotated command line script from bare function
preserving original function intact.
This is sometime needed, when the same function is used for call e.g. by a celery,
and for manual testing it is handy to have it in command line version.
Usually, the code (producing only annotated command line tool) looks like::
import plac
@plac.annotations(
name="your name",
surname="your surname"
)
def main(name, surname):
"have a fun with name and surname decorated"
print name, surname
if __name__ == "__main__":
plac.call(main)
Try from command line::
$ python placfun.py -h
usage: placfun.py [-h] name surname
have a fun with name and surname decorated
positional arguments:
name your name
surname your surname
optional arguments:
-h, --help show this help message and exit
and with params::
$ python placfun.py Jan Vlcinsky
Jan Vlcinsky
Following code does the same, but keeps the original "fun" function intact,
so that other importers do not have to use the decorated version.
For decorating explained see:: http://freepythontips.wordpress.com/2013/10/10/all-about-decorators-in-python/
"""
import plac
def fun(name, surname):
"""have a fun with name and surname decorated"""
print name, surname
main = plac.annotations(
name="your name",
surname="your surname"
) (fun)
if __name__ == "__main__":
plac.call(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment