With Time
Context Managers and Decorator for common time operations
Quick start
Installation
pip intall with-time
Usage
Measuring duration, with PrintingTimer or LoggingTimer
with_timer.PrintingTimer(label: str = None, timer: Callable[[], float] = time.time)
Prints out how long context operation or function took. Message is prepended with label. By default elapsed running time will be measured but other time methods can be utilized such as time.process_time, time.monotomic, and so on.
>>> import time
>>> from with_time import PrintingTimer
>>>
>>> # Context Manager Example
>>> with PrintingTimer("Example"):
... time.sleep(.1337)
...
Example: 0.13398265838623047 seconds
>>>
>>> # Decorator Example
>>> @PrintingTimer("Decorator Example")
... def foo():
... time.sleep(.1337)
...
>>> foo()
Decorator Example: 0.13398265838623047 seconds
with_timer.LoggingTimer(label: str = None, log_level: int = None, timer: Callable[[], float] = time.time)
LoggingTimer works just the same way as PrintinTimer except duration is logged rather than printed. Log level can be customized using logging.debug, logging.warning…
>>> import time
>>> import logging
>>> from with_time import LoggingTimer
>>> logging.basicConfig(force=True)
>>>
>>> # Context Manager Example
>>> with LoggingTimer("Example"):
... time.sleep(.1337)
...
INFO:with_time.timer:Example: 0.13399600982666016 seconds
>>>
>>> # Decorator Example
>>> @LoggingTimer("Decorator Example")
... def foo():
... time.sleep(.1337)
...
>>> foo()
INFO:with_time.timer:Decorator Example: 0.13396501541137695 seconds
with_time.SignalTimeout(seconds: float, exception=None)
Raise an exception as an execution timeout. SignalTimeout uses signal implementation which is only supported on Unix like os. By default with_time.exceptions.TimeoutError but this can be changed by passing any initialized exception object as exception
SignalTimeout does attempt to restore signals it overwrites which make some but not all nested scenarios of timeout work.
>>> import time
>>> from with_time import SignalTimeout
>>> with SignalTimeout(.1000):
... time.sleep(.1337)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File ".../with_time/timeout.py", line 21, in _handler
raise self.exception
with_time.exceptions.TimeoutError: Timed out
>>>
>>> # Custom Exception
>>> with SignalTimeout(.1000, exception=RuntimeError("Oops")):
... time.sleep(.1337)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File ".../with_time/timeout.py", line 21, in _handler
raise self.exception
RuntimeError: Oops
Metadata
Release files for with-time 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| with_time-0.3.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| with_time-0.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 11.8 kB
Release files / with_time-0.3.tar.gz
| Download URL | with_time-0.3.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a239e51ec0e42906970621c0a9ffe4ea2ae5a19bebca817238c110ab87f9312c
|
|
BLAKE2b-256 checksum How to use checksums |
6d6af26dc65581c443c301216a51ee48cbe93c0cdff08d09678c484e7dc3cf84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
|
Release files / with_time-0.3-py2.py3-none-any.whl
| Download URL | with_time-0.3-py2.py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
f6f2bbb00ebdc4383bf04f061e3b3c83eb2d3395604f6e1fc06859d9a78f1c83
|
|
BLAKE2b-256 checksum How to use checksums |
e053ff2bab82dd6f15096bae26c8ba124dbedacd6d77be5ec6049bfbf54dff25
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
|