Skip to main content

Minimalist debug tool for Python with automatic cleaner

Project description

Siren

Minimal Python debug helper with automatic cleanup.

A tiny debugging utility for Python that prints variables with file/line context, traces function calls, measures execution time, and safely removes debug calls from your code.

PyPI - Version PyPI - Python Version License: MIT


Install

pip install siren-debug

The package also installs a cleanup command:

siren-clean

Quick Start

from siren import siren

x = 10
user = {"name": "Alex", "items": [1, 2, 3]}

siren(x)
siren(user)

Example output:

[🧜‍ SIREN core.py:10] x = 10
[🧜‍ SIREN core.py:11] user = {'name': 'Alex', 'items': [1, 2, 3]}

Siren automatically uses pprint for complex objects.


Features

  • Works with Python 3+
  • Zero external dependencies
  • Prints values with file and line number
  • Uses pprint automatically for complex data
  • Optional execution timer with timeit=True
  • Function tracing with @siren.trace
  • Removes siren(...) calls automatically
  • Safe cleanup using Python tokenize
  • Works in scripts, CLI tools, Django, Flask, FastAPI, and more
  • Colored output with emoji for easy visual scanning

Usage

Import the helper and call it with one or more values:

from siren import siren

siren(x, data, user)

You can also add a custom label:

siren(value, label="BEFORE SAVE")

Timer

Enable timing for a single call:

siren(x, timeit=True)

Output example:

[🧜‍ SIREN core.py:10] x = 10
[🧜‍ SIREN TIME] 0.000123s

Function tracing

Use the trace decorator to log calls, arguments, return values, execution time, and exceptions:

from siren import trace

@siren.trace
def add(a, b):
    return a + b

add(2, 3)

Example output:

[🧜‍ SIREN core.py:10] Calling add(a=2, b=3)
[🧜‍ SIREN core.py:11] Returned from add -> 5 [int] (0.000123s)

Configuration options:

  • timeit=True – Display execution time (default: True)
  • show_args=True – Display function arguments (default: True)
  • show_return=True – Display return value (default: True)
  • show_type=True – Display return type in brackets (default: True)

Example with options:

@siren.trace(timeit=True, show_args=False, show_type=False)
def multiply(a, b):
    return a * b

The decorator also captures and logs exceptions:

@siren.trace
def divide(a, b):
    return a / b

divide(5, 0)  # Logs exception before raising

Cleaning debug calls

The package installs a command named siren-clean. Run it in a project folder to remove all siren(...) calls and associated import lines:

siren-clean

Example:

Before:

from siren import siren
siren(x)
print("hello")
siren(data)

After:

print("hello")

The cleaner preserves comments and string literals.


Advanced Features

Quiet mode (suppress output for specific calls)

from siren import siren

siren(x, quiet=True)  # Won't print anything, but returns the value

Global quiet mode

from siren import siren

siren.set_quiet(True)   # Disable all siren output
siren.set_quiet(False)  # Enable again

Logging to file

from siren import siren

siren.set_logfile("debug.log")
siren(x)  # Prints to stdout AND writes to debug.log

Conditional logging

Only print when specific conditions are met:

from siren import siren

# Only print if value equals
siren(x, if_equals=5)

# Only print if length > N
siren(items, if_len_gt=100)

# Only print if length < N
siren(items, if_len_lt=5)

# Only print if truthy
siren(result, if_true=True)

# Only print if falsy
siren(error, if_false=True)

Diff objects

Compare two objects and display differences:

from siren import siren

before = {"name": "Alice", "age": 30}
after = {"name": "Alice", "age": 31, "city": "NYC"}

siren.diff(before, after)

Output:

[🧜‍ SIREN test.py:10] DIFF
[🧜‍ SIREN test.py:11] [~] age: 30 → 31 (changed)
[🧜‍ SIREN test.py:12] [+] city: NYC (new)

Works with dicts, lists, tuples, and any comparable objects.

Interactive breakpoint

Pause execution and inspect local variables:

from siren import siren

x = 42
data = {"items": [1, 2, 3]}

siren.breakpoint()  # Pauses and displays all locals
# Press Ctrl+C to continue
# Type 'd' to enter debugger (pdb)

Output:

[🧜‍ SIREN test.py:10] BREAKPOINT
[🧜‍ SIREN test.py:11] === BREAKPOINT ===
[🧜‍ SIREN test.py:12] Locals:
[🧜‍ SIREN test.py:13]   x = 42
[🧜‍ SIREN test.py:14]   data = {'items': [1, 2, 3]}

Check configuration

from siren import siren

config = siren.get_config()
print(config)  # {"quiet": False, "logfile": None, "enabled": True}

Examples with Frameworks

Django

from django.http import JsonResponse
from siren import siren

def my_view(request):
    user_data = request.GET.dict()
    siren(user_data, label="REQUEST_PARAMS")
    
    result = process_data(user_data)
    siren(result)
    
    return JsonResponse(result)

Flask

from flask import Flask, request
from siren import siren, trace

app = Flask(__name__)

@app.route("/api/users")
def get_users():
    query = request.args.get("q")
    siren(query, label="SEARCH_QUERY")
    
    users = search_users(query)
    return {"users": users}

@siren.trace
def search_users(query):
    # Function entry/exit will be logged automatically
    return [{"id": 1, "name": "Alice"}]

FastAPI

from fastapi import FastAPI
from siren import siren, trace

app = FastAPI()

@app.get("/items/{item_id}")
async def get_item(item_id: int, q: str = None):
    siren({"item_id": item_id, "q": q}, label="QUERY_PARAMS")
    
    item = await fetch_item(item_id)
    return item

@siren.trace(timeit=True)
async def fetch_item(item_id: int):
    # Execution time and arguments will be logged
    return {"id": item_id, "name": "Item"}

Why use Siren?

Debug prints are easy to add, but hard to remove later. Siren gives you a fast debug workflow and a safe cleanup step so your temporary debug code does not stay in production.


Project


License

MIT

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

siren_debug-0.3.0.tar.gz (3.4 MB view details)

Uploaded Source

Built Distribution

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

siren_debug-0.3.0-py3-none-any.whl (4.1 MB view details)

Uploaded Python 3

File details

Details for the file siren_debug-0.3.0.tar.gz.

File metadata

  • Download URL: siren_debug-0.3.0.tar.gz
  • Upload date:
  • Size: 3.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for siren_debug-0.3.0.tar.gz
Algorithm Hash digest
SHA256 25c4337b468983901bbb3872f473fec01d05ab0164805e6f74698a2059021b70
MD5 91dee2dd3a01af2f8d2346d4d6959e60
BLAKE2b-256 dc3197b43428ef2b6ced07c63b1bf260b261b86cbca48b17e501c8c7e5abfbbe

See more details on using hashes here.

File details

Details for the file siren_debug-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: siren_debug-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for siren_debug-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83bb870aae40bc1f7db5a166cccaef6a018d0fd3c523c40596ab5dbb3c594b3e
MD5 75a6f3f2197151c661ef45d23bd17776
BLAKE2b-256 4af2167e8b8d8d2f3811775beaee28456da9e3dbf6630f7476250752a6c3b8df

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