Drop-in stopwatch utility
Project description
Pocketwatch
Pocketwatch provides a simple stopwatch for timing any block of Python code. It can display desktop notifications, play a chime after long runs, and optionally collect profiling statistics.
Installation
pip install pocketwatch
Basic Usage
from pocketwatch import Pocketwatch
with Pocketwatch("load data"):
load_data()
The context manager prints the elapsed time when the block finishes. Pass a
logging.Logger if you also want the output sent to a log. Notifications and
sound are disabled unless explicitly requested.
with Pocketwatch(
"big task",
notify=True, # show a pop-up
notify_after=5.0, # minimum duration before popup
sound=True, # play ding.wav
sound_after=60.0, # minimum duration for sound
):
run_big_task()
You can capture intermediate checkpoints using mark() (or its alias
log()):
with Pocketwatch("steps") as pw:
load()
pw.mark("loaded data")
transform()
pw.log("transformed")
To enable cProfile integration, pass profile=True. By default the raw stats
are written to profile_output.prof; set profile_output_path=None to skip
writing the file. Call end(return_stats=True) to retrieve a
pstats.Stats instance.
with Pocketwatch(profile=True) as pw:
crunch_numbers()
stats = pw.end(return_stats=True)
Decorator Shortcut
A lightweight decorator mirrors the context manager. It uses the function name as the message label and prints the elapsed time. Provide a logger argument to forward output to your own logging setup.
from pocketwatch.decorators import stopwatch
@stopwatch
def do_work():
...
Custom options may be supplied when decorating:
@stopwatch(sound=True, notify=False)
def background_job():
...
Features
- Drop-in context manager for timing code blocks
- Desktop notifications via notifypy
- Optional sound playback with bundled audio files
- Incremental mode to suppress output for short runs
- Optional logging to a custom logger
- Checkpointing with
mark()orlog() - cProfile support with stats returned on request
- Works with Python 3.9+
- Licensed under the MIT license
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 pocketwatch-0.1.2.tar.gz.
File metadata
- Download URL: pocketwatch-0.1.2.tar.gz
- Upload date:
- Size: 999.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5eb1ee38b049a2628ea0cce49c881e6de5ce8ee7eee2f94ea8aeb200950dcd1c
|
|
| MD5 |
e3e87524d5cec8e3843a8157339832d3
|
|
| BLAKE2b-256 |
3be67fdc0d97f554844f1d8ee03063c8124ba90267133593f67a6a0905cdbe1b
|
File details
Details for the file pocketwatch-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pocketwatch-0.1.2-py3-none-any.whl
- Upload date:
- Size: 996.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e87a6eda787c9c12a5698eedcc43e0fc54d963bfe324307d22edf97d946c4ef5
|
|
| MD5 |
5fa994b2cc8d43e73a446e8181e635cf
|
|
| BLAKE2b-256 |
92f4a95a38dcf7d8fe28d2dfa97d81dc176a010ba4a732397e519f625b8412bc
|