add NamedPattern

This commit is contained in:
Jan Petykiewicz 2023-01-27 10:07:39 -08:00 committed by jan
parent 1a9116cdbe
commit ab8fd9b351
3 changed files with 57 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import numpy
from numpy import pi from numpy import pi
from numpy.typing import ArrayLike from numpy.typing import ArrayLike
from ..pattern import Pattern from ..pattern import Pattern, NamedPattern
from ..ref import Ref from ..ref import Ref
from ..library import MutableLibrary from ..library import MutableLibrary
from ..error import PortError, BuildError from ..error import PortError, BuildError
@ -253,7 +253,7 @@ class Builder(PortList):
def plug( def plug(
self: BB, self: BB,
other: Union[Abstract, str], other: Union[Abstract, str, NamedPattern],
map_in: Dict[str, str], map_in: Dict[str, str],
map_out: Optional[Dict[str, Optional[str]]] = None, map_out: Optional[Dict[str, Optional[str]]] = None,
*, *,
@ -352,7 +352,7 @@ class Builder(PortList):
def place( def place(
self: BB, self: BB,
other: Union[Abstract, str], other: Union[Abstract, str, NamedPattern],
*, *,
offset: ArrayLike = (0, 0), offset: ArrayLike = (0, 0),
rotation: float = 0, rotation: float = 0,

View File

@ -25,7 +25,7 @@ from .label import Label
from .abstract import Abstract from .abstract import Abstract
if TYPE_CHECKING: if TYPE_CHECKING:
from .pattern import Pattern from .pattern import Pattern, NamedPattern
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -447,7 +447,7 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
del self[old_name] del self[old_name]
return self return self
def create(self, base_name: str) -> Tuple[str, 'Pattern']: def create(self, base_name: str) -> NamedPattern:
""" """
Convenience method to create an empty pattern, choose a name Convenience method to create an empty pattern, choose a name
for it, add it with that name, and return both the pattern and name. for it, add it with that name, and return both the pattern and name.
@ -460,8 +460,9 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
""" """
from .pattern import Pattern from .pattern import Pattern
name = self.get_name(base_name) name = self.get_name(base_name)
pat = Pattern() npat = NamedPattern(name)
self[name] = value self[name] = npat
return npat
def name_and_set( def name_and_set(
self, self,
@ -748,6 +749,8 @@ class WrapLibrary(MutableLibrary):
if callable(value): if callable(value):
value = value() value = value()
elif hasattr(value, 'as_pattern'):
value = cast('NamedPattern', value).as_pattern() # don't want to carry along NamedPattern instances
else: else:
value = value value = value
self.mapping[key] = value self.mapping[key] = value

View File

@ -665,3 +665,50 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
pyplot.ylabel('y') pyplot.ylabel('y')
pyplot.show() 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 __copy__(self) -> Pattern:
return Pattern.__copy__(self)
def __deepcopy__(self, memo: Optional[Dict] = 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,
)