[Python-ideas] millisecond and microsecond times without floats

Gregory P. Smith greg at krypto.org
Tue Jun 23 02:03:14 CEST 2015


On Mon, Jun 22, 2015 at 4:15 PM Paul Sokolovsky <pmiscml at gmail.com> wrote:

>
>
> Hello from MicroPython, a lean Python implementation
> scaling down to run even on microcontrollers
> (https://github.com/micropython/micropython).
>
> Our target hardware base oftentimes lacks floating point support, and
> using software emulation is expensive. So, we would like to have
> versions of some timing functions, taking/returning millisecond and/or
> microsecond values as integers.
>
> The most functionality we're interested in:
>
> 1. Delays
> 2. Relative time (from an arbitrary starting point, expected to be
>    wrapped)
> 3. Calculating time differences, with immunity to wrap-around.
>
> The first presented assumption is to use "time.sleep()" for delays,
> "time.monotonic()" for relative time as the base. Would somebody gave
> alternative/better suggestions?
>
> Second question is how to modify their names for
> millisecond/microsecond versions. For sleep(), "msleep" and "usleep"
> would be concise possibilities, but that doesn't map well to
> monotonic(), leading to "mmonotonic". So, better idea is to use "_ms"
> and "_us" suffixes:
>
> sleep_ms()
> sleep_us()
> monotonic_ms()
> monotonic_us()
>

If you're going to add new function names, going with the _unit suffix
seems best.

Another option to consider: keyword only arguments.

time.sleep(ms=31416)
time.sleep(us=31415927)
time.sleep(ns=31415296536)
# We could use the long form names milliseconds, microseconds and
nanoseconds but i worry with those that people would inevitably confuse ms
with microseconds as times and APIs usually given the standard
abbreviations rather than spelled out.

time.monotonic(return_int_ns=True) ?
# This seems ugly.  time.monotonic_ns() seems better.

These should be acceptable to add to Python 3.6 for consistency.

I do not think we should have functions for each ms/us/ns unit if adding
functions.  Just choose the most useful high precision unit and let people
do the math as needed for the others.

Point 3 above isn't currently addressed by time module at all.
> https://www.python.org/dev/peps/pep-0418/ mentions some internal
> workaround for overflows/wrap-arounds on some systems. Due to
> lean-ness of our hardware base, we'd like to make this matter explicit
> to the applications and avoid internal workarounds. Proposed solution
> is to have time.elapsed(time1, time2) function, which can take values
> as returned by monotonic_ms(), monotonic_us(). Assuming that results of
> both functions are encoded and wrap consistently (this is reasonable
> assumption), there's no need for 2 separate elapsed_ms(), elapsed_us()
> function.
>

Reading the PEP my takeaway is that wrap-around of underlying deficient
system APIs should be handled by the Python VM for the user. It sounds like
we should explicitly spell this out though.

I don't think time.elapsed() could ever provide any utility in either case,
just use subtraction. time.elapsed() wouldn't know when and where the time
values came from and magically be able to apply wrap around or not to them.

-gps

So, the above are rough ideas we (well, I) have. We'd like to get wider
> Python community feedback on them, see if there're better/alternative
> ideas, how Pythonic it is, etc. To clarify, this should not be construed
> as proposal to add the above functions to CPython.
>
>
> --
> Best regards,
>  Paul                          mailto:pmiscml at gmail.com
> _______________________________________________
> 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/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150623/051eb8ea/attachment-0001.html>


More information about the Python-ideas mailing list