Pål Grønås Drange, I do like the idea of literals typed with scientific units, but I often get short variable names mixed up, so I am not sure if I could use them without a cheat sheet. Formatting datetime is a good example of how confusing a collection of short names can get: Is month %m or %M? Is minute %m or %i? https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
In case you are thinking, "Kyle, how can you even *think* "%i" means minutes?!", please see https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format :) I made a class, with magic methods, to get close to what you want: (2.5*HOUR - 14*MINUTE + 9300*MILLISECOND).total_seconds() I used full names for less confusion, but you can do the same with shorter names: (2.5*h- 14*min + 9300*ms).total_seconds() Maybe the Python parser can be made to add an implied multiplication between a-number-followed-directly-by-a-variable-name. If so, then I could write: (2.5HOUR - 14MINUTE + 9300MILLISECOND).total_seconds() You can define your short, domain specific, suffixes: (2.5h - 14m + 9300ms).total_seconds() On 2018-06-02 08:21, Pål Grønås Drange wrote: > Elevator pitch: > > (2.5h - 14min + 9300ms).total_seconds() > # 8169.3 > > from datetime import datetime as dt > start = dt.now() > end = dt.now() > (end-start) < 5s > # True > > > chrono::duration: > > In C++ 14 the std::chrono::duration was introduced which corresponds > somewhat to > datetime.timedelta. > > C++ 14 introduced so-called chrono literals[1], which are literals > specified as > [number][h|min|s|ms|us|ns], e.g. > > * 2.5h > * 14min > * 9300ms > > These literals should correspond to > > * datetime.timedelta(0, 9000) # 2.5h = 2.5*3600 = 9000 seconds > * datetime.timedelta(0, 840) # 14min = 14*60 = 840 seconds > * datetime.timedelta(0, 9, 300000) # 9300ms = 9 seconds + 3*10^5 > microseconds > > > If a literal was interpreted as a datetime.timedelta, the following > would work > out of the box: > > 2.5h - 14min + 9300ms * 2 > > which would correspond to > > from datetime import timedelta as D > > D(hours=2.5) - D(minutes=14) + D(milliseconds=9300) * 2 > # datetime.timedelta(0, 8178, 600000) # (*2 precedes, so that's to be > expected) > > > (D(hours=2.5) - D(minutes=14) + D(milliseconds=9300)) * 2 > # datetime.timedelta(0, 16338, 600000) > > > > Notes: > > * C++ uses `min` instead of `m`. `min` is a keyword in Python. > * In C++, `1d` means the first day of a month [2]. > * In C++, `1990y` means the year 1990 (in the Proleptic Gregorian > calendar) [3]. > * C++ have the types signed integers and not floats, so 2.5h would not > be valid. > * My apologies if this has been discussed before; my search-fu gave me > nothing. > > > > References: > > > [1] std::literals::chrono_literals::operator""min > http://en.cppreference.com/w/cpp/chrono/operator%22%22min > > [2] http://en.cppreference.com/w/cpp/chrono/day > > [3] http://en.cppreference.com/w/cpp/chrono/year > > > Best regards, > Pål Grønås Drange > > > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/