Skip to main content

Pdb with Rich library.

Project description

pdbr

PyPI version Python Version pre-commit.ci status

pdbr is intended to make the PDB results more colorful. it uses Rich library to carry out that.

Installing

Install with pip or your favorite PyPi package manager.

pip install pdbr

Breakpoint

In order to use breakpoint(), set PYTHONBREAKPOINT with "pdbr.set_trace"

import os

os.environ["PYTHONBREAKPOINT"] = "pdbr.set_trace"

or just import pdbr

import pdbr

New commands

(ic)ecream

🍦 Icecream print.

(i)nspect / inspectall | ia

rich.inspect

search | src

Search a phrase in the current frame. In order to repeat the last one, type / character as arg.

sql

Display value in sql format.

It can be used for Django model queries as follows.

>>> sql str(Users.objects.all().query)

(syn)tax

[ val,lexer ] Display lexer.

(v)ars

Get the local variables list as table.

varstree | vt

Get the local variables list as tree.

Config

Config is specified in setup.cfg and can be local or global. Local config (current working directory) has precedence over global (default) one. Global config must be located in $XDG_CONFIG_HOME/pdbr/ directory.

Style

In order to use Rich's traceback, style, and theme:

[pdbr]
style = yellow
use_traceback = True
theme = friendly

History

store_history setting is used to keep and reload history, even the prompt is closed and opened again:

[pdbr]
...
store_history=.pdbr_history

Celery

In order to use Celery remote debugger with pdbr, use celery_set_trace as below sample. For more information see the Celery user guide.

from celery import Celery

app = Celery('tasks', broker='pyamqp://guest@localhost//')

@app.task
def add(x, y):

    import pdbr; pdbr.celery_set_trace()

    return x + y

Telnet

Instead of using telnet or nc, in terms of using pdbr style, pdbr_telnet command can be used.

Also in order to activate history and be able to use arrow keys, install and use rlwrap package.

rlwrap -H '~/.pdbr_history' pdbr_telnet localhost 6899

IPython

pdbr integrates with IPython.

This makes %magics available, for example:

(Pdbr) %timeit range(100)
104 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

To enable IPython features, install it separately, or like below:

pip install pdbr[ipython]

pytest

In order to use pdbr with pytest --pdb flag, add addopts setting in your pytest.ini.

[pytest]
addopts: --pdbcls=pdbr:RichPdb

Context Decorator

pdbr_context and apdbr_context (asyncio corresponding) can be used as with statement or decorator. It calls post_mortem if traceback is not none.

from pdbr import apdbr_context, pdbr_context

@pdbr_context()
def foo():
    ...

def bar():
    with pdbr_context():
        ...


@apdbr_context()
async def foo():
    ...

async def bar():
    async with apdbr_context():
        ...

Django DiscoverRunner

To being activated the pdb in Django test, change TEST_RUNNER like below. Unlike Django (since you are not allowed to use for smaller versions than 3), pdbr runner can be used for version 1.8 and subsequent versions.

TEST_RUNNER = "pdbr.runner.PdbrDiscoverRunner"

Middlewares

Starlette

from fastapi import FastAPI
from pdbr.middlewares.starlette import PdbrMiddleware

app = FastAPI()

app.add_middleware(PdbrMiddleware, debug=True)


@app.get("/")
async def main():
    1 / 0
    return {"message": "Hello World"}

Django

In order to catch the problematic codes with post mortem, place the middleware class.

MIDDLEWARE = (
    ...
    "pdbr.middlewares.django.PdbrMiddleware",
)

Shell

Running pdbr command in terminal starts an IPython terminal app instance. Unlike default TerminalInteractiveShell, the new shell uses pdbr as debugger class instead of ipdb.

%debug magic sample

As a Script

If pdbr command is used with an argument, it is invoked as a script and debugger-commands can be used with it.

# equivalent code: `python -m pdbr -c 'b 5' my_test.py`
pdbr -c 'b 5' my_test.py

>>> Breakpoint 1 at /my_test.py:5
> /my_test.py(3)<module>()
      1
      2
----> 3 def test():
      4         foo = "foo"
1     5         bar = "bar"

(Pdbr)

Terminal

Django shell sample

Vscode user snippet

To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select python.json.

Place the below snippet in json file for pdbr.

{
  ...
  "pdbr": {
        "prefix": "pdbr",
        "body": "import pdbr; pdbr.set_trace()",
        "description": "Code snippet for pdbr debug"
    },
}

For Celery debug.

{
  ...
  "rdbr": {
        "prefix": "rdbr",
        "body": "import pdbr; pdbr.celery_set_trace()",
        "description": "Code snippet for Celery pdbr debug"
    },
}

Samples

Traceback

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

pdbr-0.7.9.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

pdbr-0.7.9-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file pdbr-0.7.9.tar.gz.

File metadata

  • Download URL: pdbr-0.7.9.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.7.15 Linux/5.15.0-1031-azure

File hashes

Hashes for pdbr-0.7.9.tar.gz
Algorithm Hash digest
SHA256 24c9d37bc44ddaff402b3686af5dce0db3466233892882fb7a719463c6caab84
MD5 e2e2ab96726e9e71b43a068458c0eb56
BLAKE2b-256 4a8d17cf789ef584eb908c817440ea11962b9684dfd991914533a4b1e8cb82bc

See more details on using hashes here.

File details

Details for the file pdbr-0.7.9-py3-none-any.whl.

File metadata

  • Download URL: pdbr-0.7.9-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.7.15 Linux/5.15.0-1031-azure

File hashes

Hashes for pdbr-0.7.9-py3-none-any.whl
Algorithm Hash digest
SHA256 ff95aee8aac963b16c18408563939a8138c1303b5215d5e61055f57387be62e5
MD5 c5aa944bb1375c8053418b775a9cb5ce
BLAKE2b-256 5c83f28e258ea587fe4c5d9254c96e1f4d6ec07a4f66950ee45fd34ed7384976

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