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(.

Unique Selling Point

  • Print absolutely ANYTHING in human friendly way.
  • Wherever print() works, dbg() works.
  • You can use this library by just Ctrl+R to replace print( with dbg(.
  • When dbg() is called, the output also includes the file name, line number, and other key info for context.

Example Usage

pi = 3.14
ultimate_answer = 42
flag = True
stock_price = [100, 99, 101, 1]
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(
    pi,
    1 + 1,
    sorted(stock_price),
    "This string contains (, ' and ,",
    ultimate_answer,
    flag,  # You can leave a comment here as well, dbg() won't show this comment.
    stock_price,
    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.
double_linked_list = DoubleLinkedList.create(2)
dbg(double_linked_list)

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

# For even more complex structures, it works as well.
stack = Stack()
stack.push(double_linked_list)
stack.push(double_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)
dbg({"my_phones": [phone]})

# If you are extremely bored.
infinite_list = []
infinite_list.append(infinite_list)
dbg(infinite_list)

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

import numpy as np

# This library can also be used with your favorite data science libraries if you enabled our optional features.
ndarray = np.array([[1, 2, 3], [4, 5, 6]])
dbg(ndarray)

# And yes, even deeply nested ndarray is possible.
stack = Stack()
stack.push({"dict_key": ndarray})
dbg(stack)

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

[examples/example.py:76:5] pi = 3.14
[examples/example.py:76:5] 1 + 1 = 2
[examples/example.py:76:5] sorted(stock_price) = [
    1,
    99,
    100,
    101
]
[examples/example.py:76:5] "This string contains (, ' and ," = "This string contains (, ' and ,"
[examples/example.py:76:5] ultimate_answer = 42
[examples/example.py:76:5] flag = True
[examples/example.py:76:5] stock_price = [
    100,
    99,
    101,
    1
]
[examples/example.py:76:5] fruits = {
    'peach',
    'watermelon',
    'apple'
}
[examples/example.py:76:5] country_to_capital_cities = {
    China: 'Beijing',
    United Kingdom: 'London',
    Liyue: 'Liyue Harbor'
}
[examples/example.py:89:5] country_to_capital_cities = {
    China: 'Beijing',
    United Kingdom: 'London',
    Liyue: 'Liyue Harbor'
}
[examples/example.py:92:5] 1 + 1 = 2
[examples/example.py:96:5] double_linked_list = DoubleLinkedList {
    head: Node {
        val: 0
        next: Node {
            val: 1
            next: None
            prev: CYCLIC REFERENCE
        }
        prev: None
    }
    tail: Node {
        val: 1
        next: None
        prev: Node {
            val: 0
            next: CYCLIC REFERENCE
            prev: None
        }
    }
}
[examples/example.py:99:5] [double_linked_list, double_linked_list] = [
    DoubleLinkedList {
        head: Node {
            val: 0
            next: Node {
                val: 1
                next: None
                prev: CYCLIC REFERENCE
            }
            prev: None
        }
        tail: Node {
            val: 1
            next: None
            prev: Node {
                val: 0
                next: CYCLIC REFERENCE
                prev: None
            }
        }
    },
    DoubleLinkedList {
        head: Node {
            val: 0
            next: Node {
                val: 1
                next: None
                prev: CYCLIC REFERENCE
            }
            prev: None
        }
        tail: Node {
            val: 1
            next: None
            prev: Node {
                val: 0
                next: CYCLIC REFERENCE
                prev: None
            }
        }
    }
]
[examples/example.py:99:5] (double_linked_list, double_linked_list) = (
    DoubleLinkedList {
        head: Node {
            val: 0
            next: Node {
                val: 1
                next: None
                prev: CYCLIC REFERENCE
            }
            prev: None
        }
        tail: Node {
            val: 1
            next: None
            prev: Node {
                val: 0
                next: CYCLIC REFERENCE
                prev: None
            }
        }
    },
    DoubleLinkedList {
        head: Node {
            val: 0
            next: Node {
                val: 1
                next: None
                prev: CYCLIC REFERENCE
            }
            prev: None
        }
        tail: Node {
            val: 1
            next: None
            prev: Node {
                val: 0
                next: CYCLIC REFERENCE
                prev: None
            }
        }
    }
)
[examples/example.py:99:5] {'a': 1, 'b': [double_linked_list]} = {
    a: 1,
    b: [
        DoubleLinkedList {
            head: Node {
                val: 0
                next: Node {
                    val: 1
                    next: None
                    prev: CYCLIC REFERENCE
                }
                prev: None
            }
            tail: Node {
                val: 1
                next: None
                prev: Node {
                    val: 0
                    next: CYCLIC REFERENCE
                    prev: None
                }
            }
        }
    ]
}
[examples/example.py:99:5] [1, 2, 3, 4] = [
    1,
    2,
    3,
    4
]
[examples/example.py:115:5] stack = Stack {
    data: [
        DoubleLinkedList {
            head: Node {
                val: 0
                next: Node {
                    val: 1
                    next: None
                    prev: CYCLIC REFERENCE
                }
                prev: None
            }
            tail: Node {
                val: 1
                next: None
                prev: Node {
                    val: 0
                    next: CYCLIC REFERENCE
                    prev: None
                }
            }
        },
        DoubleLinkedList {
            head: Node {
                val: 0
                next: Node {
                    val: 1
                    next: None
                    prev: CYCLIC REFERENCE
                }
                prev: None
            }
            tail: Node {
                val: 1
                next: None
                prev: Node {
                    val: 0
                    next: CYCLIC REFERENCE
                    prev: None
                }
            }
        }
    ]
}
[examples/example.py:117:5] 'What if my input is a string?' = 'What if my input is a string?'
[examples/example.py:121:5] phone = Phone:
    Color: white
    Brand: Apple
    Price: 1099
[examples/example.py:122:5] {'my_phones': [phone]} = {
    my_phones: [
        Phone:
            Color: white
            Brand: Apple
            Price: 1099
    ]
}
[examples/example.py:127:5] infinite_list = [
    [...]
]
[examples/example.py:130:5]
[examples/example.py:136:5] ndarray = 
array([[1, 2, 3],
       [4, 5, 6]])
[examples/example.py:141:5] stack = Stack {
    data: [
        {
            dict_key: 
                array([[1, 2, 3],
                       [4, 5, 6]])
        }
    ]
}

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.5.tar.gz (21.1 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.5-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: crab_dbg-0.1.5.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.14 {"installer":{"name":"uv","version":"0.9.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for crab_dbg-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2ca1adc4f14d04088bc25f86c59af9880606890727974eef823580228838bf08
MD5 75d10342be66f8f1fc5949ae3486034d
BLAKE2b-256 a0d7900741af46e16dff404bac892bf2532c8a94c6d15355e346176e26783158

See more details on using hashes here.

File details

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

File metadata

  • Download URL: crab_dbg-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.14 {"installer":{"name":"uv","version":"0.9.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for crab_dbg-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 97954311fe2fc339f957b36400614369ff29f0dd1f96f3c55c9ac02cb3e0bdac
MD5 764f52402dc69943d2a199c7197f57cb
BLAKE2b-256 e875478e9e7fe7b2ea80a6e5392d29a9512205f44d549e2a4cb62bc090505688

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