Skip to content

Writing netlists

Writers convert the format-neutral intermediate representation into a target format. The built-in SpectreWriter uses packaged Jinja2 templates for layout and Python code for value formatting and supported-element checks.

Write a string

from nimphel import Circuit, Component, ComponentKind
from nimphel.writers import SpectreWriter

source = Component(
    target="vsource",
    pins=("p", "n"),
    kind=ComponentKind.VOLTAGE_SOURCE,
    defaults={"dc": 1.0},
)

with Circuit(name="supply") as circuit:
    source(("vdd", 0))

text = SpectreWriter().dumps(circuit)

Write a file object

with open("supply.scs", "w") as output:
    SpectreWriter().dump(circuit, output)

The naming follows common Python serialization APIs:

  • dumps(value) returns text.
  • dump(value, file_object) writes text.

Supported elements

The Spectre writer currently supports:

  • Directive
  • Model
  • Instance
  • Subcircuit
  • Circuit

An instance must belong to a circuit or have an explicit ref before it can be written. Unsupported element types raise UnsupportedElementError; the writer never silently substitutes an object's debug representation.

Templates and formatting

Jinja2 templates control output layout. Semantic rules remain in Python:

  • Boolean parameters render as true or false.
  • Zero and other falsy parameter values are retained.
  • Circuit and subcircuit element order is preserved.
  • Required subcircuit parameters render without an assigned value.

This separation lets a future writer reuse the intermediate representation without inheriting Spectre-specific formatting decisions.