[Python-ideas] Top 10 Python modules that need a redesign Was: Geo coordinates conversion in stdlib

Steven D'Aprano steve at pearwood.info
Sun Apr 5 06:00:49 CEST 2015


On Sat, Apr 04, 2015 at 04:16:47PM -0400, Alexander Belopolsky wrote:

> I am genuinely interested in the ways to improve date/time formatting in
> Python.  There are certainly better ways than stftime.  For example, ICU
> has a date/time format syntax that is much more readable: instead of
> "%Y-%m-%d %H:%M:%S", ICU format is "yyyy-MM-dd HH:mm:ss".
> 
> I don't think it is hard to find a way to introduce ICU format in
> datetime.__format__ while preserving backward compatibility.  For example,
> we may require that % is always "escaped" as '%' in ICU format
> and the presence of unescaped % can trigger strftime interpretation.
> 
> 
> [1] http://userguide.icu-project.org/formatparse/datetime

That would be much more understandable to anyone who has used Excel or 
Excel-compatible spreadsheets. I wouldn't even *attempt* to add it to 
datetime.__format__, I would create either:

- a new datetime method which uses ICU/Excel syntax directly:

    today = datetime.datetime.now().format_picture("yyyy-MM-dd")

- or a new module function which converts ICU/Excel syntax to 
  strftime format, which would allow post-processing in full 
  generality:

    fmt = "Today is: " + datetime.make_format("yyyy-MM-dd")
    today = datetime.now().strftime(fmt, datetime.now())


Either is much more explicit and less magical than something which 
tries to guess whether a format string is ICU format or strftime format 
from the format string.

But frankly, I would be satisfied with the list and meaning of format 
codes being documented somewhere where I can inspect it at runtime using 
help(), without having to run `man strftime` on a Linux system or search 
the Internet. Even something as trivial as giving the datetime module a 
list of format codes would work for me:

print(datetime.CODES)
-> [('%y', 'two digit year'), ('%Y', 'four digit year'), ... ]

although having it pretty-print by default would be nice :-)

As I understand it, the list of codes accepted is platform-dependent. 
While many codes are standard from machine to machine, some are not 
available on some systems.


-- 
Steve


More information about the Python-ideas mailing list