[Python-ideas] datetime.timedelta literals

Robert Vanden Eynde robertvandeneynde at hotmail.com
Mon Jun 25 19:03:54 EDT 2018


I found it fun to be able to write minutes(50) alongside with 50 * minutes so I did that :

from datetime import date, time, datetime, timedelta

class CallableTimedelta(timedelta):
    def __call__(self, x):
        return self * x

seconds, milliseconds, microseconds, days, hours, minutes, weeks = (CallableTimedelta(**{x:1}) for x in ('seconds', 'milliseconds', 'microseconds', 'days', 'hours', 'minutes', 'weeks'))

print(minutes(50) / seconds) # 3000.0
print(50 * minutes / seconds) # 3000.0
print(minutes(50).total_seconds()) # 3000.0




2018-06-07 13:34 GMT+02:00 Pål Grønås Drange <paal.drange at gmail.com<mailto:paal.drange at gmail.com>>:

For closure, I've added a package, timeliterals

(env) [pgdr at hostname ~]$ pip install timeliterals
(env) [pgdr at hostname ~]$ python
>>> from timeliterals import *
>>> 3*hours
datetime.timedelta(0, 10800)
>>> 3*minutes
datetime.timedelta(0, 180)
>>> 3*seconds
datetime.timedelta(0, 3)

The source code is at https://github.com/pgdr/timeliterals

I'm not going to submit a patch to datetime at this time, but I will if people
would be interested.

- Pål

On 5 Jun 2018 13:56, "Jacco van Dorp" <j.van.dorp at deonet.nl<mailto:j.van.dorp at deonet.nl>> wrote:
i'd also be pretty simple to implement....

Just list:
minute = timedelta(minutes=1)
hour = timedelta(hours=1)
etc...

and you could import and use them like that. Or if you really want to
write 5*m, the just from datetime import minute as m
_______________________________________________
Python-ideas mailing list
Python-ideas at python.org<mailto:Python-ideas at python.org>
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

_______________________________________________
Python-ideas mailing list
Python-ideas at python.org<mailto:Python-ideas at python.org>
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180625/b09f6ea5/attachment.html>


More information about the Python-ideas mailing list