virtual_knitting_machine.Knitting_Machine module
Module containing the Knitting_Machine class for virtual knitting machine representation and operations.
This module provides the main Knitting_Machine class which serves as the central coordinator for all knitting operations, managing needle beds, carriage movement, yarn carriers, and knit graph construction.
- class Knitting_Machine_State(*args, **kwargs)[source]
Bases:
Machine_State_With_Policy,Container,Protocol[Machine_LoopT,Carrier_State_Type]Protocol defines all the readable attributes and properties of a knitting machine used to determine its current state.
- property machine_specification: Knitting_Machine_Specification[Machine_LoopT]
Returns: Knitting_Machine_Specification[Machine_LoopT]: The specification of this machine.
- property knit_graph: Knit_Graph[Machine_LoopT]
Returns: Knit_Graph: The knit graph formed on this machine.
- property carrier_system: Yarn_Insertion_System_State[Machine_LoopT, Carrier_State_Type]
Returns: Yarn_Insertion_System_State[Carrier_State_Type]: The carrier system used by the knitting machine.
- property carriage: Carriage_State
Returns: Carriage_State: The carriage that activates needle operations on this machine.
- property front_bed: Needle_Bed_State[Machine_LoopT]
Returns: Needle_Bed_State: The front bed of needles and slider needles in this machine.
- property back_bed: Needle_Bed_State[Machine_LoopT]
Returns: Needle_Bed_State: The back bed of needles and slider needles in this machine.
- property all_needle_rack: bool
Check if racking is aligned for all needle knitting.
- Returns:
True if racking is aligned for all needle knitting, False otherwise.
- Return type:
- property rack: int
Get the current rack value of the machine.
- Returns:
The current rack value of the machine.
- Return type:
- property needle_count: int
Get the needle width of the machine.
- Returns:
The needle width of the machine.
- Return type:
- property max_rack: int
Get the maximum distance that the machine can rack.
- Returns:
The maximum distance that the machine can rack.
- Return type:
- property sliders_are_clear: bool
Check if no loops are on any slider needle and knitting can be executed.
- Returns:
True if no loops are on a slider needle and knitting can be executed, False otherwise.
- Return type:
- property slot_range: tuple[int, int]
- Returns:
The range of needle slots holding loops. The first value is the left most slot. The second value is the right most slot.
- Return type:
Notes
If there are no active loops, the slot range is (0, 0).
- front_sliders()[source]
Get list of all front bed slider needles.
- Returns:
List of slider needles on front bed.
- Return type:
- back_sliders()[source]
Get list of all back bed slider needles.
- Returns:
List of slider needles on back bed.
- Return type:
- front_slider_loops()[source]
Get list of front slider needles that currently hold loops.
- Returns:
List of front slider needles that currently hold loops.
- Return type:
- back_slider_loops()[source]
Get list of back slider needles that currently hold loops.
- Returns:
List of back slider needles that currently hold loops.
- Return type:
- all_sliders()[source]
Get list of all slider needles with front bed sliders given first.
- Returns:
List of all slider needles with front bed sliders given first.
- Return type:
- all_slider_loops()[source]
Get list of all slider needles holding loops with front bed sliders given first.
- Returns:
List of all slider needles holding loops with front bed sliders given first.
- Return type:
- get_needle_of_loop(loop)[source]
- Parameters:
loop (Machine_Knit_Loop) – The loop to search for.
- Returns:
The needle holding the loop or None if it is not held.
- Return type:
Needle | None
- has_loop(loop)[source]
- Parameters:
loop (Machine_Knit_Loop) – The loop to search for.
- Returns:
True if the loop is held on a needle. False otherwise.
- Return type:
- has_needle(needle)[source]
- Parameters:
needle (Needle_Specification) – The needle to search for.
- Returns:
True if the given needle is on a needle bed. False otherwise.
- Return type:
- property active_loops: set[Machine_LoopT]
Returns: set[Machine_Knit_Loop]: The set of loops currently held on a needle.
- active_floats()[source]
- Returns:
List of all active floats between two active loops currently held on the needle beds.
- Return type:
- loops_crossed_by_float(loop1, loop2)[source]
- Parameters:
loop1 (Machine_Knit_Loop) – First loop in the float.
loop2 (Machine_Knit_Loop) – Second loop in the float.
- Returns:
The set of machine knit loops that are held on needles crossed by the given float.
- Return type:
- get_carrier(carrier)[source]
Get the carrier or list of carriers owned by the machine at the given specification.
- Parameters:
carrier (int | Yarn_Carrier_State | Sequence[int | Yarn_Carrier_State]) – The carrier defined by a given carrier, carrier_set, integer or list of integers to form a set.
- Returns:
The carrier or list of carriers owned by the machine at the given specification.
- Return type:
Carrier_State_Type | list[Carrier_State_Type]
- get_aligned_needle(needle, aligned_slider=False)[source]
- Parameters:
needle (Needle_Specification) – The needle to find the aligned needle to.
aligned_slider (bool, optional) – If True, will return a slider needle. Defaults to False.
- Returns:
Needle aligned with the given needle at current racking.
- Return type:
Note
From Knitout Specification: Specification: at racking R, back needle index B is aligned to front needle index B+R, needles are considered aligned if they can transfer. At racking 2 it is possible to transfer from f3 to b1 using formulas F = B + R, R = F - B, B = F - R.
- __contains__(item)[source]
- Parameters:
item (Needle_Specification | Machine_Knit_Loop | Yarn_Carrier_State | Sequence[int | Yarn_Carrier_State]) – The item to search for.
- Returns:
True if the given needle or loop are on one of the needle beds or if the given carriers are all in the carrier system. Other items will return False.
- Return type:
- __getitem__(item: Machine_Knit_Loop) Needle[Machine_LoopT] | None[source]
- __getitem__(item: Slider_Needle) Slider_Needle[Machine_LoopT]
- __getitem__(item: Needle_Specification) Needle[Machine_LoopT]
- __getitem__(item: Yarn_Carrier_State) Carrier_State_Type
- __getitem__(item: Sequence[int | Yarn_Carrier_State] | Sequence[Yarn_Carrier_State] | Sequence[int]) list[Carrier_State_Type]
Access needles, carriers, or find needles holding loops on the machine.
- Parameters:
item (Needle_Specification | Yarn_Carrier_State | Yarn_Carrier_Set | Sequence[int | Yarn_Carrier_State] | Machine_Knit_Loop) – A needle, yarn carrier, carrier set, or loop to reference in the machine.
- Returns:
The needle on the machine at the given needle position, or if given yarn carrier information return the corresponding carrier or carriers on the machine, or if given a loop return the corresponding needle that holds this loop or None if the loop is not held on a needle.
- Return type:
Needle | Yarn_Carrier_State | list[Yarn_Carrier_State] | None
- Raises:
KeyError – If the item cannot be accessed from the machine.
- __len__()[source]
Get the needle bed width of the machine.
- Returns:
The needle bed width of the machine.
- Return type:
- __init__(*args, **kwargs)
- handle_violations(response_policy=None, handler=None)
Context manager that catches exceptions and routes them through the violation policy.
- Parameters:
response_policy (ViolationResponse | Violation, optional) – The ViolationResponse policy for this code block. If given a Violation, the response will be the policy assigned to that violation in the violation policy. Defaults to the response will be the default response of the violation policy.
handler (Callable[..., Any], optional) – Optional handler to attempt before falling back to the policy response.
- set_response_for(violation, response=None)
Sets the response for the given violation to the given response.
- Parameters:
violation (Violation) – The violation to set the response for.
response (ViolationResponse, optional) – The response to set for the given violation. Defaults to the default response of this policy.
- property violation_policy: Knitting_Machine_Error_Policy
Returns: Knitting_Machine_Error_Policy: The policy for handling machine state errors.
- class Knitting_Machine(machine_specification=None, knit_graph=None, violation_policy=None)[source]
Bases:
Knitting_Machine_State[Machine_LoopT,Yarn_Carrier]A virtual representation of a V-Bed WholeGarment knitting machine.
This class provides comprehensive functionality for simulating knitting operations including needle management, carriage control, yarn carrier operations, racking, and knit graph construction with support for all standard knitting operations like knit, tuck, transfer, split, and miss.
- __init__(machine_specification=None, knit_graph=None, violation_policy=None)[source]
Initialize a virtual knitting machine with specified configuration.
- Parameters:
machine_specification (Knitting_Machine_Specification, optional) – Configuration parameters for the machine. Defaults to Knitting_Machine_Specification().
knit_graph (Knit_Graph | None, optional) – Existing knit graph to use, creates new one if None. Defaults to None.
- property violation_policy: Knitting_Machine_Error_Policy
Returns: Knitting_Machine_Error_Policy: The policy for handling machine state errors.
- property machine_specification: Knitting_Machine_Specification[Machine_LoopT]
Returns: Knitting_Machine_Specification[Machine_LoopT]: The specification of this machine.
- property knit_graph: Knit_Graph[Machine_LoopT]
Returns: Knit_Graph: The knit graph formed on this machine.
- property carrier_system: Yarn_Insertion_System[Machine_LoopT]
Get the carrier system used by the knitting machine.
- Returns:
The carrier system used by the knitting machine.
- Return type:
- property carriage: Carriage
Returns: Carriage: The carriage that activates needle operations on this machine.
- property front_bed: Needle_Bed[Machine_LoopT]
Returns: Needle_Bed: The front bed of needles and slider needles in this machine.
- property back_bed: Needle_Bed[Machine_LoopT]
Returns: Needle_Bed: The back bed of needles and slider needles in this machine.
- property all_needle_rack: bool
Check if racking is aligned for all needle knitting.
- Returns:
True if racking is aligned for all needle knitting, False otherwise.
- Return type:
- property rack: int
Get the current rack value of the machine.
- Returns:
The current rack value of the machine.
- Return type:
- update_rack(front_pos, back_pos)[source]
Update the current racking to align front and back needle positions.
- static get_rack(front_pos, back_pos)[source]
Calculate racking between front and back position using formula R = F - B, F = R + B, B = F - R.
- static get_transfer_rack(start_needle, target_needle)[source]
Calculate the racking value needed to make transfer between start and target needle.
- Parameters:
start_needle (Needle_Specification) – Needle currently holding loops to transfer.
target_needle (Needle_Specification) – Needle to transfer loops to.
- Returns:
Racking value needed to make transfer between start and target needle, None if no racking can be made because needles are on the same bed.
- Return type:
- Raises:
ValueError – If the needles are on the same bed and therefor cannot be aligned by racking.
- in_hook(carrier_id)[source]
Declare that the in_hook for this yarn carrier is in use.
- Parameters:
carrier_id (int | Yarn_Carrier) – The yarn_carrier to bring in.
- out_hook(carrier_id)[source]
Declare that the yarn is no longer in service and will need to be in-hooked to use.
- Parameters:
carrier_id (int | Yarn_Carrier) – The yarn carrier to remove from service.
- bring_in(carrier_id)[source]
Bring the yarn carrier into action.
- Parameters:
carrier_id (int | Yarn_Carrier) – The yarn carrier to bring in.
- out(carrier_id)[source]
Move the yarn_carrier out of action.
- Parameters:
carrier_id (int | Yarn_Carrier) – The yarn carrier to move out.
- tuck(carrier_set, needle, direction)[source]
Place loops made with carriers in the carrier set on the given needle.
- Parameters:
carrier_set (Yarn_Carrier_Set) – Set of yarns to make loops with.
needle (Needle_Specification) – Needle to make loops on.
direction (Carriage_Pass_Direction) – The direction to tuck in.
- Returns:
List of new loops made by tucking.
- Return type:
- knit(carrier_set, needle, direction)[source]
Form new loops from the carrier set by pulling them through all loops on the given needle.
Drop the existing loops and hold the new loops on the needle.
- Parameters:
carrier_set (Yarn_Carrier_Set) – Set of yarns to make loops with.
needle (Needle_Specification) – Needle to knit on.
direction (Carriage_Pass_Direction) – The direction to knit in.
- Returns:
Tuple containing list of loops stitched through and dropped off needle by knitting process, and list of loops formed in the knitting process.
- Return type:
- Warns:
Knit_on_Empty_Needle_Warning – If attempting to knit on a needle with no loops.
- drop(needle)[source]
Drop all loops currently on given needle.
- Parameters:
needle (Needle_Specification) – The needle to drop from.
- Returns:
The list of loops dropped.
- Return type:
Note
The direction of drop operations is not recorded, just like transfer operations. This enables easy tracking of relative movements that involve carriers.
- __contains__(item)
- Parameters:
item (Needle_Specification | Machine_Knit_Loop | Yarn_Carrier_State | Sequence[int | Yarn_Carrier_State]) – The item to search for.
- Returns:
True if the given needle or loop are on one of the needle beds or if the given carriers are all in the carrier system. Other items will return False.
- Return type:
- __getitem__(item)
Access needles, carriers, or find needles holding loops on the machine.
- Parameters:
item (Needle_Specification | Yarn_Carrier_State | Yarn_Carrier_Set | Sequence[int | Yarn_Carrier_State] | Machine_Knit_Loop) – A needle, yarn carrier, carrier set, or loop to reference in the machine.
- Returns:
The needle on the machine at the given needle position, or if given yarn carrier information return the corresponding carrier or carriers on the machine, or if given a loop return the corresponding needle that holds this loop or None if the loop is not held on a needle.
- Return type:
Needle | Yarn_Carrier_State | list[Yarn_Carrier_State] | None
- Raises:
KeyError – If the item cannot be accessed from the machine.
- __len__()
Get the needle bed width of the machine.
- Returns:
The needle bed width of the machine.
- Return type:
- active_floats()
- Returns:
List of all active floats between two active loops currently held on the needle beds.
- Return type:
- property active_loops: set[Machine_LoopT]
Returns: set[Machine_Knit_Loop]: The set of loops currently held on a needle.
- all_loops()
Get list of all needles holding loops with front bed needles given first.
- all_needles()
Get list of all needles with front bed needles given first.
- all_slider_loops()
Get list of all slider needles holding loops with front bed sliders given first.
- Returns:
List of all slider needles holding loops with front bed sliders given first.
- Return type:
- all_sliders()
Get list of all slider needles with front bed sliders given first.
- Returns:
List of all slider needles with front bed sliders given first.
- Return type:
- back_loops()
Get list of back bed needles that currently hold loops.
- back_needles()
Get list of all back bed needles.
- back_slider_loops()
Get list of back slider needles that currently hold loops.
- Returns:
List of back slider needles that currently hold loops.
- Return type:
- back_sliders()
Get list of all back bed slider needles.
- Returns:
List of slider needles on back bed.
- Return type:
- front_loops()
Get list of front bed needles that currently hold loops.
- front_needles()
Get list of all front bed needles.
- front_slider_loops()
Get list of front slider needles that currently hold loops.
- Returns:
List of front slider needles that currently hold loops.
- Return type:
- front_sliders()
Get list of all front bed slider needles.
- Returns:
List of slider needles on front bed.
- Return type:
- get_aligned_needle(needle, aligned_slider=False)
- Parameters:
needle (Needle_Specification) – The needle to find the aligned needle to.
aligned_slider (bool, optional) – If True, will return a slider needle. Defaults to False.
- Returns:
Needle aligned with the given needle at current racking.
- Return type:
Note
From Knitout Specification: Specification: at racking R, back needle index B is aligned to front needle index B+R, needles are considered aligned if they can transfer. At racking 2 it is possible to transfer from f3 to b1 using formulas F = B + R, R = F - B, B = F - R.
- get_back_needle(position, is_slider=False)
- get_carrier(carrier)
Get the carrier or list of carriers owned by the machine at the given specification.
- Parameters:
carrier (int | Yarn_Carrier_State | Sequence[int | Yarn_Carrier_State]) – The carrier defined by a given carrier, carrier_set, integer or list of integers to form a set.
- Returns:
The carrier or list of carriers owned by the machine at the given specification.
- Return type:
Carrier_State_Type | list[Carrier_State_Type]
- get_front_needle(position, is_slider=False)
- get_needle_of_loop(loop)
- Parameters:
loop (Machine_Knit_Loop) – The loop to search for.
- Returns:
The needle holding the loop or None if it is not held.
- Return type:
Needle | None
- get_specified_needle(is_front, position, is_slider=False)
- handle_violations(response_policy=None, handler=None)
Context manager that catches exceptions and routes them through the violation policy.
- Parameters:
response_policy (ViolationResponse | Violation, optional) – The ViolationResponse policy for this code block. If given a Violation, the response will be the policy assigned to that violation in the violation policy. Defaults to the response will be the default response of the violation policy.
handler (Callable[..., Any], optional) – Optional handler to attempt before falling back to the policy response.
- has_loop(loop)
- Parameters:
loop (Machine_Knit_Loop) – The loop to search for.
- Returns:
True if the loop is held on a needle. False otherwise.
- Return type:
- has_needle(needle)
- Parameters:
needle (Needle_Specification) – The needle to search for.
- Returns:
True if the given needle is on a needle bed. False otherwise.
- Return type:
- loops_crossed_by_float(loop1, loop2)
- Parameters:
loop1 (Machine_Knit_Loop) – First loop in the float.
loop2 (Machine_Knit_Loop) – Second loop in the float.
- Returns:
The set of machine knit loops that are held on needles crossed by the given float.
- Return type:
- property max_rack: int
Get the maximum distance that the machine can rack.
- Returns:
The maximum distance that the machine can rack.
- Return type:
- property needle_count: int
Get the needle width of the machine.
- Returns:
The needle width of the machine.
- Return type:
- set_response_for(violation, response=None)
Sets the response for the given violation to the given response.
- Parameters:
violation (Violation) – The violation to set the response for.
response (ViolationResponse, optional) – The response to set for the given violation. Defaults to the default response of this policy.
- property sliders_are_clear: bool
Check if no loops are on any slider needle and knitting can be executed.
- Returns:
True if no loops are on a slider needle and knitting can be executed, False otherwise.
- Return type:
- property slot_range: tuple[int, int]
- Returns:
The range of needle slots holding loops. The first value is the left most slot. The second value is the right most slot.
- Return type:
Notes
If there are no active loops, the slot range is (0, 0).
- valid_rack(front_pos, back_pos)
Check if transfer can be completed at current racking.
- xfer(starting_needle, to_slider=False, from_split=False)[source]
Move all loops on starting_needle to aligned needle at current racking.
- Parameters:
starting_needle (Needle_Specification) – Needle to move loops from.
to_slider (bool, optional) – If True, loops are moved to a slider. Defaults to False.
from_split (bool, optional) – If True, this transfer is part of a split and does not move the carriage. Defaults to False.
- Returns:
The list of loops that are transferred.
- Return type:
- split(carrier_set, starting_needle, direction)[source]
Pull a loop formed in direction by the yarns in carriers through the loops on needle.
Transfer the old loops to opposite-bed needle in the process.
- Parameters:
carrier_set (Yarn_Carrier_Set) – Set of yarns to make loops with.
starting_needle (Needle_Specification) – The needle to transfer old loops from and to form new loops on.
direction (Carriage_Pass_Direction) – The carriage direction for the split operation.
- Returns:
Tuple containing the list of loops created by the split and the list of loops transferred.
- Return type:
Note
From the Knitout Documentation: Splitting with an empty carrier set will transfer. This transfers loops on starting needle to aligned needle at this racking then forms new loops pulled through the transferred loops and holds them on the starting needle.
- miss(carrier_set, needle, direction)[source]
Set the carrier positions to hover above the given needle.
- Parameters:
carrier_set (Yarn_Carrier_Set) – Set of yarns to move.
needle (Needle_Specification) – Needle to position the carriers from.
direction (Carriage_Pass_Direction) – The carriage direction for the miss operation.