Skip to main content

A better replacement for Python dir() builtin function

Project description

python-ls

A better replacement for Python's built-in dir function with searching in mind.

Sometimes when you're developing using Python's interactive shell, or IPython, or working with a Jupyter Notebook or even debugging using pdb, you find yourself having to navigate through complex object structures. If you're not entirely familiar with the class in hand you usually have two options: resort to the documentation of the libraries and projects you're working with, or put the explorer's hat on and go down a trial-and-error route, using Python's builtin dir function to see which attributes and functions an object may have and then take a good guess on the next object you will be inspecting.

There must be a better way, right?

Well, now yes, you have ls to help you with that task. If you have rough idea of what you're looking for, you can search for that "thing" by name (fingers crossed here: hopefully the developers of the APIs/libraries you're dealing with were careful enough about their naming conventions). Even if (often) your target object may be a few levels deep down the object structure.

ls goes recursively through your object structure, it tries to visit attributes searching for the name you're looking for. It also considers dictionary keys if it stumbles across dictionaries, and in the end it prints out the matching occurrences and tells you their types too.

>>> ls(ls, 'code', depth=3)
func_code                                                                             code
func_code.co_code                                                                      str    200
func_code.co_code.decode()                                      builtin_function_or_method
func_code.co_code.encode()                                      builtin_function_or_method
func_code.co_filename.decode()                                  builtin_function_or_method
func_code.co_filename.encode()                                  builtin_function_or_method
func_code.co_lnotab.decode()                                    builtin_function_or_method
func_code.co_lnotab.encode()                                    builtin_function_or_method
func_code.co_name.decode()                                      builtin_function_or_method
func_code.co_name.encode()                                      builtin_function_or_method
func_globals['xdir'].func_code                                                        code
func_globals['iter_ls'].func_code                                                     code

Install

pip install python-ls

ls available as builtin

python-ls will inject the ls function in the __builtin__ namespace at installation step.

It does this by using a .pth file which simply performs that injection.

Security

If you're running this against objects that have properties, lazy attributes or any other dynamic code, keep in mind that ls will try to fetch the value of the property/lazy attribute using getattr(). This will cause the body of the function to be executed, so you can imagine already the potential hazard here, right?

TODO: make a default unsafe=False kwarg and then only visit properties if unsafe is explicitly set to True.

About the name ls

Python has dir as a builtin. The equivalent of that command-line command in GNU/Linux world is ls. We had considered calling it xdir, which by the way is a function that works like dir() by returning a list of occurrences to you.

Contribute!

Please send your issues, bug reports and, even more welcome, your Pull Requests ;-)

Enjoy!

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

python-ls-0.1.5.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

python_ls-0.1.5-py2.py3-none-any.whl (5.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python-ls-0.1.5.tar.gz.

File metadata

  • Download URL: python-ls-0.1.5.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

File hashes

Hashes for python-ls-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0df7b757db041e2cd50bebf2f3bb8403d72b0dc4d0fa36beeee216eb6231d1a2
MD5 0ac1a6f6be8ea297babbe27dd4c54bf9
BLAKE2b-256 4579301ce42fdc7ca19802c1a307d726cedf7bf42e74915397d0919eea02d33c

See more details on using hashes here.

Provenance

File details

Details for the file python_ls-0.1.5-py2.py3-none-any.whl.

File metadata

  • Download URL: python_ls-0.1.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

File hashes

Hashes for python_ls-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 321f2003fa8caa892cbc45fcbce4be8d1531fa5df8c7389fa1679ae56451deea
MD5 7bd6a94c01a44bb3c6d16f486b46a6a3
BLAKE2b-256 ffe6741dc18e7eef8f3490499fe3f27dfe3035e5da38803841676864d8772cc4

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page