Add recursive
arg to referenced_patterns_by_id
This commit is contained in:
parent
09615eaea6
commit
1976c6e684
@ -441,28 +441,31 @@ class Pattern:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def referenced_patterns_by_id(self,
|
def referenced_patterns_by_id(self,
|
||||||
include_none: bool = False
|
include_none: bool = False,
|
||||||
|
recursive: bool = True,
|
||||||
) -> Union[Dict[int, Optional['Pattern']],
|
) -> Union[Dict[int, Optional['Pattern']],
|
||||||
Dict[int, 'Pattern']]:
|
Dict[int, 'Pattern']]:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Create a dictionary with `{id(pat): pat}` for all Pattern objects referenced by this
|
Create a dictionary with `{id(pat): pat}` for all Pattern objects referenced by this
|
||||||
Pattern (operates recursively on all referenced Patterns as well)
|
Pattern (by default, operates recursively on all referenced Patterns as well).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
include_none: If `True`, references to `None` will be included. Default `False`.
|
include_none: If `True`, references to `None` will be included. Default `False`.
|
||||||
|
recursive: If `True`, operates recursively on all referenced patterns. Default `True`.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Dictionary with `{id(pat): pat}` for all referenced Pattern objects
|
Dictionary with `{id(pat): pat}` for all referenced Pattern objects
|
||||||
"""
|
"""
|
||||||
ids: Dict[int, Optional['Pattern']] = {}
|
ids: Dict[int, Optional['Pattern']] = {}
|
||||||
for subpat in self.subpatterns:
|
for subpat in self.subpatterns:
|
||||||
if id(subpat.pattern) not in ids:
|
pat = subpat.pattern
|
||||||
if subpat.pattern is not None:
|
if id(pat) in ids:
|
||||||
ids[id(subpat.pattern)] = subpat.pattern
|
continue
|
||||||
ids.update(subpat.pattern.referenced_patterns_by_id())
|
if include_none or pat is not None:
|
||||||
elif include_none:
|
ids[id(pat)] = pat
|
||||||
ids[id(subpat.pattern)] = subpat.pattern
|
if recursive and pat is not None:
|
||||||
|
ids.update(pat.referenced_patterns_by_id())
|
||||||
return ids
|
return ids
|
||||||
|
|
||||||
def referenced_patterns_by_name(self, **kwargs) -> List[Tuple[Optional[str], Optional['Pattern']]]:
|
def referenced_patterns_by_name(self, **kwargs) -> List[Tuple[Optional[str], Optional['Pattern']]]:
|
||||||
|
Loading…
Reference in New Issue
Block a user