iteration and collection simplifications

This commit is contained in:
Jan Petykiewicz 2024-07-28 20:12:25 -07:00
parent f304217d76
commit 62fc64c344
6 changed files with 12 additions and 12 deletions

View File

@ -248,7 +248,7 @@ class RenderPather(PortList):
other_tgt = self.library[other.name]
# get rid of plugged ports
for kk in map_in.keys():
for kk in map_in:
if kk in self.paths:
self.paths[kk].append(RenderStep('P', None, self.ports[kk].copy(), self.ports[kk].copy(), None))

View File

@ -176,7 +176,7 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
tops = tuple(self.keys())
if skip is None:
skip = set([None])
skip = {None}
if isinstance(tops, str):
tops = (tops,)
@ -213,7 +213,7 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
if isinstance(tops, str):
tops = (tops,)
keep = cast(set[str], self.referenced_patterns(tops) - set((None,)))
keep = cast(set[str], self.referenced_patterns(tops) - {None})
keep |= set(tops)
filtered = {kk: vv for kk, vv in self.items() if kk in keep}
@ -665,7 +665,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
duplicates = set(self.keys()) & set(other.keys())
if not duplicates:
for key in other.keys():
for key in other:
self._merge(key, other, key)
return {}
@ -912,7 +912,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
if isinstance(tops, str):
tops = (tops,)
keep = cast(set[str], self.referenced_patterns(tops) - set((None,)))
keep = cast(set[str], self.referenced_patterns(tops) - {None})
keep |= set(tops)
new = type(self)()
@ -934,7 +934,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
A set containing the names of all deleted patterns
"""
trimmed = set()
while empty := set(name for name, pat in self.items() if pat.is_empty()):
while empty := {name for name, pat in self.items() if pat.is_empty()}:
for name in empty:
del self[name]

View File

@ -472,10 +472,10 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
self.polygonize()
for layer in self.shapes:
self.shapes[layer] = list(chain.from_iterable((
self.shapes[layer] = list(chain.from_iterable(
ss.manhattanize(grid_x, grid_y)
for ss in self.shapes[layer]
)))
))
return self
def as_polygons(self, library: Mapping[str, 'Pattern']) -> list[NDArray[numpy.float64]]:

View File

@ -239,7 +239,7 @@ class PortList(metaclass=ABCMeta):
if duplicates:
raise PortError(f'Unrenamed ports would be overwritten: {duplicates}')
renamed = {mapping[k]: self.ports.pop(k) for k in mapping.keys()}
renamed = {vv: self.ports.pop(kk) for kk, vv in mapping.items()}
if None in renamed:
del renamed[None]

View File

@ -17,11 +17,11 @@ class AutoSlots(ABCMeta):
for base in bases:
parents |= set(base.mro())
slots = tuple(dctn.get('__slots__', tuple()))
slots = tuple(dctn.get('__slots__', ()))
for parent in parents:
if not hasattr(parent, '__annotations__'):
continue
slots += tuple(getattr(parent, '__annotations__').keys())
slots += tuple(parent.__annotations__.keys())
dctn['__slots__'] = slots
return super().__new__(cls, name, bases, dctn)

View File

@ -150,7 +150,7 @@ def data_to_ports_flat(
Returns:
The updated `pattern`. Port labels are not removed.
"""
labels = list(chain.from_iterable((pattern.labels[layer] for layer in layers)))
labels = list(chain.from_iterable(pattern.labels[layer] for layer in layers))
if not labels:
return pattern