[Python-ideas] datetime.timedelta literals

Chris Barker chris.barker at noaa.gov
Mon Jun 4 16:41:13 EDT 2018


On Mon, Jun 4, 2018 at 12:58 AM, Paul Moore <p.f.moore at gmail.com> wrote:

> > That's good to hear, but if you don't mind asking, is your lack of
> > support because you use timedelta "programatically" instead of
> > hard-coded time units, or is there a different (or more) reason(s)?
> >
> > (I'm ready to yield, now I'm just curious.)
>
> I don't know what you mean by "programatically instead of hard-coded
> time units".


I think he means essentially:  Do you use timedelta with literal arguments?
-- as opposed to having it be the result of a calculation or read from a
file or ...


> In my code, I've never felt the need to be able to write
> something like "5min" rather than "timedelta(minutes=5)". Far from it
> - I find the former jarring, whereas the latter is perfectly clear, so
> even if the literal form were available I probably wouldn't use it
> much.


I'm the opposite - I use timedelta a fair bit, and find writing:

timedelta(hours=24)

Pretty awkward.

To the point that I make part of my "scripting" API take integer seconds
(or floating point hours, or...) rather than a timedelta objects, to save
my users from having to do:

from datetime import timedelta

call_a_function(...
                timestep = timedelta(seconds=10)
                ...                )

Rather than:

call_a_function(...
                timestep = 10
                ...
                )


The latter of which requires more typing, an extra import, and, most
importantly, a knowledge of datetime and the timedelta API.

So yeah -- I have a need for it.

All that being said, there are an number of things one might want a literal
for, and adding a huge pile of them is a bad idea, so I'm -1 on this
proposal anyway.

It does make me think that I may want to add my own utilities to make this
easier:

def seconds(s)
    return  timedelta(seconds=s)

etc.

Then I could add them to my "scripting" library, and my users could do:

call_a_function(...
                timestep = seconds(10)
                ...
                )

Not so bad.

In fact, maybe it would be a good idea to add such utilities to the
datetime module...


ANOTHER NOTE:

The interface to timedelta is hard to discover because the docstring is
incomplete:

In [2]: timedelta?
Init signature: timedelta(self, /, *args, **kwargs)
Docstring:      Difference between two datetime values.
File:           ~/miniconda2/envs/py3/lib/python3.6/datetime.py
Type:           type

Ouch! no idea what keyword argument it takes!

[but that's an argument for better docstrings, not adding literals...]

-CHB



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180604/7e9191cb/attachment-0001.html>


More information about the Python-ideas mailing list