Skip to main content

pickle-debug

Pickle expensive operations for debugging operation

When debugging code that runs after a slow step (loading a large file, a long computation, a network call), re-running the slow step on every iteration wastes time. pickle_debug caches the return value of selected functions on disk, so the first call computes and stores the result and later calls with the same arguments load it from the cache.

Requires Python 3.12 or later. No dependencies.

Usage

import pickle_debug

@pickle_debug.cached
def load_data(path, limit=None):
    ...  # slow

pickle_debug.enable("/tmp/mycache")

load_data("big.csv")   # computed, then stored
load_data("big.csv")   # read from cache

Caching is off until enable() is called. Until then, decorated functions simply call through. This makes it safe to leave the @cached decorators in place and turn caching on only while debugging.

Decorating a function from another module

A decorator is an ordinary function, so you can apply it after importing:

import other_module
other_module.slow = pickle_debug.cached(other_module.slow)

This affects callers that look up other_module.slow at call time. Modules that already did from other_module import slow keep the undecorated function, so patch before they import it.

Methods

slow = Slow()
slow.invoke = pickle_debug.cached(slow.invoke)   # one instance

Slow.invoke = pickle_debug.cached(Slow.invoke)   # all instances

API

enable(directory)

Turn caching on, storing results in directory (a str or Path). The directory is created if needed and checked for writability. Raises OSError if it cannot be created or written.

cached(func)

Decorator. Returns a wrapper (using functools.wraps) that:

  1. calls func directly if caching is not enabled;
  2. otherwise builds a key from func.__module__, func.__qualname__, the positional arguments and the sorted keyword arguments, and hashes it with SHA-256;
  3. returns the pickled result from <directory>/<hash>.pickle if present;
  4. otherwise calls func, writes the result to the cache, and returns it.

Results are written to a temporary file and then renamed, so an interrupted run does not leave a partial cache entry.

reset()

Delete the whole cache directory. It refuses (raises RuntimeError) if the directory contains anything other than .pickle and .tmp files, to avoid deleting unrelated data if enable() was pointed at the wrong place. It does nothing if caching is not enabled or the directory does not exist.

Caveats

  • Arguments and results must be picklable. The arguments are pickled to build the key, and the result is pickled to store it.
  • The key is the pickled arguments. Equal arguments that pickle differently (for example sets with a different iteration order) may miss the cache. Objects whose pickle changes between calls never hit it.
  • The key does not include the function's code or any files it reads. If you change the function or its input data, call reset() or use a new cache directory, or you will get stale results.
  • Bound methods and self. A wrapped bound method (slow.invoke) does not include self in the key, so all instances share entries. A wrapped class function (Slow.invoke) includes self, so the instance must be picklable and its state is part of the key.
  • This is a debugging aid. Do not use it as a production cache. Cached files are loaded with pickle, so only use a directory you trust.

Logging

The module logs to the pickle_debug logger at DEBUG level when it enables caching, reads from the cache, or stores a result.

Metadata

Release files for pickle-debug 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pickle-debug 1.0
File Size Uploaded
pickle_debug-1.0.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pickle-debug 1.0
File Interpreter ABI Platform
pickle_debug-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / pickle_debug-1.0.tar.gz

Download URL pickle_debug-1.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0adae9912668cf9b86019f59d3f1c26c93910f6f5f0f5a2e7a1620284866958b
BLAKE2b-256 checksum
How to use checksums
d7679f99a2cfe68b6a4ae602343b6cde81edc75315e8e5a6303c31352c247d7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / pickle_debug-1.0-py3-none-any.whl

Download URL pickle_debug-1.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63a242f4a922f40ea92a5dded5b7a7611771b061713032083e9764502685eff3
BLAKE2b-256 checksum
How to use checksums
0fde88653701094e0247006fe6b7d2aa9e29417bc6e68e1ec46a45ca9bd58cbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

2.0

2 release files

1.2

2 release files

1.1

2 release files

This release

1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page