<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><br>
</p>
<p>Pål Grønås Drange, <br>
</p>
<p>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?
<a class="moz-txt-link-freetext" href="https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior">https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior</a><br>
</p>
<p>In case you are thinking, "Kyle, how can you even *think* "%i"
means minutes?!", please see
<a class="moz-txt-link-freetext" href="https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format">https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format</a>
:)<br>
</p>
I made a class, with magic methods, to get close to what you want:<br>
<p>(2.5*HOUR - 14*MINUTE + 9300*MILLISECOND).total_seconds()</p>
<p>I used full names for less confusion, but you can do the same
with shorter names:<br>
</p>
<p>(2.5*h- 14*min + 9300*ms).total_seconds()</p>
<p><br>
</p>
<p>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:<br>
</p>
<p>(2.5HOUR - 14MINUTE + 9300MILLISECOND).total_seconds()</p>
<p>You can define your short, domain specific, suffixes:<br>
</p>
(2.5h - 14m + 9300ms).total_seconds()
<p><br>
</p>
<br>
<br>
<div class="moz-cite-prefix">On 2018-06-02 08:21, Pål Grønås Drange
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAPL7A87TnwvfO=m3Xu7JHfjL493L+KZDARpB9oi4Mw1HKydYmA@mail.gmail.com">
<div dir="ltr">Elevator pitch:<br>
<br>
(2.5h - 14min + 9300ms).total_seconds()<br>
# 8169.3<br>
<br>
from datetime import datetime as dt<br>
start = dt.now()<br>
end = dt.now()<br>
(end-start) < 5s<br>
# True<br>
<br>
<br>
chrono::duration:<br>
<br>
In C++ 14 the std::chrono::duration was introduced which
corresponds somewhat to<br>
datetime.timedelta.<br>
<br>
C++ 14 introduced so-called chrono literals[1], which are
literals specified as<br>
[number][h|min|s|ms|us|ns], e.g.<br>
<br>
* 2.5h<br>
* 14min<br>
* 9300ms<br>
<br>
These literals should correspond to<br>
<br>
* datetime.timedelta(0, 9000) # 2.5h = 2.5*3600 = 9000 seconds<br>
* datetime.timedelta(0, 840) # 14min = 14*60 = 840 seconds<br>
* datetime.timedelta(0, 9, 300000) # 9300ms = 9 seconds +
3*10^5 microseconds<br>
<br>
<br>
If a literal was interpreted as a datetime.timedelta, the
following would work<br>
out of the box:<br>
<br>
2.5h - 14min + 9300ms * 2<br>
<br>
which would correspond to<br>
<br>
from datetime import timedelta as D<br>
<br>
D(hours=2.5) - D(minutes=14) + D(milliseconds=9300) * 2<br>
# datetime.timedelta(0, 8178, 600000) # (*2 precedes, so that's
to be expected)<br>
<br>
<br>
(D(hours=2.5) - D(minutes=14) + D(milliseconds=9300)) * 2<br>
# datetime.timedelta(0, 16338, 600000)<br>
<br>
<br>
<br>
Notes:<br>
<br>
* C++ uses `min` instead of `m`. `min` is a keyword in Python.<br>
* In C++, `1d` means the first day of a month [2].<br>
* In C++, `1990y` means the year 1990 (in the Proleptic
Gregorian calendar) [3].<br>
* C++ have the types signed integers and not floats, so 2.5h
would not be valid.<br>
* My apologies if this has been discussed before; my search-fu
gave me nothing.<br>
<br>
<br>
<br>
References:<br>
<br>
<br>
[1] std::literals::chrono_literals::operator""min<br>
<a
href="http://en.cppreference.com/w/cpp/chrono/operator%22%22min"
moz-do-not-send="true">http://en.cppreference.com/w/cpp/chrono/operator%22%22min</a><br>
<br>
[2] <a href="http://en.cppreference.com/w/cpp/chrono/day"
moz-do-not-send="true">http://en.cppreference.com/w/cpp/chrono/day</a><br>
<br>
<div>[3] <a href="http://en.cppreference.com/w/cpp/chrono/year"
moz-do-not-send="true">http://en.cppreference.com/w/cpp/chrono/year</a></div>
<div><br>
</div>
<div><br>
</div>
<div>Best regards,</div>
<div>Pål Grønås Drange</div>
<div><br>
</div>
</div>
<!--'"--><br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Python-ideas mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python-ideas@python.org">Python-ideas@python.org</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a>
Code of Conduct: <a class="moz-txt-link-freetext" href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a>
</pre>
</blockquote>
<br>
</body>
</html>