2018-03-30 4:48 GMT+02:00 Pat Martin <wpmar...@gmail.com>: > Hello all, > > I have written the following program. It generates a template for Pelican > web site static generator. It works just fine, it generates the template > and then I put the info in it to customize. But I was wondering, is this > the "right" way to do it in python? > > #!/usr/bin/env python3 > """Generate a Pelican markdown base page.""" > > import argparse > import datetime > > > def Main(): > """Run if run as a program.""" > parser = argparse.ArgumentParser() > parser.add_argument("-T", "--title", type=str, required=True, > help='Title for site, also generates the slug', > metavar="") > parser.add_argument("-c", "--category", required=True, > help='Category or categories of post', metavar="") > parser.add_argument("-t", "--tags", type=str, required=True, > help="Tags for post", metavar="") > parser.add_argument("-a", "--author", type=str, default="Pat Martin", > help="Author of post", metavar="") > args = parser.parse_args() > > now = datetime.datetime.now() > slug = args.title.replace(" ", "-").lower() > > with open("{}.md".format(slug), 'w') as f: > f.write("Title: {}\n".format(args.title)) > f.write("Date: {}-{}-{} {}:{}\n".format(now.year, > now.month, > now.day, > now.hour, > now.minute)) > f.write("Modified: {}-{}-{} {}:{}\n".format(now.year, > now.month, > now.day, > now.hour, > now.minute)) > f.write("Category: {}\n".format(args.category)) > f.write("Slug: {}\n".format(slug)) > f.write("Authors: {}\n".format(args.author)) > f.write("Summary: \n") > > > if __name__ == "__main__": > Main() > > > > Thanks for any input. > > WP > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor >
Hi Pat, my thoughts: 1.) def Main(): function names are written with small letter with underscore between the words check PEP-8 https://www.python.org/dev/peps/pep-0008/ http://docs.python-guide.org/en/latest/writing/style/ It is better to name the functions according to what they do, in this case for example: create_template() or something like that 2.) argparse it is good, but you can write more Pythonic code using click https://pypi.python.org/pypi/click/ it is also Pythonic to use / know the Python ecosystem (the packages) 3.) with open("{}.md".format(slug), 'w') as f: f.write("Date: {}-{}-{} {}:{}\n".format(now.year, now.month,.... Python is mainly about readability https://www.python.org/dev/peps/pep-0020/ you can use more meaningful variable names: template_file instead of f if you have Python 3.6, you can use the newest formatting method, which is more readable: template_file.write(f"Date: {now.moth}-{now.day}") etc https://www.python.org/dev/peps/pep-0498/ the more readable open: slug_file_name = f"{slug}.md" with open(slug_file_name, "w") as slug_file: and if you use Python 3, the open should contain encoding: with open(slug_file_name, "w", encoding="UTF-8") as slug_file: BR, George _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor