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.3.tar.gz (17.7 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.3-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: detective_snapshot-0.1.3.tar.gz
  • Upload date:
  • Size: 17.7 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.3.tar.gz
Algorithm Hash digest
SHA256 f955221fca8542675881f19590fe6a809ba1e12ccec0ad28d48168790397a4f8
MD5 2f7e50fd0969ab4d40cdf6f9b1c3ae98
BLAKE2b-256 be0da9b40bf8b9ec845219b67746d291e6f0104507aeeb68847422a2b8dc7a03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for detective_snapshot-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 96a2fd0484f2a8a52e102d4e8780e0b201251e34fc8cf9c8b9cb6d565944dae0
MD5 cce1d95e8585839121bfc3316cf79a69
BLAKE2b-256 8129b7abd51dc4fc0112dafc78a16f8d49c63247b9e046cf6aaee686cfe47a47

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