From 62b82eb230ab798ad0be2b794250bf91745da222 Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 6 Apr 2023 16:52:01 -0700 Subject: [PATCH] get rid of NamedPattern in favor of just returning a tuple --- masque/__init__.py | 2 +- masque/builder/builder.py | 6 ++--- masque/library.py | 23 ++++++++---------- masque/pattern.py | 51 --------------------------------------- masque/ref.py | 6 ++--- 5 files changed, 16 insertions(+), 72 deletions(-) diff --git a/masque/__init__.py b/masque/__init__.py index 93d207c..a1f511f 100644 --- a/masque/__init__.py +++ b/masque/__init__.py @@ -32,7 +32,7 @@ from .error import MasqueError, PatternError, LibraryError, BuildError from .shapes import Shape, Polygon, Path, Circle, Arc, Ellipse from .label import Label from .ref import Ref -from .pattern import Pattern, NamedPattern +from .pattern import Pattern from .library import ( Library, MutableLibrary, diff --git a/masque/builder/builder.py b/masque/builder/builder.py index 7956b5e..ab20d3d 100644 --- a/masque/builder/builder.py +++ b/masque/builder/builder.py @@ -6,7 +6,7 @@ import numpy from numpy import pi from numpy.typing import ArrayLike -from ..pattern import Pattern, NamedPattern +from ..pattern import Pattern from ..ref import Ref from ..library import MutableLibrary, Tree from ..error import PortError, BuildError @@ -236,7 +236,7 @@ class Builder(PortList): def plug( self, - other: Abstract | str | NamedPattern, + other: Abstract | str, map_in: dict[str, str], map_out: dict[str, str | None] | None = None, *, @@ -337,7 +337,7 @@ class Builder(PortList): def place( self, - other: Abstract | str | NamedPattern, + other: Abstract | str, *, offset: ArrayLike = (0, 0), rotation: float = 0, diff --git a/masque/library.py b/masque/library.py index d605d5e..9afd150 100644 --- a/masque/library.py +++ b/masque/library.py @@ -26,7 +26,7 @@ from .label import Label from .abstract import Abstract if TYPE_CHECKING: - from .pattern import Pattern, NamedPattern + from .pattern import Pattern logger = logging.getLogger(__name__) @@ -490,22 +490,21 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta) ref.target = new_target return self - def create(self, name: str) -> 'NamedPattern': + def mkpat(self, name: str) -> tuple[str, 'Pattern']: """ - Convenience method to create an empty pattern, choose a name - for it, add it with that name, and return both the pattern and name. + Convenience method to create an empty pattern, add it to the library, + and return both the pattern and name. Args: - base_name: Prefix used when naming the pattern + name: Name for the pattern Returns: (name, pattern) tuple """ - from .pattern import NamedPattern - #name = self.get_name(base_name) - npat = NamedPattern(name) - self[name] = npat - return npat + from .pattern import Pattern + pat = Pattern() + self[name] = pat + return pat def add( self, @@ -865,8 +864,6 @@ class WrapLibrary(MutableLibrary): if callable(value): value = value() - elif hasattr(value, 'as_pattern'): - value = cast('NamedPattern', value).as_pattern() # don't want to carry along NamedPattern instances else: value = value self.mapping[key] = value @@ -1045,7 +1042,7 @@ class Tree(MutableLibrary): def __init__( self, - top: 'str | NamedPattern', + top: str, library: MutableLibrary | None = None ) -> None: self.top = top if isinstance(top, str) else top.name diff --git a/masque/pattern.py b/masque/pattern.py index 9e136e1..e1ba0ed 100644 --- a/masque/pattern.py +++ b/masque/pattern.py @@ -700,54 +700,3 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable): pyplot.xlabel('x') pyplot.ylabel('y') pyplot.show() - - -class NamedPattern(Pattern): - """ - TODO: Document NamedPattern - """ - __slots__ = ('_name',) - - _name: str - """ The pattern's name """ - - @property - def name(self) -> str: - return self._name - - def __init__(self, name: str) -> None: - """ - Creates an empty NamedPattern - - Args: - name: The pattern's name. Immutable. - - """ - Pattern.__init__(self) - self._name = name - - def __repr__(self) -> str: - s = f' str: - return self.name - - def __copy__(self) -> Pattern: - return Pattern.__copy__(self) - - def __deepcopy__(self, memo: dict | None = None) -> Pattern: - return Pattern.__deepcopy__(self, memo) - - def as_pattern(self) -> Pattern: - return Pattern( - shapes=self.shapes, - labels=self.labels, - refs=self.refs, - annotations=self.annotations, - ports=self.ports, - ) diff --git a/masque/ref.py b/masque/ref.py index add2f7d..f490c41 100644 --- a/masque/ref.py +++ b/masque/ref.py @@ -21,7 +21,7 @@ from .traits import ( if TYPE_CHECKING: - from . import Pattern, NamedPattern + from . import Pattern class Ref( @@ -46,7 +46,7 @@ class Ref( def __init__( self, - target: 'str | NamedPattern | None', + target: str | None, *, offset: ArrayLike = (0.0, 0.0), rotation: float = 0.0, @@ -64,8 +64,6 @@ class Ref( scale: Scaling factor applied to the pattern's geometry. repetition: `Repetition` object, default `None` """ - if hasattr(target, 'name'): - target = cast('NamedPattern', target).name self.target = target self.offset = offset self.rotation = rotation