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, knit_graph, instance=0)[source]

Bases: Yarn

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:

dict[Machine_Knit_Loop, Needle]

Parameters:
__init__(carrier, properties, knit_graph, instance=0)[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.

  • knit_graph (Optional[Knit_Graph])

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:

bool

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:

bool

property is_cut: bool

Check if yarn is no longer on a carrier (has been cut).

Returns:

True if yarn is no longer on a carrier, False otherwise.

Return type:

bool

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:

Yarn_Carrier

cut_yarn()[source]

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:

Machine_Knit_Yarn

property last_loop: Machine_Knit_Loop | None

Get the last loop in this yarn with machine-specific type checking.

Returns:

The last loop in the yarn or None if no loops exist.

Return type:

Machine_Knit_Loop | None

last_needle()[source]

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:

dict[Machine_Knit_Loop, Machine_Knit_Loop]

make_loop_on_needle(holding_needle, max_float_length=None)[source]

Add a new loop at the end of the yarn on the specified needle with configurable float length validation.

Parameters:
  • holding_needle (Needle) – The needle to make the loop on and hold it.

  • max_float_length (int | None, optional) – The maximum allowed distance between needles holding a loop. If None no float length validation is performed. Defaults to None.

Returns:

The newly created machine knit loop.

Return type:

Machine_Knit_Loop

Raises:

Use_Cut_Yarn_Exception – If attempting to use a cut yarn that is no longer on a carrier.

Warns:

Long_Float_Warning – If max_float_length is specified and the distance between this needle and the last needle exceeds the maximum.

__str__()[source]
Returns:

The string specifying the instance and carrier of this yarn.

Return type:

str

__repr__()[source]
Returns:

The string specifying the instance and carrier of this yarn.

Return type:

str

__contains__(item)

Check if a loop is contained on this yarn.

Parameters:

item (Loop | int) – The loop or loop ID being checked for in the yarn.

Returns:

True if the loop is on the yarn, False otherwise.

Return type:

bool

__getitem__(item)

Get a loop by its ID from this yarn.

Parameters:

item (int) – The loop ID to retrieve from this yarn.

Returns:

The loop on the yarn with the matching ID.

Return type:

Loop

Raises:

KeyError – If the loop ID 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:

int

__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]

__len__()

Get the number of loops on this yarn.

Returns:

The total number of loops on this yarn.

Return type:

int

add_loop_behind_float(back_loop, u, v)

Record that a loop falls behind the float between two other loops.

Parameters:
  • back_loop (Loop) – The loop that is positioned behind the float.

  • u (Loop) – The first loop in the float pair.

  • v (Loop) – The second loop in the float pair.

Return type:

None

add_loop_in_front_of_float(front_loop, u, v)

Record that a loop falls in front of the float between two other loops.

Parameters:
  • front_loop (Loop) – The loop that is positioned in front of the float.

  • u (Loop) – The first loop in the float pair.

  • v (Loop) – The second loop in the float pair.

Return type:

None

add_loop_to_end(loop)

Add an existing loop to the end of this yarn and associated knit graph.

Parameters:

loop (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:

Loop

edge_iter()

Iterate over the edges between loops on this yarn.

Returns:

An iterator over tuples of connected loops representing the yarn path.

Return type:

Iterator[tuple[Loop, Loop]]

property first_loop: Loop | 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

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_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:

bool

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:

bool

insert_loop(loop, prior_loop=None)

Insert a loop into the yarn sequence after the specified prior loop.

Parameters:
  • loop (Loop) – The loop to be added to 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).

Return type:

None

property knit_graph: None | Knit_Graph

Get the knit graph that owns this yarn.

Returns:

The knit graph that owns this yarn, or None if not associated with a graph.

Return type:

None | Knit_Graph

loops_behind_floats()

Get all float segments with loops positioned behind them.

Returns:

List of tuples containing the two loops defining each float and the set of loops positioned behind that float. Only includes floats that have loops behind them.

Return type:

list[tuple[Loop, Loop, set[Loop]]]

loops_in_front_of_floats()

Get all float segments with loops positioned in front of them.

Returns:

List of tuples containing the two loops defining each float and the set of loops positioned in front of that float. Only includes floats that have loops in front of them.

Return type:

list[tuple[Loop, Loop, set[Loop]]]

make_loop_on_end()

Create and add a new loop at the end of the yarn.

Returns:

The newly created loop that was added to the end of this yarn.

Return type:

Loop

next_loop(loop)

Get the loop that follows the specified loop on this yarn.

Parameters:

loop (Loop) – 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:

Loop | None

Raises:

KeyError – If the specified loop is not on this yarn.

prior_loop(loop)

Get the loop that precedes the specified loop on this yarn.

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.

property yarn_id: str

Get the string identifier for this yarn.

Returns:

The string representation of the yarn properties.

Return type:

str