Created
March 18, 2014 23:09
-
-
Save vlcinsky/9631795 to your computer and use it in GitHub Desktop.
Create annotated function preserving original function intact
This file contains hidden or 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
""" 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