Skip to content

Instantly share code, notes, and snippets.

@brandonwillard
Last active April 29, 2020 00:12
Show Gist options
  • Save brandonwillard/dd7b28a2a7f3d912827993b8288a045a to your computer and use it in GitHub Desktop.
Save brandonwillard/dd7b28a2a7f3d912827993b8288a045a to your computer and use it in GitHub Desktop.
A `.pdbrc` that enables IPython's pretty printing in `[i]pdb`
#
# Make `pp` use IPython's pretty printer, instead of the standard `pprint` module.
#
# Sources: https://nedbatchelder.com/blog/200704/my_pdbrc.html
#
# XXX: This .pdbrc is only valid for Python >= 3.4
#
import pdb
import inspect as __inspect
from pprint import pprint as __pprint
from contextlib import suppress as __suppress
# Use IPython's pretty printing within PDB
with __suppress(ImportError): from IPython.lib.pretty import pprint as __pprint
pdb.__pprint = __pprint
alias pp import pdb; pdb.__pprint(%*)
# Print a dictionary sorted by key.
alias pd pp {k: v for k, v in sorted(dict(%*).items(), key=lambda i: i[0])}
# Print the member variables of a thing.
alias pi pd %*.__dict__
alias pii pd dict(__inspect.getmembers(%1))
# Print the member variables of self.
alias ps pi self
# Print the locals.
alias pl pd locals()
alias pg pd globals()
# Next and list, and step and list.
alias nll n;;ll
alias nl n;;l
alias sl s;;l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment