From 9d466882a0d93f42db3bd947e334e3a0afc098c9 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 6 Feb 2023 12:11:53 -0800 Subject: [PATCH] misc fixes --- masque/library.py | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/masque/library.py b/masque/library.py index afce0f4..25ee7c7 100644 --- a/masque/library.py +++ b/masque/library.py @@ -38,6 +38,15 @@ ML = TypeVar('ML', bound='MutableLibrary') LL = TypeVar('LL', bound='LazyLibrary') +def _rename_patterns(lib: 'Library', name: str) -> str: + # TODO document rename function + if not name.startswith('_'): + return name + + stem = name.split('$')[0] + return lib.get_name(stem) + + class Library(Mapping[str, 'Pattern'], metaclass=ABCMeta): # inherited abstract functions #def __getitem__(self, key: str) -> 'Pattern': @@ -473,7 +482,7 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta) ref.target = new_target return self - def create(self, base_name: str) -> NamedPattern: + def create(self, name: str) -> 'NamedPattern': """ Convenience method to create an empty pattern, choose a name for it, add it with that name, and return both the pattern and name. @@ -485,14 +494,14 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta) (name, pattern) tuple """ from .pattern import Pattern - name = self.get_name(base_name) + #name = self.get_name(base_name) npat = NamedPattern(name) self[name] = npat return npat - def name_and_set( + def set( self, - base_name: str, + name: str, value: Union['Pattern', Callable[[], 'Pattern']], ) -> str: """ @@ -506,7 +515,7 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta) Returns: The name of the pattern. """ - name = self.get_name(base_name) + #name = self.get_name(base_name) self[name] = value return name @@ -796,6 +805,9 @@ class WrapROLibrary(Library): def __len__(self) -> int: return len(self.mapping) + def __contains__(self, key: str) -> bool: + return key in self.mapping + def __repr__(self) -> str: return f'' @@ -821,6 +833,9 @@ class WrapLibrary(MutableLibrary): def __len__(self) -> int: return len(self.mapping) + def __contains__(self, key: str) -> bool: + return key in self.mapping + def __setitem__( self, key: str, @@ -905,6 +920,8 @@ class LazyLibrary(MutableLibrary): pat = tree[tree.top] del tree[tree.top] self.add(tree) + else: + pat = pat_or_tree self.cache[key] = pat return pat @@ -915,6 +932,9 @@ class LazyLibrary(MutableLibrary): def __len__(self) -> int: return len(self.dict) + def __contains__(self, key: str) -> bool: + return key in self.dict + def _merge(self, key_self: str, other: Mapping[str, 'Pattern'], key_other: str) -> None: if isinstance(other, LazyLibrary): self.dict[key_self] = other.dict[key_other] @@ -1009,12 +1029,12 @@ class Tree(MutableLibrary): library: MutableLibrary @property - def pattern(self) -> Pattern: + def pattern(self) -> 'Pattern': return self.library[self.top] def __init__( self, - top: Union[str, NamedPattern], + top: Union[str, 'NamedPattern'], library: Optional[MutableLibrary] = None ) -> None: self.top = top if isinstance(top, str) else top.name @@ -1047,12 +1067,3 @@ class Tree(MutableLibrary): def _merge(self, key_self: str, other: Mapping[str, 'Pattern'], key_other: str) -> None: self.library._merge(key_self, other, key_other) - - -def _rename_patterns(lib: Library, name: str) -> str: - # TODO document rename function - if not name.startswith('_'): - return name - - stem = name.split('$')[0] - return lib.get_name(stem)