Skip to main content

Measure the amount of time that elapses between lap times

Project description

la-stopwatch

Measure the amount of time that elapses between lap times.

install

pip install la-stopwatch

usage

There is two versions of stopwatch:

  • StopwatchNS
  • Stopwatch

While both measure using nanoseconds, the second option convert nanoseconds to timedelta before returning any time measurement. All examples will be using Stopwatch but both have the same methods.

Time start when Stopwatch is created.

from la_stopwatch import Stopwatch


stopwatch = Stopwatch()

with open("filename", "r") as f:
    raw = f.readlines()

print(stopwatch.duration()) # 0:00:00.000292
print(stopwatch) # same as above

Record each lap time for future analysis.

from la_stopwatch import Stopwatch


stopwatch = Stopwatch()

with open("filename1", "r") as f:
    raw = f.readlines()
stopwatch.record()

with open("filename2", "r") as f:
    raw = f.readlines()
stopwatch.record()

with open("filename3", "r") as f:
    raw = f.readlines()
stopwatch.record("last record")

# Dictionary with all records
print(stopwatch.get_records())

# First record in the dictionary
print(stopwatch.get_record(0))

# Record with name "last record"
print(stopwatch.get_record("last record"))

Some methods return the Stopwatch so you can chain method calls. For example, you can record how much time take to open each file if you reset every time after recording.

from la_stopwatch import Stopwatch


stopwatch = Stopwatch()

with open("filename1", "r") as f:
    raw = f.readlines()
stopwatch.record().reset()

with open("filename2", "r") as f:
    raw = f.readlines()
stopwatch.record()

print(stopwatch.get_record(0))
print(stopwatch.get_record(1))

Use log() method to print/log the duration.

from la_stopwatch import Stopwatch


stopwatch = Stopwatch()

with open("filename", "r") as f:
    raw = f.readlines()

stopwatch.log("Duration: %(duration)s")
from logging import getLogger, basicConfig
from la_stopwatch import Stopwatch


basicConfig(level=0)

stopwatch = Stopwatch(logger=getLogger())

with open("filename", "r") as f:
    raw = f.readlines()

stopwatch.log("Duration: %(duration)s")

There is support for context manager.

from la_stopwatch import Stopwatch


with Stopwatch("Duration: %(duration)s"):
    with open("filename", "r") as f:
        raw = f.readlines()

And support for decorator.

from la_stopwatch import Stopwatch


@Stopwatch("Duration: %(duration)s")
def open_file():
    with open("filename", "r") as f:
        raw = f.readlines()

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

la-stopwatch-0.0.1.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

la_stopwatch-0.0.1-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

Supported by

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