SpiceCircuit

class spicelib.SpiceCircuit(parent=None)[source]

Bases: BaseSubCircuit

Represents sub-circuits within a SPICE circuit. Since sub-circuits can have sub-circuits inside them, it serves as base for the top level netlist. This hierarchical approach helps to encapsulate and protect parameters and components from edits made at a higher level.

add_component(component, **kwargs)[source]

Adds a component to the netlist. The component is added to the end of the netlist, just before the .END statement. If the component already exists, it will be replaced by the new one.

Parameters:
  • component (Component) – The component to be added to the netlist

  • kwargs

    The following keyword arguments are supported:

    • insert_before (str) - The reference of the component before which the new component should be inserted.

    • insert_after (str) - The reference of the component after which the new component should be inserted.

Returns:

Nothing

Return type:

None

add_instruction(instruction)[source]
begin_update()
class_for_instruction(instruction, cmd='')[source]
clone(new_parent, **kwargs)[source]

Creates a new copy of the SpiceCircuit. Changes done at the new copy do not affect the original.

Key new_name:

The new name to be given to the circuit

Key type new_name:

str

Returns:

The new replica of the SpiceCircuit object

Return type:

SpiceCircuit

property editor: BaseEditor | None

Returns the editor object representing the netlist. If the current object is a sub-circuit, it returns the parent

end_update(name, value, updates)
get_all_nodes()[source]

Retrieves all nodes existing on a Netlist.

Returns:

Circuit Nodes

Return type:

list[str]

get_all_parameter_names()[source]

Returns all parameter names from the netlist.

Returns:

A list of parameter names found in the netlist

Return type:

list[str]

get_component(reference)[source]

Returns an object representing the given reference in the schematic file.

Parameters:

reference (str) – Reference of the component

Returns:

The SpiceComponent object or a SpiceSubcircuit in case of hierarchical design

Raises:

ComponentNotFoundError - In case the component is not found

Raises:

UnrecognizedSyntaxError when the line doesn’t match the expected REGEX.

Raises:

NotImplementedError if there isn’t an associated regular expression for the component prefix.

Return type:

SpiceComponent | SpiceCircuitInstance | SpiceCircuit

get_component_attribute(reference, attribute)[source]

Returns the attribute of a component retrieved from the netlist.

Parameters:
  • reference (str) – Reference of the component

  • attribute (str) – Name of the attribute to be retrieved

Returns:

Value of the attribute

Return type:

str

Raises:

ComponentNotFoundError - In case the component is not found

Raises:

UnrecognizedSyntaxError when the line doesn’t match the expected REGEX.

Raises:

NotImplementedError if there isn’t an associated regular expression for the component prefix.

get_component_floatvalue(element)

Returns the value of a component retrieved from the netlist.

Parameters:

element (str) – Reference of the circuit element to get the value in float format.

Returns:

value of the circuit element in float type

Return type:

float

Raises:

ComponentNotFoundError - In case the component is not found

NotImplementedError - for not supported operations

get_component_nodes(reference)[source]

Returns the nodes to which the component is attached to.

Parameters:

reference (str) – Reference of the circuit element to get the nodes.

Returns:

List of nodes

Return type:

list[str]

get_component_parameters(reference)[source]

Returns the parameters of a component retrieved from the netlist.

Parameters:

element (str) – Reference of the circuit element to get the parameters.

Returns:

parameters of the circuit element in dictionary format.

Return type:

dict

Raises:

ComponentNotFoundError - In case the component is not found

NotImplementedError - for not supported operations

get_component_value(reference)[source]

Returns the value of a component retrieved from the netlist.

Parameters:

reference (str) – Reference of the circuit element to get the value.

Returns:

value of the circuit element .

Raises:

ComponentNotFoundError - In case the component is not found

NotImplementedError - for not supported operations

Return type:

str | None

get_components(prefixes='*')[source]

Returns a list of components that match the list of prefixes indicated on the parameter prefixes. In case prefixes is left empty, it returns all the ones that are defined by the REPLACE_REGEXES. The list will contain the designators of all components found.

Parameters:

prefixes (str) – Type of prefixes to search for. Examples: ‘C’ for capacitors; ‘R’ for Resistors; etc… See prefixes in SPICE documentation for more details. The default prefix is ‘*’ which is a special case that returns all components.

Returns:

A list of components matching the prefixes demanded.

Return type:

list

get_parameter(param)[source]

Returns the value of a parameter retrieved from the netlist.

Parameters:

param (str) – Name of the parameter to be retrieved

Returns:

Value of the parameter being sought

Return type:

str

Raises:

ParameterNotFoundError - In case the component is not found

get_subcircuit(instance_name)[source]

Returns an object representing a Subcircuit. This object can manipulate elements such as the SpiceEditor does.

Parameters:

instance_name (str) – Reference of the subcircuit

Returns:

SpiceCircuit instance

Return type:

SpiceCircuit

Raises:
  • UnrecognizedSyntaxError – when an spice command is not recognized by spicelib

  • ComponentNotFoundError – When the reference was not found

get_subcircuit_named(name)[source]

Returns the sub-circuit object with the given name.

Parameters:

name (str) – name of the subcircuit

Returns:

_description_

Return type:

_type_

get_subcircuit_names()[source]

Returns a list of the names of the sub-circuits in the netlist.

Returns:

list of sub-circuit names

Return type:

list[str]

is_read_only()[source]

Check if the component can be edited. This is useful when the editor is used on non modifiable files.

Returns:

True if the component is read-only, False otherwise

Return type:

bool

modified_subcircuits()[source]

Returns a list of all sub-circuits that have been modified.

Returns:

List of modified sub-circuits

Return type:

list[SpiceCircuit]

name()[source]

Returns the name of the Sub-Circuit.

Return type:

str

remove_Xinstruction(search_pattern)[source]
remove_component(designator)[source]

Removes a component from the design. Current implementation only allows removal of a component from the main netlist, not from a sub-circuit.

Parameters:

designator (str) – Component reference in the design. Ex: V1, C1, R1, etc…

Returns:

Nothing

Raises:

ComponentNotFoundError - When the component doesn’t exist on the netlist.

Return type:

None

remove_instruction(instruction)[source]
reset_netlist(**kwargs)[source]

Reverts all changes done to the netlist.

Returns:

None

Return type:

bool

save_netlist(run_netlist_file)[source]
set_component_attribute(reference, attribute, value)

Sets the value of the attribute of the component. Attributes are the values that are not related with SPICE parameters. For example, component manufacturer, footprint, schematic appearance, etc. User can define whatever attributes they want. The only restriction is that the attribute name must be a string.

Parameters:
  • reference (str) – Reference of the component

  • attribute (str) – Name of the attribute to be set

  • value (str) – Value of the attribute to be set

Returns:

Nothing

Raises:

ComponentNotFoundError - In case the component is not found

Return type:

None

set_component_parameters(reference, **kwargs)[source]

Adds one or more parameters to the component on the netlist. The argument is in the form of a key-value pair where each parameter is the key and the value is value to be set in the netlist.

Usage 1:

editor.set_component_parameters(R1, value=330, temp=25)

Usage 2:

value_settings = {'value': 330, 'temp': 25}
editor.set_component_parameters(R1, **value_settings)
Parameters:

element (str) – Reference of the circuit element.

Key <param_name>:

The key is the parameter name and the value is the value to be set. Values can either be strings; integers or floats. When None is given, the parameter will be removed, if possible.

Returns:

Nothing

Raises:

ComponentNotFoundError - In case one of the component is not found.

Return type:

None

set_component_value(reference, value)[source]

Changes the value of a component, such as a Resistor, Capacitor or Inductor. For components inside sub-circuits, use the sub-circuit designator prefix with ‘:’ as separator (Example X1:R1) Usage:

runner.set_component_value('R1', '3.3k')
runner.set_component_value('X1:C1', '10u')
Parameters:
  • reference (str) – Reference of the circuit element to be updated.

  • value (str, int or float) – value to be set on the given circuit element. Float and integer values will be automatically formatted as per the engineering notations ‘k’ for kilo, ‘m’, for mili and so on.

Raises:

ComponentNotFoundError - In case the component is not found

ValueError - In case the value doesn’t correspond to the expected format

NotImplementedError - In case the circuit element is defined in a format which is not supported by this version.

If this is the case, use GitHub to start a ticket. https://github.com/nunobrum/spicelib

set_component_values(**kwargs)

Adds one or more components on the netlist. The argument is in the form of a key-value pair where each component designator is the key and the value is value to be set in the netlist.

Usage 1:

editor.set_component_values(R1=330, R2="3.3k", R3="1Meg", V1="PWL(0 1 30m 1 30.001m 0 60m 0 60.001m 1)")

Usage 2:

value_settings = {'R1': 330, 'R2': '3.3k', 'R3': "1Meg", 'V1': 'PWL(0 1 30m 1 30.001m 0 60m 0 60.001m 1)'}
editor.set_component_values(**value_settings)
Key <comp_ref>:

The key is the component designator (Ex: V1) and the value is the value to be set. Values can either be strings; integers or floats

Returns:

Nothing

Raises:

ComponentNotFoundError - In case one of the component is not found.

set_element_model(reference, model)[source]

Changes the value of a circuit element, such as a diode model or a voltage supply. Usage:

runner.set_element_model('D1', '1N4148')
runner.set_element_model('V1' "SINE(0 1 3k 0 0 0)")
Parameters:
  • reference (str) – Reference of the circuit element to be updated.

  • model (str) – model name of the device to be updated

Raises:

ComponentNotFoundError - In case the component is not found

ValueError - In case the model format contains irregular characters

NotImplementedError - In case the circuit element is defined in a format which is not supported by this version.

If this is the case, use GitHub to start a ticket. https://github.com/nunobrum/spicelib

set_parameter(param, value)[source]

Sets the value of a parameter in the netlist. If the parameter is not found, it is added to the netlist.

Usage:

runner.set_parameter("TEMP", 80)

This adds onto the netlist the following line:

.PARAM TEMP=80

This is an alternative to the set_parameters which is more pythonic in its usage and allows setting more than one parameter at once.

Parameters:
  • param (str) – Spice Parameter name to be added or updated.

  • value (str, int or float) – Parameter Value to be set.

Returns:

Nothing

Return type:

None

set_parameters(**kwargs)

Adds one or more parameters to the netlist. Usage:

for temp in (-40, 25, 125):
    for freq in sweep_log(1, 100E3,):
        editor.set_parameters(TEMP=80, freq=freq)
Key param_name:

Key is the parameter to be set. values the ther corresponding values. Values can either be a str; an int or a float.

Returns:

Nothing

setname(new_name)[source]

Renames the sub-circuit to a new name. No check is done to the new name. It is up to the user to make sure that the new name is valid.

Parameters:

new_name (str) – The new Name.

Returns:

Nothing

property was_modified: bool
write_lines(stream)[source]

Internal function. Do not use.

class spicelib.SpiceComponent(*args, **kwargs)[source]

Bases: Component

Represents a SPICE component in the netlist. It allows the manipulation of the parameters and the value of the component.

property attributes: dict
begin_update()[source]
clear_param()
clear_parameter(key)

Clears a parameter. :param key: name

clear_parameters()

Clears all parameters. It will set all existing parameters to None. This will delete them when writing back to file.

clone(new_parent=None)

Clones the component

default_netlist = None
end_update(reference, value, update_type)[source]
get_parameter(key)

Gets a parameter value :param key: Parameter name :return: Parameter value :raises: ParameterNotFoundError when the parameter is not found

get_parameters()

Gets all parameter values :return: Dictionary with parameter names and values

get_value()
Returns:

Component value

Return type:

str | float | int | complex | None

get_value_str()
Returns:

Component value in string format

Return type:

str | None

property obj
property params: OrderedDict
property parent
port_list()

Gets the port names of the component as a list :return: List of port names

port_names(sep=' ')

Gets the port names of the component :return: List of port names

property ports: list[Net]

Gets the ports of the component :return: List of Net objects

property reference: str
rewrite_lines(stream)[source]

Write the SPICE representation of the component into a stream

Returns:

Number of characters written

Return type:

int

set_param(key, value)
set_parameter(key, value)[source]

Informs the netlist that a parameter of the component has changed

set_parameters(**params)[source]

Informs the netlist that the parameters of the component have changed

set_params(**params)
set_value(value)[source]

Informs the netlist that the value of the component has changed

property value
property value_str
write_lines(stream)[source]

Get the SPICE representation of the component as a string. This creates a new line from the attributes alone

Returns:

number of characters written

Return type:

int