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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32255fb90f2f149ba9d1189952da44728ff5cac729499b6ad32774d9fea9f634
|
|
| MD5 |
a66a4f80041ef7d50fcc6502a0a4a3b1
|
|
| BLAKE2b-256 |
0700cddd6225f678a16c7b3d1f24d4ac0b03f8ed1b54dffef36304c1a1e7dfb2
|
Provenance
The following attestation bundles were made for elapse-0.1.0.tar.gz:
Publisher:
publish.yml on cathackk/elapse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
elapse-0.1.0.tar.gz -
Subject digest:
32255fb90f2f149ba9d1189952da44728ff5cac729499b6ad32774d9fea9f634 - Sigstore transparency entry: 1059820676
- Sigstore integration time:
-
Permalink:
cathackk/elapse@1c5412d3298355569293d0cae20013a9329da3ee -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/cathackk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1c5412d3298355569293d0cae20013a9329da3ee -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba495eb3ebb1e9f9b565748dc6bdb5ccbd1b020c0e88b02e4e7f30d73dc1a3a0
|
|
| MD5 |
01006c358df88a6d8a7b03f4784b37f3
|
|
| BLAKE2b-256 |
b5c416703822ea2f7a0a532cf9f9dffbf652d1f8d3b2146b99715cd3f0b74646
|
Provenance
The following attestation bundles were made for elapse-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on cathackk/elapse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
elapse-0.1.0-py3-none-any.whl -
Subject digest:
ba495eb3ebb1e9f9b565748dc6bdb5ccbd1b020c0e88b02e4e7f30d73dc1a3a0 - Sigstore transparency entry: 1059820680
- Sigstore integration time:
-
Permalink:
cathackk/elapse@1c5412d3298355569293d0cae20013a9329da3ee -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/cathackk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1c5412d3298355569293d0cae20013a9329da3ee -
Trigger Event:
release
-
Statement type: