From ae1f76b58c4486517951cc4721472b7d0287cef9 Mon Sep 17 00:00:00 2001 From: topper-123 Date: Sun, 12 Mar 2017 18:17:21 +0000 Subject: [PATCH 1/5] Let .search return a copy of self .search currenly modifies ``self``. This proposes creating a copy of self and modifying and returning that. --- pdir/api.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pdir/api.py b/pdir/api.py index 990b9b4..4087795 100644 --- a/pdir/api.py +++ b/pdir/api.py @@ -46,16 +46,18 @@ def search(self, term, case_sensitive=False): (case insensitive) Return: - A PrettyDir object with matched names. + A new PrettyDir object with matched names. """ + import copy + new_pretty_dir = copy.copy(self) if case_sensitive: - self.attrs = [attr for attr in self.attrs if term in attr.name] + new_pretty_dir.attrs = [attr for attr in self.attrs if term in attr.name] else: term = term.lower() - self.attrs = [ + new_pretty_dir.attrs = [ attr for attr in self.attrs if term in attr.name.lower() ] - return self + return new new_pretty_dir s = search From 953c798a9dcba7bb898e977b78980fcc5c16a4be Mon Sep 17 00:00:00 2001 From: topper-123 Date: Sun, 12 Mar 2017 18:20:07 +0000 Subject: [PATCH 2/5] correct typo --- pdir/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdir/api.py b/pdir/api.py index 4087795..bb04e11 100644 --- a/pdir/api.py +++ b/pdir/api.py @@ -57,7 +57,7 @@ def search(self, term, case_sensitive=False): new_pretty_dir.attrs = [ attr for attr in self.attrs if term in attr.name.lower() ] - return new new_pretty_dir + return new_pretty_dir s = search From 206e3e6958d104fd130614f149103f7ec57003d1 Mon Sep 17 00:00:00 2001 From: topper-123 Date: Mon, 13 Mar 2017 07:34:23 +0000 Subject: [PATCH 3/5] New proposal for .search (incl. term in __init__) --- pdir/api.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/pdir/api.py b/pdir/api.py index bb04e11..4fad125 100644 --- a/pdir/api.py +++ b/pdir/api.py @@ -13,7 +13,7 @@ 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=case_sensitive): self.obj = obj self.attrs = [] if obj is None: @@ -21,6 +21,7 @@ def __init__(self, obj=None): else: source = {name: self.__getattr(name) for name in dir(obj)} self.__inspect_category(source) + self.attrs = self._search_attrs(self, term=term, case_sensitive=case_sensitive) def __repr__(self): output = [] @@ -48,18 +49,31 @@ def search(self, term, case_sensitive=False): Return: A new PrettyDir object with matched names. """ - import copy - new_pretty_dir = copy.copy(self) + 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: - new_pretty_dir.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() - new_pretty_dir.attrs = [ + return [ attr for attr in self.attrs if term in attr.name.lower() ] - return new_pretty_dir - s = search + s = search def __getattr(self, name): """A wrapper around getattr(), handling some exceptions.""" From 2a58f7a5e8637bf01be9783acb730e598b8ed7ac Mon Sep 17 00:00:00 2001 From: topper-123 Date: Mon, 13 Mar 2017 07:47:16 +0000 Subject: [PATCH 4/5] Corrected bug --- pdir/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdir/api.py b/pdir/api.py index 4fad125..29e4330 100644 --- a/pdir/api.py +++ b/pdir/api.py @@ -13,7 +13,7 @@ class PrettyDir(object): """Class that provides pretty dir and search API.""" - def __init__(self, obj=None, term=None, case_sensitive=case_sensitive): + def __init__(self, obj=None, term=None, case_sensitive=False): self.obj = obj self.attrs = [] if obj is None: From 19234326c9bc56da556fa11c4e75bec08315dd07 Mon Sep 17 00:00:00 2001 From: topper-123 Date: Mon, 13 Mar 2017 08:29:43 +0000 Subject: [PATCH 5/5] Another bug... --- pdir/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pdir/api.py b/pdir/api.py index 29e4330..9114ff4 100644 --- a/pdir/api.py +++ b/pdir/api.py @@ -21,7 +21,8 @@ def __init__(self, obj=None, term=None, case_sensitive=False): else: source = {name: self.__getattr(name) for name in dir(obj)} self.__inspect_category(source) - self.attrs = self._search_attrs(self, term=term, case_sensitive=case_sensitive) + if term: + self.attrs = self._search_attrs(self, term=term, case_sensitive=case_sensitive) def __repr__(self): output = []