skidl.part module

Handles parts in a circuit.

This module provides classes and functions for creating, managing, and connecting electronic parts in a circuit. Parts can be instantiated from libraries, customized with attributes, and connected to form complete circuits.

class skidl.part.Part(lib=None, name=None, dest=NETLIST, tool=None, connections=None, part_defn=None, circuit=None, ref_prefix='', ref=None, tag=None, pin_splitters=None, **kwargs)[source]

Bases: SkidlBaseObject

A class for storing a definition of a schematic part.

This class represents electronic components in a schematic. Parts can be instantiated from libraries, customized with attributes, and connected together to form circuits.

Parameters:
  • lib (str or SchLib, optional) – Either a SchLib object or a schematic part library file name.

  • name (str, optional) – A string with name of the part to find in the library, or to assign to the part defined by the part definition.

  • dest (str, optional) – String that indicates where the part is destined for (e.g., NETLIST).

  • tool (str, optional) – The format for the library file or part definition (e.g., KICAD).

  • connections (dict, optional) – A dictionary with part pin names/numbers as keys and the nets to which they will be connected as values. For example: { ‘IN-‘:a_in, ‘IN+’:gnd, ‘1’:AMPED_OUTPUT, ‘14’:vcc, ‘7’:gnd }

  • part_defn (list, optional) – A list of strings that define the part (usually read from a schematic library file).

  • circuit (Circuit, optional) – The Circuit object this Part belongs to.

  • ref_prefix (str, optional) – Prefix for part references such as ‘U’ or ‘J’.

  • ref (str, optional) – A specific part reference to be assigned.

  • tag (str, optional) – A specific tag to tie the part to its footprint in the PCB.

  • pin_splitters (str, optional) – String of characters that split long pin names into shorter aliases.

Keyword Arguments:

kwargs – Name/value pairs for setting attributes for the part. For example, manf_num=’LM4808MP-8’ would create an attribute named ‘manf_num’ for the part and assign it the value ‘LM4808MP-8’.

Raises:
  • ValueError – If the part library and definition are both missing.

  • ValueError – If an unknown file format is requested.

name

The part name.

Type:

str

description

Description of the part.

Type:

str

ref

Reference designator for the part (e.g., ‘R1’, ‘U3’).

Type:

str

ref_prefix

Prefix for the reference (e.g., ‘R’, ‘U’).

Type:

str

pins

List of Pin objects for this part.

Type:

list

unit

Dictionary for storing subunits of the part.

Type:

dict

circuit

The circuit this part belongs to.

Type:

Circuit

add_pins(*pins)[source]

Add one or more pins to a part and return the part.

Parameters:

*pins – Pin objects to add to the part.

Returns:

The part with pins added.

Return type:

Part

associate_pins()[source]

Make sure all the pins in a part have valid references to the part.

This updates each pin’s part attribute to point to this part object.

attached_to(nets=None)[source]

Return True if any part pin is connected to a net in the list.

Parameters:

nets (list, optional) – List of nets to check for connection.

Returns:

True if part is connected to any of the nets, False otherwise.

Return type:

bool

convert_for_spice(spice_part, pin_map)[source]

Convert a Part object for use with SPICE.

Parameters:
  • spice_part (Part) – The type of SPICE Part to be converted to.

  • pin_map (dict) – Dict with pin numbers/names of self as keys and num/names of spice_part pins as replacement values.

copy(num_copies=None, dest=NETLIST, circuit=None, io=None, **attribs)[source]

Make zero or more copies of this part while maintaining all pin/net connections.

Parameters:
  • num_copies (int, optional) – Number of copies to make of this part.

  • dest (str, optional) – Indicates where the copy is destined for (e.g., NETLIST).

  • circuit (Circuit, optional) – The circuit this part should be added to.

  • io (list, optional) – XSPICE I/O names.

Keyword Arguments:

attribs – Name/value pairs for setting attributes for the copy.

Returns:

A list of Part copies or a single Part if num_copies==1.

Return type:

Part or list

Raises:

ValueError – If the requested number of copies is a non-integer or negative.

Notes

An instance of a part can be copied just by calling it like so:

res = Part("Device",'R')    # Get a resistor.
res_copy = res(value='1K')  # Copy the resistor and set resistance value.

You can also use the multiplication operator to make copies:

cap = Part("Device", 'C')   # Get a capacitor
caps = 10 * cap             # Make an array with 10 copies of it.
copy_units(src)[source]

Make copies of the units from the source part.

Parameters:

src (Part) – The source part containing units to copy.

Raises:

Exception – If an illegal unit type is encountered.

create_network()[source]

Create a network from the pins of a part.

Returns:

A network containing the part’s pins.

Return type:

Network

Raises:

Exception – If the part has more than 2 pins.

disconnect()[source]

Disconnect all the part’s pins from nets.

This removes all connections to the part’s pins.

erc_desc()[source]

Create description of part for ERC and other error reporting.

Returns:

A string description in the form “name/ref”

Return type:

str

erc_list = [<function dflt_part_erc>]
export(addtl_part_attrs=None)[source]

Return a string to recreate a Part object.

Parameters:

addtl_part_attrs (list, optional) – List of additional part attribute names to include in export.

Returns:

String that can be evaluated to rebuild the Part object.

Return type:

str

property foot

Get the part footprint.

Returns:

The part’s footprint.

Return type:

str

generate_svg_component(symtx='', tool=None, net_stubs=None)[source]

Generate the SVG for displaying a part in an SVG schematic.

Parameters:
  • symtx (str, optional) – Transform for the SVG symbol. Defaults to “”.

  • tool (str, optional) – Tool format to use for SVG generation. Defaults to config.tool.

  • net_stubs (list, optional) – List of net stubs to include in the SVG.

Returns:

SVG representation of the component.

Return type:

str

classmethod get(text, circuit=None)[source]

Get the part with the given text from a circuit, or return None.

Parameters:

text (str) – A text string that will be searched for in the list of parts.

Keyword Arguments:

circuit (Circuit, optional) – The circuit whose parts will be searched. If set to None, then the parts in the default_circuit will be searched.

Returns:

A list of parts or a single part that match the text string with

either their reference, name, alias, or their description.

Return type:

Part or list

get_pins(*pin_ids, **criteria)[source]

Return list of part pins selected by pin numbers or names.

Parameters:

pin_ids – A list of strings containing pin names, numbers, regular expressions, slices, lists or tuples. If empty, then it will select all pins.

Keyword Arguments:
  • criteria – Key/value pairs that specify attribute values the pins must have in order to be selected.

  • silent (bool, optional) – If True, don’t issue error messages. Defaults to False.

  • only_search_numbers (bool, optional) – Only search for pins by number. Defaults to False.

  • only_search_names (bool, optional) – Only search for pins by name. Defaults to False.

  • match_regex (bool, optional) – Allow regex pattern matching for pin names. Defaults to False.

Returns:

A list of pins matching the given IDs and satisfying all the criteria,

or just a single Pin object if only a single match was found. Or None if no match was found and silent=True.

Return type:

Pin or list

Raises:

ValueError – If pins can’t be found and silent=False.

grab_pins()[source]

Grab pins back from PartUnits.

Makes each unit release its pins back to the part that contains it.

property hiername

Return the hierarchical name of the part.

Returns:

The hierarchical name including hierarchy prefix and tag.

Return type:

str

property hiertuple
is_connected()[source]

Return T/F depending upon whether a part is connected in a netlist.

If a part has pins but none of them are connected to nets, then this method will return False. Otherwise, it will return True even if the part has no pins (which can be the case for mechanical parts, silkscreen logos, or other non-electrical schematic elements).

Returns:

True if the part is connected or has no pins, False otherwise.

Return type:

bool

is_movable()[source]

Return T/F if the part can be moved from one circuit into another.

This method returns true if:
  1. the part is not in a circuit, or

  2. the part has pins but none of them are connected to nets, or

  3. the part has no pins (which can be the case for mechanical parts, silkscreen logos, or other non-electrical schematic elements).

Returns:

True if the part can be moved to another circuit, False otherwise.

Return type:

bool

make_unit(label, *pin_ids, **criteria)[source]

Create a PartUnit from a set of pins in a Part object.

Parts can be organized into smaller pieces called PartUnits. A PartUnit acts like a Part but contains only a subset of the pins of the Part.

Parameters:
  • label (str) – The label used to identify the PartUnit.

  • pin_ids – A list of strings containing pin names, numbers, regular expressions, slices, lists or tuples.

Keyword Arguments:

criteria – Key/value pairs that specify attribute values the pin must have in order to be selected.

Returns:

The newly created part unit.

Return type:

PartUnit

Warning

Will issue a warning if the unit label collides with any part pin names.

property match_pin_regex

Get the enable/disable flag for pin regular-expression matching.

Returns:

Current state of regex matching flag.

Return type:

bool

property ordered_pins

Return the pins of the part in a sorted order.

Returns:

Sorted list of the part’s pins.

Return type:

list

parse(partial_parse=False)[source]

Create a part from its stored part definition.

Parameters:

partial_parse (bool, optional) – When true, just get the name and aliases for the part. Leave the rest unparsed. Defaults to False.

property partclasses

Get all part classes associated with this part.

Returns the combined set of part classes from both the hierarchical nodes surrounding this part and the part classes directly assigned to this part.

Returns:

A set containing all part classes from the node hierarchy and

the part’s directly assigned classes.

Return type:

set

property ref

Get the part reference.

When setting the part reference, if another part with the same reference is found, the reference for this part is adjusted to make it unique.

Returns:

The part’s reference designator.

Return type:

str

release_pins()[source]

A Part can’t release pins back to its PartUnits, so do nothing.

rename_pin(pin_id, new_pin_name)[source]

Assign a new name to a pin of a part.

Parameters:
  • pin_id – ID of pin to rename (name or number)

  • new_pin_name (str) – New name for the pin

renumber_pin(pin_id, new_pin_num)[source]

Assign a new number to a pin of a part.

Parameters:
  • pin_id – ID of pin to renumber (name or number)

  • new_pin_num – New number for the pin

rmv_pins(*pin_ids)[source]

Remove one or more pins from a part.

Parameters:

*pin_ids – IDs (names or numbers) of pins to remove.

rmv_unit(label)[source]

Remove a PartUnit from a Part.

Parameters:

label (str) – Label of unit to remove.

similarity(part, **options)[source]

Return a measure of how similar two parts are.

Parameters:
  • part (Part) – The part to compare to for similarity.

  • options (dict) – Dictionary of options and settings affecting similarity computation.

Returns:

Value for similarity (larger means more similar).

Return type:

float

split_pin_names(delimiters)[source]

Use chars in delimiters to split pin names and add as aliases to each pin.

Parameters:

delimiters (str) – String of characters to use as delimiters for splitting pin names.

swap_pins(pin_id1, pin_id2)[source]

Swap pin name/number between two pins of a part.

Parameters:
  • pin_id1 – ID of first pin (name or number)

  • pin_id2 – ID of second pin (name or number)

property tag_ref_name

Return the tag, reference, or name of an object.

This provides a way to retrieve the most relevant identifier for the object. If the tag is set, it will be returned. Otherwise, if the reference is set, it will be returned. If all else fails, the name will be returned.

Returns:

The tag, reference, or name of the object.

Return type:

str

validate()[source]

Check that pins and units reference the correct part that owns them.

Raises:

AssertionError – If pins or units don’t properly reference this part.

property value

Get the part value.

Returns:

The part’s value, or part name if no value is set.

Return type:

str

value_to_str()[source]

Return value of part as a string.

Returns:

String representation of the part’s value.

Return type:

str

class skidl.part.PartUnit(parent, label, *pin_ids, **criteria)[source]

Bases: Part

Create a PartUnit from a set of pins in a Part object.

Parts can be organized into smaller pieces called PartUnits. A PartUnit acts like a Part but contains only a subset of the pins of the Part. Except for the pins, the PartUnit is a shallow copy of the Part and cannot store any other unique data.

Parameters:
  • part (Part) – This is the parent Part whose pins the PartUnit is built from.

  • label (str) – A unique label for this unit.

  • pin_ids – A list of strings containing pin names, numbers, regular expressions, slices, lists or tuples. If empty, it will match every pin of the part.

Keyword Arguments:
  • criteria – Key/value pairs that specify attribute values the pin must have in order to be selected.

  • unit (int) – Unit number assigned to this PartUnit. Defaults to 1.

Examples

This will return unit 1 from a part:

lm358 = Part('linear','lm358')
lm358a = PartUnit(lm358, unit=1)

Or you can specify the pins directly:

lm358a = PartUnit(lm358, 1, 2, 3)
add_pins_from_parent(*pin_ids, **criteria)[source]

Add selected pins from the parent to the part unit.

Parameters:

pin_ids – Pin names/numbers to add from parent.

Keyword Arguments:

criteria – Key/value pairs for selecting pins.

export()[source]

Return a string describing the PartUnit for exporting purposes.

Returns:

Dictionary representation of the unit as a string.

Return type:

str

grab_pins()[source]

Grab pin from Part and assign to PartUnit.

This changes each pin’s part reference to point to this unit.

property hiertuple
property ref

Get the unit’s hierarchical reference.

Returns:

Reference in the form “parent_ref.label”

Return type:

str

release_pins()[source]

Return PartUnit pins to parent Part.

This changes each pin’s part reference to point back to the parent part.

validate()[source]

Check that unit pins point to the parent part.

Raises:

AssertionError – If pins don’t refer to the parent part.

skidl.part.default_empty_footprint_handler(part)[source]

Handle the situation of a Part with no footprint when generating netlist/PCB.

Parameters:

part (Part) – The part with no footprint.

Note

By default, this function logs an error message if the footprint is missing. Override this function if you want to try and set some default footprint for particular types of parts (such as using an 0805 footprint for a resistor).