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.0.tar.gz (999.2 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.0-py3-none-any.whl (996.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pocketwatch-0.1.0.tar.gz
  • Upload date:
  • Size: 999.2 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.0.tar.gz
Algorithm Hash digest
SHA256 88baec663b17ae970e5480bd4b8ad727e8028f8283b28c6ed6d20844b0ced272
MD5 900f62d032da6f597e6e23441f646b83
BLAKE2b-256 108bd0d6e0eb96a06011b5bc24dff95f495a29a9f3b105687b4fcaec8b16146c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pocketwatch-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 996.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f776230f065763f0dd5c048919c2ff3784d92a7e4ca30626beb8939bbcc83e8
MD5 d9bca218a36eed282eb248a6e618d1da
BLAKE2b-256 da992ad1d34bc04694906ac147cc444e44f9f30868cb208f0e14d2b43e349562

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