Skip to main content

A package for capturing and comparing function input/output snapshots

Project description

Detective Snapshot 🕵️‍♂️🔍

A Python package for capturing 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

Install

pip install detective-snapshot

Quick Start

from detective import snapshot

@snapshot()
def calculate_cat_food(cats):
    total = sum(get_cat_appetite(cat) for cat in cats)
    return f"Need {total}oz of food per day"

@snapshot()
def get_cat_appetite(cat):
    # Cats eat about 0.5oz per pound of weight
    return cat["weight"] * 0.5

# Feed the kitties
cats = [
    {"name": "Whiskers", "weight": 10},
    {"name": "Chonk", "weight": 15}
]
result = calculate_cat_food(cats)

With debug mode on, each call to an outermost decorated function creates a new snapshot file in ./debug_snapshots/ with a unique UUID:

{
    "FUNCTION": "calculate_cat_food",
    "INPUTS": {
        "cats": [
            {"name": "Whiskers", "weight": 10},
            {"name": "Chonk", "weight": 15}
        ]
    },
    "OUTPUT": "Need 12.5oz of food per day",
    "CALLS": [
        {
            "FUNCTION": "get_cat_appetite",
            "INPUTS": {"cat": {"name": "Whiskers", "weight": 10}},
            "OUTPUT": 5.0
        },
        {
            "FUNCTION": "get_cat_appetite",
            "INPUTS": {"cat": {"name": "Chonk", "weight": 15}},
            "OUTPUT": 7.5
        }
    ]
}

Field Selection

Capture only the fields you care about:

@snapshot(
    input_fields=["cat.name", "cat.weight"],
    output_fields=["daily_food"]
)
def calculate_cat_diet(cat):
    daily_food = cat["weight"] * 0.5
    return {"daily_food": daily_food, "feeding_schedule": "twice daily"}

Selection Patterns

Pattern Example Description
Direct Field name Select a field directly from root
Nested Field cat.weight Navigate through nested objects
Array Index cats[0].name Select specific array element
Array Wildcard cats[*].weight Select field from all array elements
Multiple Fields cat.(name,weight) Select multiple fields from an object
Wildcard Object pets.*.name Select field from all child objects
Args Syntax args[0].name Select from function arguments
Mixed Access pets[*].records.*.weight Combine array and object access

Full JSONPath support is also available for more complex queries.

Advanced Usage

Capture Complex Objects

@dataclass
class Cat:
    name: str
    breed: str
    medical_records: List[Record]

@snapshot(input_fields=["cat.medical_records[*].weight"])
def track_weight_history(cat: Cat):
    return [record.weight for record in cat.medical_records]

Handle Nested Function Calls

@snapshot()
def process_cattery(cattery):
    cats = get_cats(cattery.id)
    return categorize_cats(cats)

@snapshot()
def get_cats(cattery_id):
    return ["Whiskers", "Chonk"]

@snapshot()
def categorize_cats(cats):
    return {"chonky": cats}

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

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

detective_snapshot-0.1.4.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

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

detective_snapshot-0.1.4-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file detective_snapshot-0.1.4.tar.gz.

File metadata

  • Download URL: detective_snapshot-0.1.4.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for detective_snapshot-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a6b4a68e2bc002bf4ad0503f49fe015e1aa1ab1ce9a61b8eac3c70404389b39e
MD5 8fab28693beab51dacd30f7ee6bda335
BLAKE2b-256 98bf2b525fe48f452f49dad660715c9d3f99e7dc95aaf96a5f71188e42018c23

See more details on using hashes here.

File details

Details for the file detective_snapshot-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for detective_snapshot-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8686938da8e7beeff3c44ff1e7b987483790567983c452020558b310b5cd442a
MD5 37585a937c27d3a949d443331a118adf
BLAKE2b-256 1f4f2cfbfbde5152efb73f10915654ca8209160b3b5c9afe6681bcd8b25097d8

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