Skip to main content

A lightweight stopwatch with context manager, live reads, lap support, and human-readable formatting

Project description

elapse

A lightweight Python stopwatch with context manager support, live reads mid-execution, lap timing, and human-readable duration formatting. Zero dependencies.

Install

pip install elapse

Usage

Basic timing with context manager

from elapse import StopWatch

with StopWatch() as sw:
    do_work()
    print(f"so far: {sw}")  # reads live while still running

print(f"total: {sw}")  # final time after exiting

Output:

so far: 1.234 s
total: 2.507 s

Durations auto-scale to human-readable units:

0.042 s
5 min 33.300 s
2 h 9 min 37.700 s

Measuring multiple blocks

A stopped stopwatch can be reused to accumulate time across separate blocks:

with StopWatch() as duration:
    stuff_1()

non_measured_stuff()

with duration:
    stuff_2()

print(duration)  # stuff_1() + stuff_2(), excluding non_measured_stuff()

Nesting stopwatches

Use separate instances to measure total and per-phase time simultaneously:

with StopWatch() as total:
    with StopWatch() as phase_1:
        stuff_1()
    with StopWatch() as phase_2:
        stuff_2()

print(f"total: {total}, phase 1: {phase_1}, phase 2: {phase_2}")

Laps

sw = StopWatch().start()
run_phase_1()
sw.lap()
run_phase_2()
sw.lap()
sw.stop()
print(sw)  # 5.200 s [2.100 s, 3.100 s]

Manual start/stop

sw = StopWatch()
sw.start()
# ... work ...
sw.stop()
print(sw.seconds())   # float
print(sw.delta())      # timedelta

sw.reset()
sw.start()
# ... more work ...

Calling start() on an already-running stopwatch raises RuntimeError. Calling stop() on a stopped watch is a no-op. reset() clears elapsed time and laps but keeps the watch running if it was running.

Precision

Control decimal places with the precision parameter (default 3):

print(StopWatch(elapsed_seconds=1.5, precision=1))  # 1.5 s
print(StopWatch(elapsed_seconds=1.5, precision=5))  # 1.50000 s

Standalone duration formatting

from elapse import duration_str

duration_str(90.5)      # '1 min 30.500 s'
duration_str(7200, 0)   # '2 h 0 min 0 s'

License

MIT

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

elapse-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

elapse-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: elapse-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for elapse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 32255fb90f2f149ba9d1189952da44728ff5cac729499b6ad32774d9fea9f634
MD5 a66a4f80041ef7d50fcc6502a0a4a3b1
BLAKE2b-256 0700cddd6225f678a16c7b3d1f24d4ac0b03f8ed1b54dffef36304c1a1e7dfb2

See more details on using hashes here.

Provenance

The following attestation bundles were made for elapse-0.1.0.tar.gz:

Publisher: publish.yml on cathackk/elapse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: elapse-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for elapse-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba495eb3ebb1e9f9b565748dc6bdb5ccbd1b020c0e88b02e4e7f30d73dc1a3a0
MD5 01006c358df88a6d8a7b03f4784b37f3
BLAKE2b-256 b5c416703822ea2f7a0a532cf9f9dffbf652d1f8d3b2146b99715cd3f0b74646

See more details on using hashes here.

Provenance

The following attestation bundles were made for elapse-0.1.0-py3-none-any.whl:

Publisher: publish.yml on cathackk/elapse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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