From 57d6dfa087898fd0fdd664a8e9b6144f47d2bfab Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Tue, 21 Aug 2018 13:49:14 -0500 Subject: [PATCH] Enable searching with regular expressions --- README.md | 2 +- python_ls/_ls.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 34e060a..d80fac5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/python_ls/_ls.py b/python_ls/_ls.py index 614e422..21fc93c 100644 --- a/python_ls/_ls.py +++ b/python_ls/_ls.py @@ -1,3 +1,4 @@ +import re from collections import Container try: @@ -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 = '' @@ -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)