virtual_knitting_machine.machine_components.needles.Needle module
A module containing the Needle class and related functions for virtual knitting machine operations.
This module provides the core Needle class which represents individual needles on a knitting machine. Needles can be on the front or back bed and can hold loops for knitting operations. The module includes functionality for loop management, needle positioning, and various knitting operations.
- class Needle_Specification(*args, **kwargs)[source]
Bases:
ProtocolProtocol for the positional details of a Needle object.
- property pull_direction: Pull_Direction
Get the direction this needle pulls loops during knit operations.
- Returns:
BtF (Back to Front) for front needles, FtB (Front to Back) for back needles.
- Return type:
Pull_Direction
- slot_by_racking(racking)[source]
The slot number indicates the front-bed alignment of the given needle. For front bed needles this is equivalent to the position. For back bed needles this is dependent on the racking alignment of the beds.
- Parameters:
racking (int) – The racking alignment to determine the slot number from.
- Returns:
The slot number of the given component for the given racking.
- Return type:
Notes
Racking Calculations: * R = F - B * F = R + B * B = F - R.
- __str__()[source]
Return string representation of the needle.
- Returns:
String representation (e.g., ‘f5’ for front needle at position 5, ‘bs2’ for back slider at position 2).
- Return type:
- __repr__()[source]
Return string representation of the needle.
- Returns:
String representation of the needle.
- Return type:
- __hash__()[source]
Return hash value for the needle.
- Returns:
Hash value based on position, bed side, and slider state.
- Return type:
- __lt__(other)[source]
Compare if this needle is less than another needle or number.
- Parameters:
other (Needle | int | float) – The other needle or number to compare with.
- Returns:
True if this needle’s position is less than the other value. If the needles are at the same location but in opposite positions (back vs. front), the front needle is considered less than the back. This orders needles as front-to-back in a leftward carriage pass.
- Return type:
- __add__(other)[source]
Add another needle’s position or an integer to this needle’s position.
- Parameters:
other (Needle_Specification | int) – The needle or integer to add.
- Returns:
New needle with the sum position on the same bed.
- Return type:
- __sub__(other)[source]
Subtract another needle’s position or an integer from this needle’s position.
- 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]): ….
- __init__(*args, **kwargs)
- class Needle_Position(is_front, position, is_slider)[source]
Bases:
Needle_Specification- __add__(other)
Add another needle’s position or an integer to this needle’s position.
- Parameters:
other (Needle_Specification | int) – The needle or integer to add.
- Returns:
New needle with the sum position on the same bed.
- Return type:
- 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]): ….
- __eq__(other)
Check equality with another needle.
- __hash__()
Return hash value for the needle.
- Returns:
Hash value based on position, bed side, and slider state.
- Return type:
- __lt__(other)
Compare if this needle is less than another needle or number.
- Parameters:
other (Needle | int | float) – The other needle or number to compare with.
- Returns:
True if this needle’s position is less than the other value. If the needles are at the same location but in opposite positions (back vs. front), the front needle is considered less than the back. This orders needles as front-to-back in a leftward carriage pass.
- Return type:
- __radd__(other)
Right-hand add operation.
- __repr__()
Return string representation of the needle.
- Returns:
String representation of the needle.
- Return type:
- __str__()
Return string representation of the needle.
- Returns:
String representation (e.g., ‘f5’ for front needle at position 5, ‘bs2’ for back slider at position 2).
- Return type:
- __sub__(other)
Subtract another needle’s position or an integer from this needle’s position.
- property pull_direction: Pull_Direction
Get the direction this needle pulls loops during knit operations.
- Returns:
BtF (Back to Front) for front needles, FtB (Front to Back) for back needles.
- Return type:
Pull_Direction
- slot_by_racking(racking)
The slot number indicates the front-bed alignment of the given needle. For front bed needles this is equivalent to the position. For back bed needles this is dependent on the racking alignment of the beds.
- Parameters:
racking (int) – The racking alignment to determine the slot number from.
- Returns:
The slot number of the given component for the given racking.
- Return type:
Notes
Racking Calculations: * R = F - B * F = R + B * B = F - R.
- class Needle(is_front, position, knitting_machine)[source]
Bases:
Needle_Specification,Slotted_Position[Machine_LoopT]A class for managing individual needles on a knitting machine.
This class represents a needle on either the front or back bed of a knitting machine. Each needle can hold multiple loops and provides methods for knitting operations, loop transfers, and position calculations.
- held_loops
List of loops currently held by this needle.
- Type:
- __init__(is_front, position, knitting_machine)[source]
Initialize a new needle.
- Parameters:
is_front (bool) – True if this is a front bed needle, False for back bed.
position (int) – The needle index/position on the machine bed.
knitting_machine (Knitting_Machine_State) – The machine that owns this needle.
- property position_in_sheet: int
Returns: int: The position of this needle in the sheet in the current gauged-schema.
- property knitting_machine: Knitting_Machine_State[Machine_LoopT, Any]
Returns: Knitting_Machine_State: The knitting machine that owns this component.
- property gauge_neighbors: list[Self]
Returns: list[Sheet_Needle]: List of needles that neighbor this loop in sheets at the current gauge layering.
- property slot_number: int
The slot number indicates the front-bed alignment of the given needle. For front bed needles this is equivalent to the position. For back bed needles this is dependent on the racking alignment of the beds.
- Returns:
The slot number of the given component for the given racking.
- Return type:
Notes
Racking Calculations: * R = F - B * F = R + B * B = F - R.
- property has_loops: bool
Check if the needle is currently holding any loops.
- Returns:
True if needle is holding loops, False otherwise.
- Return type:
- property needle_position: Needle_Position
Returns: Needle_Position: The Needle_Position of this needle, not associated with the knitting machine.
- main_needle()[source]
Get the non-slider needle at this needle position.
- Returns:
The non-slider needle at this needle position.
- Return type:
- active_floats()[source]
Get active floats connecting to loops on this needle.
- 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 yarns in the system.
- Return type:
- float_overlaps_needle(u, v)[source]
Check if a float between two loops overlaps this needle’s position.
- Parameters:
u (Machine_Knit_Loop) – Machine_Knit_Loop at start of float.
v (Machine_Knit_Loop) – Machine_Knit_Loop at end of float.
- Returns:
True if the float between u and v overlaps the position of this needle.
- Return type:
- add_loop(loop)[source]
Add a loop to the set of currently held loops.
- Parameters:
loop (Machine_Knit_Loop) – Loop to add onto needle.
- add_loops(loops)[source]
Add multiple loops to the held set.
- Parameters:
loops (Sequence[Machine_Knit_Loop]) – List of loops to place onto needle.
- transfer_loops(target_needle)[source]
Transfer all loops from this needle to a target needle.
- Parameters:
target_needle (Needle) – Needle to transfer loops to.
- Returns:
Loops that were transferred.
- Return type:
- drop()[source]
Drop all held loops by releasing them from the needle.
- Returns:
The loops that were dropped.
- Return type:
- static iterates_over_needles(needles)[source]
- Parameters:
needles (Iterable[Needle_Specification]) – The needles to iterate over and determine that they are all Needles.
- Returns:
Returns true if all elements in needles are needle objects. Typing system will recognize this guarantee.
- Return type:
- __add__(other)
Add another needle’s position or an integer to this needle’s position.
- Parameters:
other (Needle_Specification | int) – The needle or integer to add.
- Returns:
New needle with the sum position on the same bed.
- Return type:
- 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]): ….
- __eq__(other)
Check equality with another needle.
- __hash__()
Return hash value for the needle.
- Returns:
Hash value based on position, bed side, and slider state.
- Return type:
- __lt__(other)
Compare if this needle is less than another needle or number.
- Parameters:
other (Needle | int | float) – The other needle or number to compare with.
- Returns:
True if this needle’s position is less than the other value. If the needles are at the same location but in opposite positions (back vs. front), the front needle is considered less than the back. This orders needles as front-to-back in a leftward carriage pass.
- Return type:
- __radd__(other)
Right-hand add operation.
- __repr__()
Return string representation of the needle.
- Returns:
String representation of the needle.
- Return type:
- __str__()
Return string representation of the needle.
- Returns:
String representation (e.g., ‘f5’ for front needle at position 5, ‘bs2’ for back slider at position 2).
- Return type:
- __sub__(other)
Subtract another needle’s position or an integer from this needle’s position.
- aligned_with_slot(slot)
- Parameters:
slot (int | Slotted_Position) – The slot to compare to.
- Returns:
True if this is positioned at the same slot, False otherwise.
- Return type:
- direction_to_slot(slot)
- Parameters:
slot (int) – The slot to find a direction towards.
- Returns:
The direction of carriage movement from this component to the slot or None if the slots are aligned.
- Return type:
Carriage_Pass_Direction | None
- 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.
- in_pass_direction(slot, direction)
- Parameters:
slot (int | Slotted_Position) – The slot following this slot in a carriage pass movement.
direction (Carriage_Pass_Direction) – The direction to test.
- Returns:
True if the given slot is after this slot in the given direction. False otherwise.
- Return type:
- left_of_slot(slot)
- Parameters:
slot (int | Slotted_Position) – The slot to compare to.
- Returns:
True if this is positioned left of the given slot, False otherwise.
- Return type:
- property machine_is_all_needle_racked: bool
Returns: bool: True if the knitting machine is racked for all needle knitting.
- property machine_specification: Knitting_Machine_Specification[Machine_LoopT]
Returns: Knitting_Machine_Specification[Machine_LoopT]: The machine specification of the machine associated with this component.
- property needle_count_of_machine: int
Returns: int: The number of needles on each bed of the knitting machine. Defaults to the bed size in the knitting machine specification.
- property pull_direction: Pull_Direction
Get the direction this needle pulls loops during knit operations.
- Returns:
BtF (Back to Front) for front needles, FtB (Front to Back) for back needles.
- Return type:
Pull_Direction
- right_of_slot(slot)
- Parameters:
slot (int | Slotted_Position) – The slot to compare to.
- Returns:
True if this is positioned right of the given slot, False otherwise.
- Return type:
- property rightmost_slot_on_machine: int
Returns: int: The rightmost slot on this knitting machine. Defaults to the standard bed size in the knitting machine specification.
- 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.
- slot_by_racking(racking)
The slot number indicates the front-bed alignment of the given needle. For front bed needles this is equivalent to the position. For back bed needles this is dependent on the racking alignment of the beds.
- Parameters:
racking (int) – The racking alignment to determine the slot number from.
- Returns:
The slot number of the given component for the given racking.
- Return type:
Notes
Racking Calculations: * R = F - B * F = R + B * B = F - R.
- property violation_policy: Knitting_Machine_Error_Policy
Returns: Knitting_Machine_Error_Policy: The policy for handling machine state errors.