Skip to main content

A pretty-printed version of dir(obj) for more comfortable debugging

Project description

Overview

How often have you wished for a more well organized output from Python's built-in dir function when looking through a deeply nested pydantic model after seeing page due to tens of private methods? Running dir on a pandas DataFrame produces a list of 443 entries!

The goal of this library is to create a more useful debugging tool than the built-in function dir(<class>). The issues with the built-in dir(<class>) addressed by this library are:

  1. Inclusion of dunder methods that are very rarely useful
  2. No differentiation between attributes/methods
  3. No docstring display
  4. No grouping of similar functionality together, only alphabetical sorting
  5. No way to jump to the source code of the

This library takes the output of dir and runs the following steps:

  1. Groups the attributes and methods by the class they are defined by
    • Identifies the source code location of the class, allowing you to quickly jump to them for a deeper dive in IDEs like VS Code.
    • Prints the docstring summary of the class.
  2. Identifies if it is a dunder method, non-dunder method, or attribute
    • Within non-dunder methods, adds a "ᶜ" or "ˢ" to indicate if it is a classmethod or staticmethod rather than a standard instance method.
  3. Pulls the summary of the docstring for each attribute/method, if it exists
    • For attributes, there is no __doc__ attribute, but we follow the convention of PEP-258 and most autocompletion tools of the next literal expression if it exists
  4. Colorizes the output to visually differentiate the classes, attributes, methods, and dunder methods

Limitations

Currently, this library only works with classes. For all other entities - functions, modules, etc - it falls back to the built-in dir implementation.

Installation

You can install the library using pip:

pip install pretty-dir

Auto-loading in PDB (Breakpoint)

PDB is the built-in Python debugger that you can invoke with the breakpoint() function (or import pdb; pdb.set_trace() in Python versions before 3.7). To make ppdir automatically available in every PDB session, you can include it in a PDB configuration file.

To make and add ppdir to your global PDB configuration, create a file named ~/.pdbrc if it doesn't already exist, and add the following lines to it:

from ppdir import ppdir, defaults
defaults(include_docs=True)  # remember to set your preferred defaults!

Now ppdir will be automatically available whenever you call breakpoint() in your code.

Basic Usage Example

from ppdir import ppdir

class BaseClass:
    base: int

class MyClass(BaseClass):
    a: int
    "example attr docstring"
    b: str

    def my_method(self):
        """This is my method."""
        pass

ppdir(MyClass)

Demo

Running the code in demo.py, you should see the difference between the built-in dir and ppdir like the following images:

Before:

before

After:

after

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

pretty_dir-0.9.13.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

pretty_dir-0.9.13-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pretty_dir-0.9.13.tar.gz.

File metadata

  • Download URL: pretty_dir-0.9.13.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pretty_dir-0.9.13.tar.gz
Algorithm Hash digest
SHA256 e54a2999289229aa6205b3276efe29a5da632680e28408840c35ea9e065f41a5
MD5 5abc0059733adb16d510bf6377ce2df1
BLAKE2b-256 be24aff09da970bf5e18bbde6498aa6f6af9ecbae2e7969b57c8b48f9ae66d2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pretty_dir-0.9.13.tar.gz:

Publisher: deploy.yml on douglassimonsen/ppdir

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pretty_dir-0.9.13-py3-none-any.whl.

File metadata

  • Download URL: pretty_dir-0.9.13-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pretty_dir-0.9.13-py3-none-any.whl
Algorithm Hash digest
SHA256 420c71115f28ab36658bda14ca695a5d10af9a0c2649f8ff87cb333bf4af24f9
MD5 75449b4f7c22b729f77e71d925973857
BLAKE2b-256 7f13f7a5b07d950de72c9cfd91edceb38ffa87b03f61557e6eb3e049492fdb6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pretty_dir-0.9.13-py3-none-any.whl:

Publisher: deploy.yml on douglassimonsen/ppdir

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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