Skip to content

Commit

Permalink
netlist: add rename_models
Browse files Browse the repository at this point in the history
  • Loading branch information
flaport committed Nov 15, 2024
1 parent 4114a13 commit b5cb8e5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions sax/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .netlist import load_recursive_netlist as load_recursive_netlist
from .netlist import netlist as netlist
from .netlist import rename_instances as rename_instances
from .netlist import rename_models as rename_models
from .saxtypes import Array as Array
from .saxtypes import ArrayLike as ArrayLike
from .saxtypes import Complex as Complex
Expand Down
85 changes: 82 additions & 3 deletions sax/netlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,10 @@ def rename_instances(
) -> Netlist | RecursiveNetlist | NetlistDict | RecursiveNetlistDict:
given_as_dict = isinstance(netlist, dict)

try:
netlist = Netlist.model_validate(netlist)
except ValidationError:
if is_recursive(netlist):
netlist = RecursiveNetlist.model_validate(netlist)
else:
netlist = Netlist.model_validate(netlist)

if isinstance(netlist, RecursiveNetlist):
net = RecursiveNetlist(
Expand Down Expand Up @@ -545,3 +545,82 @@ def rename_instances(
settings=netlist.settings,
)
return net if not given_as_dict else net.model_dump()


@overload
def rename_models(netlist: Netlist, mapping: dict[str, str]) -> Netlist:
...


@overload
def rename_models(
netlist: RecursiveNetlist, mapping: dict[str, str]
) -> RecursiveNetlist:
...


@overload
def rename_models(netlist: NetlistDict, mapping: dict[str, str]) -> NetlistDict:
...


@overload
def rename_models(
netlist: RecursiveNetlistDict, mapping: dict[str, str]
) -> RecursiveNetlistDict:
...


def rename_models(
netlist: Netlist | RecursiveNetlist | NetlistDict | RecursiveNetlistDict,
mapping: dict[str, str],
) -> Netlist | RecursiveNetlist | NetlistDict | RecursiveNetlistDict:
given_as_dict = isinstance(netlist, dict)

if is_recursive(netlist):
netlist = RecursiveNetlist.model_validate(netlist)
else:
netlist = Netlist.model_validate(netlist)

if isinstance(netlist, RecursiveNetlist):
net = RecursiveNetlist(
**{
k: rename_models(v, mapping).model_dump()
for k, v in netlist.root.items()
}
)
return net if not given_as_dict else net.model_dump()

# it's a sax.Netlist now:
inverse_mapping = {v: k for k, v in mapping.items()}
if len(inverse_mapping) != len(mapping):
raise ValueError("Duplicate names to map onto found.")

instances = {}
for k, instance in netlist.instances.items():
given_as_str = False
if isinstance(instance, str):
given_as_str = True
instance = {
"component": instance,
"settings": {},
}
elif isinstance(instance, Component):
instance = instance.model_dump()
assert isinstance(instance, dict)
instance["component"] = mapping.get(
instance["component"], instance["component"]
)
if given_as_str:
instances[k] = instance["component"]
else:
instances[k] = instance

net = Netlist(
instances=instances,
connections=netlist.connections,
ports=netlist.ports,
placements=netlist.placements,
settings=netlist.settings,
)
return net if not given_as_dict else net.model_dump()

0 comments on commit b5cb8e5

Please sign in to comment.