Skip to main content

Custom timer

Project description

thyming

I couldn't find a timer that matched my needs, so I created my own. I don't exactly remember which ones I tried and what reservations I had but whatever.

Install thyming

pip install thyming
from thyming import Timer # duh

How to use thyming.Timer

There are two ways to use it: the ordinary way and as a context manager.

1. The ordinary way

You can use it the ordinary way, by creating a timer and measuring time with it multiple times.

t = Timer()
# do something here
t.measure() # measure time
# do something else
t.measure() # measure time again
# ...
t.stop() # timer stops
print(t.rtimes()) 
# [2.8563, 5.0682, 22.2241] # print recorded times rounded to n digits (4 by default)

# You can reuse the timer after stopping it
t.start()
# ...
t.stop()
print(t.rtimes()) 
# [5.516]
# previous recorded times are stored in t.prev__recorded _times
print(t.prev_times)
[[2.8563294369996584, 5.068209224999919, 22.224131080000006]]

2. The context manager way

(This is the magic of __enter__ and __exit__.)

with Timer() as t:
    # ...
    t.measure()
    # ...
    t.measure()
# you leave timer here

Logging time

Timer accepts a logger function, which takes a string and returns nothing (it's of type Callable[[str], None]). Theoretically, this could be lambda s: None but obviously we want to squeeze something useful out of it, so it's better to supply a function but prints the string argument to some output, e.g.: print or logging.info, which is the default.

import logging
logging.basicConfig(level=logging.INFO)
t = Timer().start()
print(t.logger) # <function info at 0x7f38ac8b99d0> # i.e. logging.info
print(t.logger == logging.info) # True
t = Timer().start()
sleep(1)
t.measure() # INFO:root:Elapsed time: 1.0011 seconds.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thyming-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

thyming-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file thyming-0.1.0.tar.gz.

File metadata

  • Download URL: thyming-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for thyming-0.1.0.tar.gz
Algorithm Hash digest
SHA256 41c0156c9163dee2e395641413d272c987f749f746a7dd9e98bad96ec87da68b
MD5 32d52d5385c059ad03aeea96c0951f04
BLAKE2b-256 eb51de54948b4ece38c332cc134b11814299af6a7962d11fca6a7b5ab9127763

See more details on using hashes here.

File details

Details for the file thyming-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: thyming-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for thyming-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f37cc5613abfafddb31a099083466c548c51045c4632d4142f80412c973e55d9
MD5 658c70a9de607c8591841bce5748ad9f
BLAKE2b-256 079d9ca28996408423f89d6b81d4aed42d54a1754661b3a9d0bd590b1f3cb702

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page