get rid of NamedPattern in favor of just returning a tuple

This commit is contained in:
jan 2023-04-06 16:52:01 -07:00
parent 9115371b19
commit cbfbdf66a1
5 changed files with 16 additions and 72 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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'<NamedPattern "{self.name}":'
s += f' sh{len(self.shapes)} sp{len(self.refs)} la{len(self.labels)} ['
for name, port in self.ports.items():
s += f'\n\t{name}: {port}'
s += ']>'
return s
def __str__(self) -> 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,
)

View File

@ -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