[Tutor] pythonic

Albert-Jan Roskam sjeik_appie at hotmail.com
Sun Apr 1 15:20:10 EDT 2018


On Mar 30, 2018 10:39, Alan Gauld via Tutor <tutor at python.org> wrote:
>
> 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))

Lately I've been using format(), which uses __format__, because I find it slightly more readable:
format(datetime.now(), "%Y-%m-%d %H:%M")


More information about the Tutor mailing list