Python file structure

Chris Angelico rosuav at gmail.com
Wed May 13 22:03:49 EDT 2015


On Thu, May 14, 2015 at 4:34 AM, Tim Chase
<python.list at tim.thechases.com> wrote:
> Usually mine look something like
>
>   def do_real_work(options, args):
>     ...
>   def main():
>     parser = [optparse,argparse,docopt]....
>     options, args = parser.parse_args()
>     do_real_work(options, args)
>   if __name__ == "__main__":
>     main()
>
> since my real-work function usually relies on configuration
> (sometimes this also includes a config-file or environment variables
> being munged into some "options" data structure).
>

Sure. I rather dislike the whole duplication that that entails,
though, so I try to have the functions themselves do their own
argparse config. To that end, I put together a new project on PyPI,
but have now deprecated it in favour of Clize; the upshot is that my
main function becomes trivial again:

https://github.com/Rosuav/LetMeKnow/blob/master/letmeknow.py

@command
def await(...):
    """argparse config comes from here"""

if __name__ == "__main__":
    clize.run(commands)

So it comes and goes a bit. If there's real content in your main(),
then by all means, separate it out from do_real_work; but if the work
is all done elsewhere, not much point with main().

ChrisA



More information about the Python-list mailing list