Skip to main content

pdir: Selective, Pretty dir printing utlity for Python

dir() is a very useful utility function in python for developers & debuggers, which returns a list of names comprising the attributes of the given object, and of attributes reachable from it. However, a lot many times all those names can be bugging.

Case-1: Say you are really interested in knowing only the useful methods a Python “list” offers. And you do not like the unreadable, unfiltered mess resulting from using “dir(list)”.

Case-2: You really love the unix “grep” tool and you want something like that which allows you to regex filter the results of dir() in a simple call.

Then pdir should be very useful for you. I wrote this bit because something like this is very helpful to me, who quickly needs a list of only the important functions a namespace offers; likes reading clean pretty stuff; and really loves the unix “grep” utility.

Your new dir() friends-

  • rdir - Regex based dir filtering utility (alternative reference: rd)

  • pdir - Pretty, convenient dir printing with help (alternative reference: pd)

Quickstart

pdir

You can use import the pdir function from pdir after installing this package, and consider it as your new dir() friend.

An example:

>>> from pdir import pdir
>>> pdir(list)
['append',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']

Now lets compare this with what dir(list) prints:

>>> dir(list)
['__add__', '__class__', .........................................................
..................................................................................
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Some of the elements are substituted with dots (…) here.

Wasn’t the earlier one clean and better?

pdir > v0.1 also ships with a help utility (alpha)

>>> pdir(list,help=True)
-append-
L.append(object) -- append object to end-
-count-
L.count(value) -> integer -- return number of occurrences of value-
-extend-
L.extend(iterable) -- extend list by appending elements from the iterable-

pd is the alternative reference to the pdir function explained above.

>>> from pdir import pd
>>> pd(list)  # Is same as pdir used in the example before.

rdir

Having explained pdir, which should probably be the more convenient side of this package, rdir is the more powerful and useful component here. It allows you to run a regex filter to restrict the names returned by dir.

>>> from pdir import rdir
>>> rdir(list,'in')
['__contains__', '__init__', 'index', 'insert']
>>>
>>> # The above shows a list of items that match the regex='in' filter. Very similar to the unix grep utility.
>>>
>>> # To get an inverse list (NOT matching regex) pass the *inv=True* parameter. keyword "inv" is optional.
>>> rdir(list, '__',True)
>>> ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> # ^ returned a list without any '__' (underscores).

The pdir function is itself dependent on rdir in its internal usage.

rdir -> * Does not pretty print by default and returns a list which can be assigned to another object. * To enable pretty printing, you have to disable return, by providing ret = False as an additional parameter.

Notes

  • pdir is essentially a wrapper on rdir, which in itself is a wrapper around the Python dir function.

  • The code for this tool is available here.

  • Would be glad to see anyone interested in contributing.

  • You can email me or leave an issue on github suggesting improvements.

Release files for pdir 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdir 0.2.2
File Size Uploaded
pdir-0.2.2.tar.gz 4.2 kB Details

Release files / pdir-0.2.2.tar.gz

Download URL pdir-0.2.2.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
43e7fdd73807b17c58f548db04eabba8cbea4033582a42b9d3c98ceda1fbfffb
BLAKE2b-256 checksum
How to use checksums
0dab145dea39b17df17c86fbb40285e22a27c9f7b856077c4d81225a552e25e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.2 This release

1 release file

0.2.1

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page