Adding new filter and search functions to parser
This commit is contained in:
+20
-23
@@ -120,7 +120,7 @@ class Adaptor(SelectorsGeneration):
|
|||||||
def _is_text_node(element: Union[html.HtmlElement, etree._ElementUnicodeResult]) -> bool:
|
def _is_text_node(element: Union[html.HtmlElement, etree._ElementUnicodeResult]) -> bool:
|
||||||
"""Return True if given element is a result of a string expression
|
"""Return True if given element is a result of a string expression
|
||||||
Examples:
|
Examples:
|
||||||
Xpath -> '/text()', '/@attribute' etc...
|
XPath -> '/text()', '/@attribute' etc...
|
||||||
CSS3 -> '::text', '::attr(attrib)'...
|
CSS3 -> '::text', '::attr(attrib)'...
|
||||||
"""
|
"""
|
||||||
# Faster than checking `element.is_attribute or element.is_text or element.is_tail`
|
# Faster than checking `element.is_attribute or element.is_text or element.is_tail`
|
||||||
@@ -1007,28 +1007,25 @@ class Adaptors(List[Adaptor]):
|
|||||||
return result
|
return result
|
||||||
return default
|
return default
|
||||||
|
|
||||||
# def __getattr__(self, name):
|
def search(self, func: Callable[['Adaptor'], bool]) -> Union['Adaptor', None]:
|
||||||
# if name in dir(self.__class__):
|
"""Loop over all current elements and return the first element that matches the passed function
|
||||||
# return super().__getattribute__(name)
|
:param func: A function that takes each element as an argument and returns True/False
|
||||||
#
|
:return: The first element that match the function or ``None`` otherwise.
|
||||||
# # Execute the method itself on each Adaptor
|
"""
|
||||||
# results = []
|
for element in self:
|
||||||
# for item in self:
|
if func(element):
|
||||||
# results.append(getattr(item, name))
|
return element
|
||||||
#
|
return None
|
||||||
# if all(callable(r) for r in results):
|
|
||||||
# def call_all(*args, **kwargs):
|
def filter(self, func: Callable[['Adaptor'], bool]) -> Union['Adaptors', List]:
|
||||||
# final_results = [r(*args, **kwargs) for r in results]
|
"""Filter current elements based on the passed function
|
||||||
# if all([isinstance(r, (Adaptor, Adaptors,)) for r in results]):
|
:param func: A function that takes each element as an argument and returns True/False
|
||||||
# return self.__class__(final_results)
|
:return: The new `Adaptors` object or empty list otherwise.
|
||||||
# return final_results
|
"""
|
||||||
#
|
results = [
|
||||||
# return call_all
|
element for element in self if func(element)
|
||||||
# else:
|
]
|
||||||
# # Flatten the result if it's a single-item list containing a list
|
return self.__class__(results) if results else results
|
||||||
# if len(self) == 1 and isinstance(results[0], list):
|
|
||||||
# return self.__class__(results[0])
|
|
||||||
# return self.__class__(results)
|
|
||||||
|
|
||||||
def get(self, default=None):
|
def get(self, default=None):
|
||||||
"""Returns the first item of the current list
|
"""Returns the first item of the current list
|
||||||
|
|||||||
Reference in New Issue
Block a user