Skip to main content

Python equivalent to Rust's `dbg!()` macro

Project description

Crab Debugger

This repo contains the Python equivalent of Rust's dbg!() macro debugging tool, which helps developers inspect variables and expressions during development. The dbg method is a perfect replacement for Python built-in function print so if that is your way of debugging, then you can switch to crab_dbg with just a Ctrl + R to replace print( with dbg(.

Features

  • Easily print values of variables and expressions using the dbg() function, eliminating the need for multiple print() statements
  • Supports primitive types (int, char, str, bool, etc.) along with basic and complex data structures (lists, arrays, NumPy arrays, PyTorch tensors, etc.)
  • When dbg() is called, the output also includes the file name, line number, and other key info for context
  • Able to process multi-line arguments and recursively inspects user-defined classes and nested objects.

Example Usage

from sys import stderr
from crab_dbg import dbg

pai = 3.14
ultimate_answer = 42
flag = True
fruits = ["apple", "peach", "watermelon"]
country_to_capital_cities = {
    "China": "Beijing",
    "United Kingdom": "London",
    "Liyue": "Liyue Harbor",
}

# You can use dbg to inspect a lot of variables.
dbg(
    pai,
    ultimate_answer,
    flag,  # You can leave a comment here as well, dbg() won't show this comment.
    fruits,
    country_to_capital_cities,
)

# Or, you can use dbg to inspect one. Note that you can pass any keyword arguments originally supported by print()
dbg(country_to_capital_cities, file=stderr)

# You can also use dbg to inspect expressions.
dbg(1 + 1)

# When used with objects, it will show all fields contained by that object.
linked_list = LinkedList.create(2)
dbg(linked_list)

# dbg() works with lists, tuples, and dictionaries.
dbg(
    [linked_list, linked_list],
    (linked_list, linked_list),
    {"a": 1, "b": linked_list},
    [
        1,
        2,
        3,
        4,
    ],
)

# For even more complex structures, it works as well.
stack = Stack()
stack.push(linked_list)
stack.push(linked_list)
dbg(stack)

dbg("What if my input is a string?")

# If your type has its own __repr__ or __str__ implementation, no worries, crab_dbg will jut use it.
phone = Phone("Apple", "white", 1099)
dbg(phone)

# It works with your favorite machine learning data structures as well, if you enabled corresponding features.
import numpy as np
import torch
numpy_array = np.zeros(shape=(2, 3))
dbg(numpy_array)

torch_tensor = torch.from_numpy(numpy_array)
dbg(torch_tensor)

# If invoked without arguments, then it will just print the filename and line number.
dbg()

The above example will generate the following output in your terminal:

[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:76:5] pai = 3.14
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:76:5] ultimate_answer = 42
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:76:5] flag = True
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:76:5] fruits = [
    'apple',
    'peach',
    'watermelon'
]
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:76:5] country_to_capital_cities = {
    China: Beijing
    United Kingdom: London
    Liyue: Liyue Harbor
}
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:85:5] country_to_capital_cities = {
    China: Beijing
    United Kingdom: London
    Liyue: Liyue Harbor
}
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:88:5] 1 + 1 = 2
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:92:5] linked_list = LinkedList {
    start: Node {
        val: 0
        next: Node {
            val: 1
            next: None
        }
    }
}
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:95:5] [linked_list, linked_list] = [
    LinkedList {
        start: Node {
            val: 0
            next: Node {
                val: 1
                next: None
            }
        }
    },
    LinkedList {
        start: Node {
            val: 0
            next: Node {
                val: 1
                next: None
            }
        }
    }
]
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:95:5] (linked_list, linked_list) = (
    LinkedList {
        start: Node {
            val: 0
            next: Node {
                val: 1
                next: None
            }
        }
    },
    LinkedList {
        start: Node {
            val: 0
            next: Node {
                val: 1
                next: None
            }
        }
    }
)
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:95:5] {"a": 1, "b": linked_list} = {
    a: 1
    b: LinkedList {
        start: Node {
            val: 0
            next: Node {
                val: 1
                next: None
            }
        }
    }
}
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:95:5] [1,2,3,4,] = [
    1,
    2,
    3,
    4
]
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:111:5] stack = Stack {
    data: [
        LinkedList {
            start: Node {
                val: 0
                next: Node {
                    val: 1
                    next: None
                }
            }
        },
        LinkedList {
            start: Node {
                val: 0
                next: Node {
                    val: 1
                    next: None
                }
            }
        }
    ]
}
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:113:5] "What if my input is a string?" = 'What if my input is a string?'
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:117:5] phone = A white phone made by Apple, official price: 1099.
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:120:5] numpy_array = 
array([[0., 0., 0.],
       [0., 0., 0.]])
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:123:5] torch_tensor = 
tensor([[0., 0., 0.],
        [0., 0., 0.]], dtype=torch.float64)
[/Users/wenqingzong/Projects/crab_dbg/examples/example.py:126:5]

For full executable code please refer to ./examples/example.py.

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

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

crab_dbg-0.1.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

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

crab_dbg-0.1.1-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file crab_dbg-0.1.1.tar.gz.

File metadata

  • Download URL: crab_dbg-0.1.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.19

File hashes

Hashes for crab_dbg-0.1.1.tar.gz
Algorithm Hash digest
SHA256 283c241a9c2cd875b490d634685b9ec11b145d5965fcc7d494edf4730bc7499a
MD5 4e22a3ef9b91fe2d6b0395d48462bd60
BLAKE2b-256 ebd92a1d8de2630f77f3f96327b621407cfc53a97362abac707fbf0da29ed212

See more details on using hashes here.

File details

Details for the file crab_dbg-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: crab_dbg-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.19

File hashes

Hashes for crab_dbg-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8243c1d56ae4d4196ade0b257965d5012156cb279ce4347edcaa73033b3191ea
MD5 511063d43f5cd6cfacda07cddb870df4
BLAKE2b-256 ad0fcc7846456d505635095a613bcd03bdcd1456e87c46e501981103d669ae25

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