11.08., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

Commit e9506364 authored by di68kap's avatar di68kap

- DHParser/syntaxtree.py: reverse-parameter added to pick and select_by_tag_function

parent aa36fbe5
...@@ -541,7 +541,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -541,7 +541,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
# yield from child.select(match_function, True, reverse) # yield from child.select(match_function, True, reverse)
def select_by_tag(self, tag_names: Union[str, AbstractSet[str]], def select_by_tag(self, tag_names: Union[str, AbstractSet[str]],
include_root: bool = False) -> Iterator['Node']: include_root: bool = False, reverse: bool = False) -> Iterator['Node']:
""" """
Returns an iterator that runs through all descendants that have one Returns an iterator that runs through all descendants that have one
of the given tag names. of the given tag names.
...@@ -570,9 +570,9 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -570,9 +570,9 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
""" """
if isinstance(tag_names, str): if isinstance(tag_names, str):
tag_names = frozenset({tag_names}) tag_names = frozenset({tag_names})
return self.select(lambda node: node.tag_name in tag_names, include_root) return self.select(lambda node: node.tag_name in tag_names, include_root, reverse)
def pick(self, tag_names: Union[str, Set[str]]) -> Optional['Node']: def pick(self, tag_names: Union[str, Set[str]], reverse: bool = False) -> Optional['Node']:
""" """
Picks the first descendant with one of the given tag_names. Picks the first descendant with one of the given tag_names.
...@@ -582,7 +582,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -582,7 +582,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
exists, it returns None. exists, it returns None.
""" """
try: try:
return next(self.select_by_tag(tag_names, False)) return next(self.select_by_tag(tag_names, False, reverse))
except StopIteration: except StopIteration:
return None return None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment