From 4847ad9c403cd5c7a202e45c1a72c972bb0d1379 Mon Sep 17 00:00:00 2001 From: Stefan Holst Date: Tue, 21 Feb 2023 23:26:48 +0900 Subject: [PATCH] locating io ports and busses by name --- src/kyupy/circuit.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/kyupy/circuit.py b/src/kyupy/circuit.py index 53b2e51..d2febf8 100644 --- a/src/kyupy/circuit.py +++ b/src/kyupy/circuit.py @@ -6,6 +6,7 @@ and a line is an instance of class :class:`Line`. """ from collections import deque +import re class GrowingList(list): @@ -371,3 +372,22 @@ class Circuit: queue.extend(preds) region.append(n) yield stem, region + + def io_loc(self, prefix): + d_top = dict() + for i, n in enumerate(self.interface): + if m := re.match(fr'({prefix}.*?)((?:[_\[\]]\d+)*[_\[\]]*$)', n.name): + path = [m[1]] + [int(v) for v in re.split(r'[_\[\]]+', m[2]) if len(v) > 0] + d = d_top + for j in path[:-1]: + d[j] = d.get(j, dict()) + d = d[j] + d[path[-1]] = i + + def sorted_values(d): + return [sorted_values(v) for k, v in sorted(d.items())] if isinstance(d, dict) else d + + l = sorted_values(d_top) + while isinstance(l, list) and len(l) == 1: l = l[0] + + return l