Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable searching with regular expressions #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Sometimes when you're developing using Python's interactive shell, or IPython, o

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.
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 or a regular expression (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.

Expand Down
6 changes: 5 additions & 1 deletion python_ls/_ls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from collections import Container

try:
Expand All @@ -24,6 +25,9 @@ def ls(obj, attr=None, depth=None, dunder=False, under=True):
if depth is None and attr is None:
depth = 1

if attr and isinstance(attr, str):
attr = re.compile(attr)

for attr, value in iter_ls(obj, attr=attr, depth=depth,
dunder=dunder, under=under):
size = ''
Expand Down Expand Up @@ -65,7 +69,7 @@ def exclude_unders(a):
return not a.startswith('_')

def attr_filter_callback(a):
return attr in a
return not not attr.search(a) or attr.pattern in a

if attr:
callbacks.append(attr_filter_callback)
Expand Down