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(is_front, position)[source]

Bases: object

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:

list[Machine_Knit_Loop]

Parameters:
__init__(is_front, position)[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.

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

property is_front: bool

Check if needle is on the front bed.

Returns:

True if needle is on front bed, False otherwise.

Return type:

bool

property position: int

Get the index position of the needle on the machine bed.

Returns:

The index on the machine bed of the needle.

Return type:

int

property has_loops: bool

Check if the needle is currently holding any loops.

Returns:

True if needle is holding loops, False otherwise.

Return type:

bool

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:

dict[Machine_Knit_Loop, Machine_Knit_Loop]

float_overlaps_needle(u, v)[source]

Check if a float between two loops overlaps this needle’s position.

Parameters:
Returns:

True if the float between u and v overlaps the position of this needle.

Return type:

bool

add_loop(loop)[source]

Add a loop to the set of currently held loops.

Parameters:

loop (Machine_Knit_Loop) – Loop to add onto needle.

Return type:

None

add_loops(loops)[source]

Add multiple loops to the held set.

Parameters:

loops (list[Machine_Knit_Loop]) – List of loops to place onto needle.

Return type:

None

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:

list[Machine_Knit_Loop]

drop()[source]

Drop all held loops by releasing them from the needle.

Returns:

The loops that were dropped.

Return type:

list[Machine_Knit_Loop]

property is_back: bool

Check if needle is on the back bed.

Returns:

True if needle is on the back bed, False otherwise.

Return type:

bool

opposite()[source]

Get the needle on the opposite bed at the same position.

Returns:

The needle on the opposite bed at the same position.

Return type:

Needle

offset(offset)[source]

Get a needle offset by the specified amount on the same bed.

Parameters:

offset (int) – The amount to offset the needle position.

Returns:

The needle offset spaces away on the same bed.

Return type:

Needle

racked_position_on_front(rack)[source]

Get the position of the needle on the front bed at a given racking.

Parameters:

rack (int) – The racking value.

Returns:

The front needle position given a racking (no change for front bed needles).

Return type:

int

main_needle()[source]

Get the non-slider needle at this needle position.

Returns:

The non-slider needle at this needle position. If this is not a slider needle, this instance is returned.

Return type:

Needle

__str__()[source]

Return string representation of the needle.

Returns:

String representation (e.g., ‘f5’ for front needle at position 5).

Return type:

str

__repr__()[source]

Return string representation of the needle.

Returns:

String representation of the needle.

Return type:

str

__hash__()[source]

Return hash value for the needle.

Returns:

Hash value based on position (negative for back needles).

Return type:

int

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

bool

Raises:

TypeError – If other is not a Needle or number.

__int__()[source]

Return integer representation of the needle position.

Returns:

The needle position.

Return type:

int

__index__()[source]

Return index representation of the needle position.

Returns:

The needle position as an index.

Return type:

int

at_racking_comparison(other, rack=0, all_needle_racking=False)[source]

Compare needle positions at a given racking.

Parameters:
  • other (Needle) – The other needle to compare positions with.

  • rack (int, optional) – Racking value to compare between. Defaults to 0.

  • all_needle_racking (bool, optional) – If true, account for front back alignment in all needle knitting. Defaults to False.

Returns:

1 if self > other, 0 if equal, -1 if self < other.

Return type:

int

Note

At an all needle racking, the front needle is always < the back needle, regardless of direction.

static needle_at_racking_cmp(n1, n2, racking=0, all_needle_racking=False)[source]

Static method to compare two needles at a given racking.

Parameters:
  • n1 (Needle) – First needle in comparison.

  • n2 (Needle) – Second needle in comparison.

  • racking (int, optional) – Racking value to compare between. Defaults to 0.

  • all_needle_racking (bool, optional) – If true, account for front back alignment in all needle knitting. Defaults to False.

Returns:

1 if n1 > n2, 0 if equal, -1 if n1 < n2.

Return type:

int

Note

At an all needle racking, the front needle is always < the back needle, regardless of direction.

__add__(other)[source]

Add another needle’s position or an integer to this needle’s position.

Parameters:

other (Needle | int) – The needle or integer to add.

Returns:

New needle with the sum position on the same bed.

Return type:

Needle

__radd__(other)[source]

Right-hand add operation.

Parameters:

other (Needle | int) – The needle or integer to add.

Returns:

New needle with the sum position on the same bed.

Return type:

Needle

__sub__(other)[source]

Subtract another needle’s position or an integer from this needle’s position.

Parameters:

other (Needle | int) – The needle or integer to subtract.

Returns:

New needle with the difference position on the same bed.

Return type:

Needle

__rsub__(other)[source]

Right-hand subtract operation.

Parameters:

other (Needle | int) – The needle or integer to subtract from.

Returns:

New needle with the difference position on the same bed.

Return type:

Needle

__lshift__(other)[source]

Left shift operation (equivalent to subtraction).

Parameters:

other (Needle | int) – The needle or integer to shift by.

Returns:

New needle shifted left (position decreased).

Return type:

Needle

__rshift__(other)[source]

Right shift operation (equivalent to addition).

Parameters:

other (Needle | int) – The needle or integer to shift by.

Returns:

New needle shifted right (position increased).

Return type:

Needle

__rlshift__(other)[source]

Right-hand left shift operation.

Parameters:

other (Needle | int) – The needle or integer to shift.

Returns:

New needle with shifted position.

Return type:

Needle

__rrshift__(other)[source]

Right-hand right shift operation.

Parameters:

other (Needle | int) – The needle or integer to shift.

Returns:

New needle with shifted position.

Return type:

Needle

__eq__(other)[source]

Check equality with another needle.

Parameters:

other (Needle) – The other needle to compare with.

Returns:

True if needles are equal (same bed, position, and slider status).

Return type:

bool

property is_slider: bool

Check if the needle is a slider needle.

Returns:

True if the needle is a slider, False otherwise.

Return type:

bool