[Tutor] pythonic

Alan Gauld alan.gauld at yahoo.co.uk
Fri Mar 30 04:37:16 EDT 2018


On 30/03/18 03:48, Pat Martin wrote:

> the "right" way to do it in python?

More or less, a couple of comments below...

> def Main():

Python function names begin with a lowercase letter by convention.

>     """Run if run as a program."""
>     parser = argparse.ArgumentParser()
...

> 
>     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))

Formatting of dates and times is usually done using
the time.strftime function which is specifically
designed for that. It might be worth taking a peek
at the docs on that one. You can call it directly
on a datetime object - 'now' in your case:

fmt="%Y-%m-%d %H:%M\n"
f.write(now.strftime(fmt))


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list