[Python-ideas] datetime.timedelta literals

Joao S. O. Bueno jsbueno at python.org.br
Tue Jun 26 08:41:44 EDT 2018


I like the feel of it.


In [1]: import pint

In [2]: reg = pint.UnitRegistry()

In [3]: from extradict import MapGetter

In [4]: with MapGetter(reg):
  ...:     from reg import cm, min, hour, km
  ...:

In [5]: km
Out[5]: <Unit('kilometer')>

In [6]: 10 * km / hour
Out[6]: <Quantity(10.0, 'kilometer / hour')>

As for the request that started the thread - there was a thread about
this not long ago - less than 1 year, for sure. Please,
whoever intend to support it, check the arguments there.


On Sun, 3 Jun 2018 at 07:53, Pål Grønås Drange <paal.drange at gmail.com> wrote:
>
> > What about
> >
> > 2.5*h - 14*min + 9300*ms * 2
>
> That doesn't seem feasible to implement, however, that is essentially how the
> Pint [1] module works:
>
> import pint
> u = pint.UnitRegistry()
> (2.5*u.hour - 14*u.min + 9300*u.ms) * 2
> #  <Quantity(4.5385, 'hour')>
>
> ((2.5*u.hour - 14*u.min + 9300*u.ms) * 2).to('sec')
> #  <Quantity(16338.6, 'second')>
>
> > However why be limited to time units ? One would want in certain
> > application to define other units, like meter ? Would we want a litteral
> > for that ?
>
> Pint works with all units imaginable:
>
> Q = u.Quantity
> Q(u.c, (u.m/u.s)).to('km / hour')
> #  <Quantity(3.6 speed_of_light, 'kilometer / hour')>
>
>
> However, the idea was just the six (h|min|s|ms|us|ns) time literals; I believe
> time units are used more often than other units, e.g. in constructs like
>
> while end - start < 1min:
>    poll()
>    sleep(1s)  # TypeError
>    sleep(1s.total_seconds())  # works, but ugly
>
>
> [1] https://pypi.org/project/Pint/
>
> Best regards,
> Pål Grønås Drange
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list