virtual_knitting_machine.machine_constructed_knit_graph.Machine_Knit_Yarn module
Module containing the Machine_Knit_Yarn class for representing yarn in machine knitting operations.
This module extends the base Yarn class to include machine-specific functionality including carrier management, float tracking, loop creation, and machine state coordination for yarn operations on virtual knitting machines.
- class Machine_Knit_Yarn(carrier, properties, instance=0, **_kwargs)[source]
Bases:
Yarn[Machine_LoopT]An extension of the base Yarn class to capture machine knitting specific information.
This includes carrier assignment, active loop tracking, float management, and machine state coordination. This class manages yarn operations during machine knitting including loop creation, float validation, and carrier state tracking with configurable maximum float lengths.
- active_loops
Dictionary mapping active loops to their holding needles.
- Type:
- __init__(carrier, properties, instance=0, **_kwargs)[source]
Initialize a machine knit yarn with carrier and properties.
- Parameters:
carrier (Yarn_Carrier) – The yarn carrier this yarn is assigned to.
properties (Yarn_Properties | None) – Properties for this yarn, creates default if None.
instance (int, optional) – Instance number for yarn identification. Defaults to 0.
- property is_active: bool
Check if yarn is active and can form new loops.
- Returns:
True if yarn is active and can form new loops, False otherwise.
- Return type:
- property is_hooked: bool
Check if carrier is on yarn inserting hook.
- Returns:
True if carrier is on yarn inserting hook, False otherwise.
- Return type:
- property carrier: Yarn_Carrier
Get the carrier assigned to yarn or None if yarn has been dropped from carrier.
- Returns:
Carrier assigned to yarn or None if yarn has been dropped from carrier.
- Return type:
- property last_needle: Needle[Machine_LoopT] | None
Get the needle that holds the loop closest to the end of the yarn.
- Returns:
- The needle that holds the loop closest to the end of the yarn,
or None if the yarn has been dropped entirely.
- Return type:
Needle | None
- active_floats()[source]
Get dictionary of loops that are active keyed to active yarn-wise neighbors.
- Returns:
- Dictionary of loops that are active keyed to active yarn-wise neighbors.
Each key-value pair represents a directed float where key comes before value on the yarn.
- Return type:
- add_loop_to_end(loop)[source]
Add an existing loop to the end of this yarn and associated knit graph.
- Parameters:
loop (Machine_Knit_Loop) – The loop to be added at the end of this yarn.
- Returns:
The loop that was added to the end of the yarn.
- Return type:
- Warns:
Long_Float_Warning – If the float from the prior loop is too long for the machine specification.
- classmethod __class_getitem__(params)
Parameterizes a generic class.
At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.
However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….
- __contains__(item)
- __getitem__(item)
Get a loop by its ID from this yarn.
- Parameters:
item (int | tuple[LoopT | int, LoopT | int] | slice) – The loop ,loop ID, or float between two loops to retrieve from this yarn. If given a slice, it will retrieve the elements between the specified indices in the standard ordering of loops along the yarn.
- Returns:
The loop on the yarn with the matching ID. Float_Edge[LoopT]: The float data for the given pair of loops forming a float. list[LoopT]: The loops in the slice of the yarn based on their ordering along the yarn.
- Return type:
LoopT
- Raises:
KeyError – If the item is not found on this yarn.
- __hash__()
Get the hash value of this yarn for use in sets and dictionaries.
- Returns:
Hash value based on the yarn properties.
- Return type:
- __iter__()
Iterate over loops on this yarn in sequence from first to last.
- Returns:
An iterator over the loops on this yarn in their natural sequence order.
- Return type:
Iterator[Loop]
- __repr__()
Get the representation string of this yarn for debugging.
- Returns:
The representation of the yarn properties.
- Return type:
- __str__()
Get the string representation of this yarn.
- Returns:
The yarn identifier string.
- Return type:
- add_edge(u, v, edge_data)
Connect the given loop u to the loop v with the given edge data. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int :param edge_data: The edge data to associate with the connection. :type edge_data: EdgeT
- Raises:
KeyError – Either of the given loops does not exist in the graph.
- add_loop(loop)
Add the given loop to the loop graph. :param loop: The loop to add to the graph. :type loop: LoopT
- ancestors(loop)
- Parameters:
loop (LoopT) – The loop to get the ancestors of from the graph.
- Returns:
The ancestors of the given loop.
- Return type:
set[LoopT]
- property contains_loops: bool
Returns: bool: True if the graph has at least one loop, False otherwise.
- cut_yarn()
Cut yarn to make it no longer active and create a new yarn instance of the same type.
- Returns:
New yarn of the same type after cutting this yarn.
- Return type:
Yarn[LoopT]
- dfs_edges(loop)
- dfs_preorder_loops(loop)
- Parameters:
loop (LoopT) – The loop to start iteration from.
- Returns:
The depth-first-search ordering of loops in the graph starting from the given loop.
- Return type:
Iterator[LoopT]
- property edge_iter: Iterator[tuple[LoopT, LoopT, EdgeT]]
- Returns:
Iterator over the edges and edge-data in the graph.
- Return type:
Iterator[tuple[LoopT, LoopT, EdgeT]]
Notes
No guarantees about the order of the edges.
- property first_loop: LoopT | None
Get the first loop at the beginning of the yarn.
- Returns:
The first loop on this yarn, or None if no loops have been added.
- Return type:
Loop | None
- property float_iter: Iterator[tuple[LoopT, LoopT]]
Returns: Iterator[tuple[Loop, Loop]]: An iterator over tuples of connected loops representing the yarn path.
- following_float(loop)
- Parameters:
loop (LoopT) – The loop at the start of the desired float.
- Returns:
The float-edge between the given loop and the next loop on the yarn or None if it’s the last loop.
- Return type:
Float_Edge[LoopT] | None
- get_child_loop(loop)
- Parameters:
loop (LoopT) – The loop to look for a child loop from.
- Returns:
The child loop if one exists, or None if no child loop is found.
- Return type:
LoopT | None
- get_edge(u, v)
- get_loop(loop_id)
- Parameters:
loop_id (int) – The loop id of the loop to get from the graph.
- Returns:
The loop node in the graph.
- Return type:
LoopT
- get_loops_behind_float(u, v)
Get all loops positioned behind the float between two loops.
- Parameters:
u (Loop) – The first loop in the float pair.
v (Loop) – The second loop in the float pair.
- Returns:
Set of loops positioned behind the float between u and v, or empty set if no float exists.
- Return type:
set[Loop]
- get_loops_in_front_of_float(u, v)
Get all loops positioned in front of the float between two loops.
- Parameters:
u (Loop) – The first loop in the float pair.
v (Loop) – The second loop in the float pair.
- Returns:
Set of loops positioned in front of the float between u and v, or empty set if no float exists.
- Return type:
set[Loop]
- has_child_loop(loop)
- Parameters:
loop (Loop) – The loop to check for child connections.
- Returns:
True if the loop has a child loop, False otherwise.
- Return type:
- has_edge(u, v)
- has_float(u, v)
Check if there is a float edge between two loops on this yarn.
- Parameters:
u (Loop) – The first loop to check for float connection.
v (Loop) – The second loop to check for float connection.
- Returns:
True if there is a float edge between the loops, False otherwise.
- Return type:
- has_loop(loop)
- property has_loops: bool
Check if the yarn has any loops on it.
- Returns:
True if the yarn has at least one loop, False otherwise.
- Return type:
- insert_loop(loop, prior_loop=None)
Insert a loop into the yarn sequence after the specified prior loop.
- Parameters:
loop (Loop | int, optional) – The loop or loop id to create a new loop. Defaults to a new loop with the id of the next loop along this yarn.
prior_loop (Loop | None) – The loop that should come before this loop on the yarn. If None, defaults to the last loop (adding to end of yarn).
- Raises:
Use_Cut_Yarn_ValueError – If the yarn is cut and should not form new loops.
- property is_cut: bool
Returns: bool: True if yarn has been cut and will no longer form loops, False otherwise.
- is_descendant(ancestor, descendant)
- Parameters:
ancestor (LoopT) – The loop to check if it is an ancestor of the other loop.
descendant (LoopT) – The loop to check if it is a descendant of the other loop.
- Returns:
True if there is a directed path from the ancestor to the descendant, False otherwise.
- Return type:
- is_terminal_loop(loop)
- Parameters:
loop (LoopT) – The loop to check for terminal status.
- Returns:
True if the loop has no child loops, False otherwise.
- Return type:
- property knit_graph: Knit_Graph[LoopT]
Returns: Knit_Graph | Knit_Graph: The knit graph that owns this yarn, or None if not associated with a graph.
- property last_loop: LoopT | None
Get the most recently added loop at the end of the yarn.
- Returns:
The last loop on this yarn, or None if no loops have been added.
- Return type:
Loop | None
- loops_behind_floats()
Get all float segments with loops positioned behind them.
- loops_in_front_of_floats()
Get all float segments with loops positioned in front of them.
- next_loop(loop)
- Parameters:
loop (LoopT) – The loop to find the next loop from.
- Returns:
The next loop on yarn after the specified loop, or None if it’s the last loop.
- Return type:
LoopT | None
- Raises:
KeyError – If the specified loop is not on this yarn.
- next_loop_id()
- Returns:
The ID of the next loop to be added to this yarn based on the knit graph or, if no knit graph is associated with this yarn, based on the last loop on this yarn.
- Return type:
- predecessors(loop)
- prior_loop(loop)
- Parameters:
loop (Loop) – The loop to find the prior loop from.
- Returns:
The prior loop on yarn before the specified loop, or None if it’s the first loop.
- Return type:
Loop | None
- Raises:
KeyError – If the specified loop is not on this yarn.
- proceeding_float(loop)
- Parameters:
loop (LoopT) – The loop at the end of the desired float.
- Returns:
The float-edge from the loop prior to the given loop and to the next loop or None if it’s the first loop.
- Return type:
Float_Edge[LoopT] | None
- remove_edge(u, v)
Removes the edge from loop u to the loop v from the graph. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int
- Raises:
KeyError – The given edge does not exist in the graph.
- remove_loop(loop)
Remove the given loop from the yarn. Reconnects any neighboring loops to form a new float with the positioned in-front-of or behind the original floats positioned accordingly. Resets the first_loop and last_loop properties if the removed loop was the tail of the yarn.
- Parameters:
loop (LoopT) – The loop to remove from the yarn.
- Raises:
KeyError – The given loop does not exist in the yarn.
- remove_loop_relative_to_floats(loop)
Removes the given loop from positions relative to floats along this yarn.
- Parameters:
loop (LoopT) – The loop to remove from positions relative to floats on this yarn.
- property sorted_loops: list[LoopT]
Returns: list[Loop]: The list of loops in the graph sorted from the earliest formed loop to the latest formed loop.
- source_loops(loop)
- Parameters:
loop (LoopT) – The loop to get the sources of.
- Returns:
The source loops of the given loop. These are the loops that are the source of all ancestors to the given loop.
- Return type:
set[LoopT]
- successors(loop)