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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pocketwatch-0.1.1.tar.gz
  • Upload date:
  • Size: 999.3 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.1.tar.gz
Algorithm Hash digest
SHA256 3177ee0dc3bc1fef1e52ac5717fce1b27f3c6f4df53b66744771e15693812076
MD5 d20d5543e283d8d13578745cd5da7219
BLAKE2b-256 62be2b5f3086e776c4d8169039d8d99299f0dba82860b18d16eae19547771de2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pocketwatch-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8f4d861ed1ae3f346510424ee1c307bf82cebb571abc79c3480dec073b67441b
MD5 ad5771fdbdc32bd62f6822e1487f3f2a
BLAKE2b-256 e3fd181cac2d290ec9e394262046047cb735a7d42c794a3dc0ff05446a797a60

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