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.
- 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:
- 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.
- 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:
- 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:
- 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.
- 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:
- erc_list = [<function dflt_part_erc>]¶
- generate_svg_component(symtx='', tool=None, net_stubs=None)[source]¶
Generate the SVG for displaying a part in an SVG schematic.
- Parameters:
- Returns:
SVG representation of the component.
- Return type:
- 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:
- 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:
- 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:
- 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:
- is_movable()[source]¶
Return T/F if the part can be moved from one circuit into another.
- This method returns true if:
the part is not in a circuit, or
the part has pins but none of them are connected to nets, or
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:
- 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:
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:
- property ordered_pins¶
Return the pins of the part in a sorted order.
- Returns:
Sorted list of the part’s pins.
- Return type:
- 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:
- 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:
- 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.
- 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:
- 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:
- 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:
- 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:
- 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:
- 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).