Skip to main content

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() or log()
  • cProfile support with stats returned on request
  • Works with Python 3.9+
  • Licensed under the MIT license

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

pocketwatch-0.1.2.tar.gz (999.6 kB view details)

Uploaded Source

Built Distribution

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

pocketwatch-0.1.2-py3-none-any.whl (996.8 kB view details)

Uploaded Python 3

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

Hashes for pocketwatch-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5eb1ee38b049a2628ea0cce49c881e6de5ce8ee7eee2f94ea8aeb200950dcd1c
MD5 e3e87524d5cec8e3843a8157339832d3
BLAKE2b-256 3be67fdc0d97f554844f1d8ee03063c8124ba90267133593f67a6a0905cdbe1b

See more details on using hashes here.

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

Hashes for pocketwatch-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e87a6eda787c9c12a5698eedcc43e0fc54d963bfe324307d22edf97d946c4ef5
MD5 5fa994b2cc8d43e73a446e8181e635cf
BLAKE2b-256 92f4a95a38dcf7d8fe28d2dfa97d81dc176a010ba4a732397e519f625b8412bc

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