Skip to content
Closed
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
29 changes: 23 additions & 6 deletions pdir/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
class PrettyDir(object):
"""Class that provides pretty dir and search API."""

def __init__(self, obj=None):
def __init__(self, obj=None, term=None, case_sensitive=False):
self.obj = obj
self.attrs = []
if obj is None:
source = _getframe(1).f_locals
else:
source = {name: self.__getattr(name) for name in dir(obj)}
self.__inspect_category(source)
if term:
self.attrs = self._search_attrs(self, term=term, case_sensitive=case_sensitive)

def __repr__(self):
output = []
Expand All @@ -46,18 +48,33 @@ def search(self, term, case_sensitive=False):
(case insensitive)

Return:
A PrettyDir object with matched names.
A new PrettyDir object with matched names.
"""
return PrettyDir(self.obj, term=term, case_sensitive=case_sensitive)

s = search

def _search_attrs(self, term, case_sensitive=False):
"""Search for names that match some pattern.

Args:
term: String used to match names. A name is returned if it matches
the whole search term.
case_sensitive: Boolean to match case or not, default is False
(case insensitive)

Return:
A list of matched PrettyAttribute objects.
"""
if case_sensitive:
self.attrs = [attr for attr in self.attrs if term in attr.name]
return [attr for attr in self.attrs if term in attr.name]
else:
term = term.lower()
self.attrs = [
return [
attr for attr in self.attrs if term in attr.name.lower()
]
return self

s = search
s = search

def __getattr(self, name):
"""A wrapper around getattr(), handling some exceptions."""
Expand Down