From 0e2b628465db88aeed0d081509c593028a9e6717 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Fri, 2 Jun 2023 21:15:16 -0700 Subject: [PATCH] add prune_layers and prune_refs --- masque/pattern.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/masque/pattern.py b/masque/pattern.py index a465acf..34f1a95 100644 --- a/masque/pattern.py +++ b/masque/pattern.py @@ -678,6 +678,33 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable): self.labels[layer].append(Label(*args, **kwargs)) return self + def prune_layers(self) -> Self: + """ + Removes empty layers (empty lists) in `self.shapes` and `self.labels`. + + Returns: + self + """ + for layer in list(self.shapes): + if not self.shapes[layer]: + del self.shapes[layer] + for layer in list(self.labels): + if not self.labels[layer]: + del self.labels[layer] + return self + + def prune_refs(self) -> Self: + """ + Remove empty ref lists in `self.refs`. + + Returns: + self + """ + for target in list(self.refs): + if not self.refs[target]: + del self.refs[target] + return self + def flatten( self, library: Mapping[str, 'Pattern'],