add prune_empty and delete()

This commit is contained in:
Jan Petykiewicz 2023-02-09 16:38:42 -08:00 committed by jan
parent 85a2eb6acc
commit f766a3ad64

View File

@ -799,6 +799,35 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
new._merge(key, self, key)
return new
def prune_empty(
self,
repeat: bool = True,
) -> Set[str]:
# TODO doc prune_empty
trimmed = set()
while empty := set(name for name, pat in lib.items() if pat.is_empty()):
for name in empty:
del lib[name]
for pat in lib.values():
pat.refs = [ref for ref in pat.refs if ref.target not in empty]
trimmed |= empty
if not repeat:
break
return trimmed
def delete(
self: ML,
key: str,
delete_refs: bool = True,
) -> ML:
# TODO doc delete()
del self[key]
if delete_refs:
for pat in lib.values():
pat.refs = [ref for ref in pat.refs if ref.target != key]
return self
class WrapROLibrary(Library):
mapping: Mapping[str, 'Pattern']