Merge pull request #176 from Birdi7/add-multiple-text-filter

Add multiple text filter
This commit is contained in:
Alex Root Junior 2019-08-05 10:21:23 +03:00 committed by GitHub
commit 8823c8f22a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 257 additions and 24 deletions

View file

@ -206,18 +206,19 @@ class Text(Filter):
"""
def __init__(self,
equals: Optional[Union[str, LazyProxy]] = None,
contains: Optional[Union[str, LazyProxy]] = None,
startswith: Optional[Union[str, LazyProxy]] = None,
endswith: Optional[Union[str, LazyProxy]] = None,
equals: Optional[Union[str, LazyProxy, Iterable[Union[str, LazyProxy]]]] = None,
contains: Optional[Union[str, LazyProxy, Iterable[Union[str, LazyProxy]]]] = None,
startswith: Optional[Union[str, LazyProxy, Iterable[Union[str, LazyProxy]]]] = None,
endswith: Optional[Union[str, LazyProxy, Iterable[Union[str, LazyProxy]]]] = None,
ignore_case=False):
"""
Check text for one of pattern. Only one mode can be used in one filter.
In every pattern, a single string is treated as a list with 1 element.
:param equals:
:param contains:
:param startswith:
:param endswith:
:param equals: True if object's text in the list
:param contains: True if object's text contains all strings from the list
:param startswith: True if object's text starts with any of strings from the list
:param endswith: True if object's text ends with any of strings from the list
:param ignore_case: case insensitive
"""
# Only one mode can be used. check it.
@ -232,6 +233,9 @@ class Text(Filter):
elif check == 0:
raise ValueError(f"No one mode is specified!")
equals, contains, endswith, startswith = map(lambda e: [e] if isinstance(e, str) or isinstance(e, LazyProxy)
else e,
(equals, contains, endswith, startswith))
self.equals = equals
self.contains = contains
self.endswith = endswith
@ -267,25 +271,17 @@ class Text(Filter):
text = text.lower()
if self.equals is not None:
self.equals = str(self.equals)
if self.ignore_case:
self.equals = self.equals.lower()
return text == self.equals
self.equals = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.equals))
return text in self.equals
elif self.contains is not None:
self.contains = str(self.contains)
if self.ignore_case:
self.contains = self.contains.lower()
return self.contains in text
self.contains = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.contains))
return all(map(text.__contains__, self.contains))
elif self.startswith is not None:
self.startswith = str(self.startswith)
if self.ignore_case:
self.startswith = self.startswith.lower()
return text.startswith(self.startswith)
self.startswith = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.startswith))
return any(map(text.startswith, self.startswith))
elif self.endswith is not None:
self.endswith = str(self.endswith)
if self.ignore_case:
self.endswith = self.endswith.lower()
return text.endswith(self.endswith)
self.endswith = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.endswith))
return any(map(text.endswith, self.endswith))
return False