add name_and_set

This commit is contained in:
Jan Petykiewicz 2023-01-26 11:43:55 -08:00
parent a061c5a2d9
commit 6cbdd7930d

View File

@ -432,6 +432,26 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
def _merge(self, other: Mapping[str, 'Pattern'], key: str) -> None: def _merge(self, other: Mapping[str, 'Pattern'], key: str) -> None:
pass pass
def name_and_set(
self,
base_name: str
value: Union['Pattern', Callable[[], 'Pattern']],
) -> str:
"""
Convenience method which finds a suitable name for the provided
pattern, adds it with that name, and returns the name.
Args:
base_name: Prefix used when naming the pattern
value: The pattern (or callable used to generate it)
Returns:
The name of the pattern.
"""
name = self.get_name(base_name)
self[name] = value
return name
def add( def add(
self: ML, self: ML,
other: Mapping[str, 'Pattern'], other: Mapping[str, 'Pattern'],