Allow initializing `attotime` objects with arbitrary `nanosecond` number

Issue #2 wontfix
stdedos created an issue

Instead of handling numerical conversions externally, teach the method (e.g. attotime.attotime) to automatically convert excess units of time to the necessary unit.

e.g. attotime.attotime(nanosecond=time.perf_counter_ns()) should not lead to

...
  File "/home/stdedos/.local/lib/python3.8/site-packages/attotime/objects/attotime.py", line 38, in __init__
    raise ValueError(
ValueError: nanosecond must be in 0..1000

Comments (2)

  1. Brandon Nielsen repo owner

    I do see the appeal, but the objects are designed to match the Python 3 API so are bounded accordingly.

    Perf counters generally shouldn't be used as an absolute time value ("only the difference between the results of two calls is valid"), so a delta might be a good fit for your use, eg.:

    >>> test1 = attotime.attotimedelta(nanoseconds=time.perf_counter_ns())
    >>> test2 = attotime.attotimedelta(nanoseconds=time.perf_counter_ns())
    >>> (test2 - test1).total_nanoseconds()
    Decimal('4149026897')
    
  2. stdedos reporter

    Eventually, that’s what I wanted to do, yes.

    I also wanted, after-the-fact, to “optimize this” to human-readable time, say print

    ~4,149s
    

    instead

  3. Log in to comment