[Python-ideas] datetime.timedelta literals
Pål Grønås Drange
paal.drange at gmail.com
Sun Jun 3 06:53:19 EDT 2018
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180603/0a75b295/attachment.html>
More information about the Python-ideas
mailing list