A package for capturing and comparing function input/output snapshots
Reason this release was yanked:
contained bug for multiple calls
Project description
Detective Snapshot 🕵️♂️🔍
A Python package for capturing and comparing function input/output snapshots. Perfect for debugging, testing, and understanding complex function call hierarchies.
Features
- 📸 Capture function inputs and outputs
- 🌳 Track nested function calls
- 🎯 Select specific fields to snapshot
- 📦 Support for Python objects, dataclasses, and protobufs
Installation
pip install detective-snapshot
Quick Start
Enable debug mode to write snapshots:
export DEBUG=true
With debug mode on, each call to an outermost decorated function creates a new snapshot file in ./debug_snapshots/ with a unique UUID.
Here's a simple example using a library catalog system:
from detective import snapshot
@snapshot()
def get_book_details(book):
author = get_author(book["author_id"])
return f"{book['title']} by {author}"
@snapshot()
def get_author(author_id):
# Simulate database lookup
return "J.K. Rowling"
# Use the functions
book = {
"title": "Harry Potter",
"author_id": "jkr_001"
}
result = get_book_details(book)
This will create a debug file in ./debug_snapshots/ with content like:
{
"FUNCTION": "get_book_details",
"INPUTS": {
"book": {
"title": "Harry Potter",
"author_id": "jkr_001"
}
},
"OUTPUT": "Harry Potter by J.K. Rowling",
"CALLS": [
{
"FUNCTION": "get_author",
"INPUTS": {
"author_id": "jkr_001"
},
"OUTPUT": "J.K. Rowling"
}
]
}
Field Selection
Detective Snapshot supports both its own simple field selection syntax and full JSONPath expressions out of the box. You can capture specific fields using various selection patterns:
@snapshot(
input_fields=["book.title", "book.author_id"],
output_fields=["name"]
)
def process_book(book):
# Only specified fields will be captured
pass
Supported Field Selection Patterns
| Pattern | Example | Description |
|---|---|---|
| Direct Field | name |
Select a field directly from root |
| Nested Field | user.address.city |
Navigate through nested objects |
| Array Index | books[0].title |
Select specific array element |
| Array Wildcard | books[*].title |
Select field from all array elements |
| Multiple Fields | user.(name,age) |
Select multiple fields from an object |
| Wildcard Object | users.*.name |
Select field from all child objects |
| Args Syntax | args[0].name |
Select from function arguments |
| Mixed Access | users[*].addresses.*.city |
Combine array and object access |
| JSONPath | $.users[?(@.age > 18)].name |
Use full JSONPath expressions |
For more examples of field selection patterns, check out our test files - particularly test_snapshot_fields_selection.py which contains comprehensive examples of different selection patterns and edge cases.
Advanced Usage
Capture Complex Objects
@dataclass
class Book:
title: str
author: str
chapters: List[Chapter]
@snapshot(input_fields=["book.chapters[*].title"])
def get_chapter_titles(book: Book):
return [chapter.title for chapter in book.chapters]
Handle Nested Function Calls
@snapshot()
def process_library(library):
books = get_books(library.id)
return categorize_books(books)
@snapshot()
def get_books(library_id):
return ["Book1", "Book2"]
@snapshot()
def categorize_books(books):
return {"fiction": books}
The debug file will include the complete call hierarchy with inputs and outputs for each function.
Contributing
Contributions are welcome! Please check out our Contributing Guide for details.
License
MIT License - see LICENSE for details.
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 detective_snapshot-0.1.1.tar.gz.
File metadata
- Download URL: detective_snapshot-0.1.1.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f65fb8f844260bae522dc455d922c61a66f7098fae31183d729e6342f29bf514
|
|
| MD5 |
05b5aa62072bfba8561b77724fa7ba97
|
|
| BLAKE2b-256 |
01b54c5e37bf531440f1f32db272c350c326199b669b5028341b10ccf7e3371b
|
File details
Details for the file detective_snapshot-0.1.1-py3-none-any.whl.
File metadata
- Download URL: detective_snapshot-0.1.1-py3-none-any.whl
- Upload date:
- Size: 21.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a12ab57afab3e8a5037c2e711b627a1fe64af5e03db8a7aa2e239de2ede3f4eb
|
|
| MD5 |
e980496a7b8dd9f175aabac8ba07ab10
|
|
| BLAKE2b-256 |
3833183982f69349be4c4b37ae1282f3013b26df13390bddd75dc319daf926eb
|