GithubHelp home page GithubHelp logo

gabrielcnr / python-ls Goto Github PK

View Code? Open in Web Editor NEW
45.0 4.0 7.0 22 KB

Think about Python's dir builtin with recursive search capabilities

License: MIT License

Python 100.00%
python inspection dir devtools

python-ls's Introduction

python-ls

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

Build Status PyPI PythonVersions

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!

python-ls's People

Contributors

gabrielcnr avatar hitenjadeja avatar nicoddemus avatar pyjay avatar swfiua avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

python-ls's Issues

Exceptions on search term without depth, and when processing re module

As shown in the following examples:

>>> ls(os, 'blah')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/skeledrew/.pyenv/versions/3.6.1/envs/general/lib/python3.6/site-packages/python_ls/_ls.py", line 32, in ls
    dunder=dunder, under=under):
  File "/home/skeledrew/.pyenv/versions/3.6.1/envs/general/lib/python3.6/site-packages/python_ls/_ls.py", line 53, in iter_ls
    if current_depth <= depth:
TypeError: '<=' not supported between instances of 'int' and 'NoneType'
'<=' not supported between instances of 'int' and 'NoneType'

and

>>> ls(re)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/skeledrew/.pyenv/versions/3.6.1/envs/general/lib/python3.6/site-packages/python_ls/_ls.py", line 37, in ls
    size = len(value)
TypeError: object of type 'RegexFlag' has no len()
object of type 'RegexFlag' has no len()

Unable to pip install

Great project idea! I love developer tooling that makes life easier.

Having some errors trying to pip install the module.

Collecting python-ls
  Could not find a version that satisfies the requirement python-ls (from versions: )
No matching distribution found for python-ls

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.