virtual_knitting_machine.machine_components.Needle_Bed module

Representation module for needle beds on knitting machines. This module provides the Needle_Bed class which represents one bed of needles (front or back) on a knitting machine, managing both regular needles and slider needles with their associated loops and operations.

class Needle_Bed_State(*args, **kwargs)[source]

Bases: Machine_State_With_Policy, Protocol[Machine_LoopT]

Protocol for the readable properties of a needle bed.

property knitting_machine: Knitting_Machine_State[Machine_LoopT, Any]

Returns: Knitting_Machine_State: The knitting machine this bed belongs to.

property violation_policy: Knitting_Machine_Error_Policy

Returns: Knitting_Machine_Error_Policy: The policy for handling machine state errors.

property is_front: bool

Check if this is the front bed.

Returns:

True if this is the front bed, False if back bed.

Return type:

bool

property needles: list[Needle[Machine_LoopT]]

Returns: list[Needle]: The needles on this bed ordered from 0 to the needle count specified by the knitting machine.

property sliders: list[Slider_Needle[Machine_LoopT]]

Returns: list[Slider_Needle]: The slider needles on this bed ordered from 0 to the needle count specified by the knitting machine.”

property is_back: bool

Returns: bool: True if this is the back bed, False if front bed.

property needle_count: int

Returns: int: The number of needles on the bed.

property loop_holding_needles: list[Needle[Machine_LoopT]]

Returns: list[Needle]: List of needles on bed that actively hold loops.

property loop_holding_sliders: list[Slider_Needle[Machine_LoopT]]

Returns: list[Slider_Needle]: List of sliders on bed that actively hold loops.

property active_loops: set[Machine_LoopT]

Returns: set[Machine_Knit_Loop]: The set of loops held on needles on the needle bed.

property active_slider_loops: set[Machine_LoopT]

Returns: set[Machine_Knit_Loop]: The set of loops held on slider needles on the needle bed.

property active_needle_count: int

Returns: int: The number of active needles that hold loops.

property active_slider_count: int

Returns: int: The number of active sliders that hold loops.

property sliders_are_clear: bool

Check if no loops are on any slider needle.

Returns:

True if no loops are on a slider needle, False otherwise.

Return type:

bool

loop_is_active(loop)[source]
Parameters:

loop (Machine_Knit_Loop) – The loop to check if it is held by this bed.

Returns:

True if the given loop is on held on a needle or slider needle, False otherwise.

Return type:

bool

loop_on_slider(loop)[source]
Parameters:

loop (Machine_Knit_Loop) – The loop to check if it is actively on a slider needle

Returns:

True if the loop is on held on a slider needle. False, otherwise.

Return type:

bool

loop_on_needle(loop)[source]
Parameters:

loop (Machine_Knit_Loop) – The loop to check if it is actively held by a needle

Returns:

True if the loop is held by a needle. False, otherwise.

Return type:

bool

get_needle_of_loop(loop)[source]
Parameters:

loop (Machine_Knit_Loop) – The loop being searched for.

Returns:

None if the bed does not hold the loop, otherwise the needle position that holds it.

Return type:

Needle | None

slider_is_active(slider)[source]
Parameters:

slider (int | Needle_Specification) – The slider or index of a slider on this needle bed.

Returns:

True if the given slider is on this bed and holds at least one loop, False otherwise.

Return type:

bool

needle_is_active(needle)[source]
Parameters:

needle (int | Needle_Specification) – The needle or index of a needl on this needle bed.

Returns:

True if the given needle is on this bed and holds at least one loop, False otherwise.

Return type:

bool

__len__()[source]
Returns:

Number of needles on the bed.

Return type:

int

__iter__()[source]

Iterate over the needles in this bed.

Returns:

Iterator over the needles on this bed.

Return type:

Iterator[Needle]

__contains__(item)[source]
Parameters:

item (Machine_Knit_Loop | Needle_Specification | int) – The value to find in the needle bed.

Returns:

True if the item is in the bed, False otherwise. Integers are checked against the range of the needle bed. Needles are checked against range and bed position. Loops are checked to see if they are being held on this bed.

Return type:

bool

__getitem__(item: Machine_Knit_Loop) Needle[Machine_LoopT] | None[source]
__getitem__(item: Needle_Specification | int) Needle[Machine_LoopT]
__getitem__(item: slice) list[Needle[Machine_LoopT]]

Get an indexed needle on the bed, or find needle holding a specific loop.

Parameters:

item (Machine_Knit_Loop | Needle_Specification | slice | int) – The needle position to get, loop to find needle for, or slice for multiple needles.

Returns:

The needle(s) at the specified position(s) or holding the specified loop.

Return type:

Needle | list[Needle] | None

Raises:

KeyError – If needle position is out of range or the loop is not held on this bed.

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)
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.

class Needle_Bed(is_front, knitting_machine)[source]

Bases: Needle_Bed_State[Machine_LoopT]

A structure to hold information about loops held on one bed of needles where increasing indices indicate needles moving from left to right (LEFT -> 0 1 2….N <- RIGHT of Machine). This class manages both regular needles and slider needles, tracks active sliders, and provides methods for loop manipulation and needle access operations.

__init__(is_front, knitting_machine)[source]

Initialize a needle bed representation for the machine.

Parameters:
  • is_front (bool) – True if this is the front bed, False if it is the back bed.

  • knitting_machine (Knitting_Machine) – The knitting machine this bed belongs to.

property knitting_machine: Knitting_Machine[Machine_LoopT]

Returns: Knitting_Machine: The knitting machine this bed belongs to.

property is_front: bool

Check if this is the front bed.

Returns:

True if this is the front bed, False if back bed.

Return type:

bool

property needles: list[Needle[Machine_LoopT]]

Returns: list[Needle]: The needles on this bed ordered from 0 to the needle count specified by the knitting machine.

property sliders: list[Slider_Needle[Machine_LoopT]]

Returns: list[Slider_Needle]: The slider needles on this bed ordered from 0 to the needle count specified by the knitting machine.”

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)
Parameters:

item (Machine_Knit_Loop | Needle_Specification | int) – The value to find in the needle bed.

Returns:

True if the item is in the bed, False otherwise. Integers are checked against the range of the needle bed. Needles are checked against range and bed position. Loops are checked to see if they are being held on this bed.

Return type:

bool

__getitem__(item)

Get an indexed needle on the bed, or find needle holding a specific loop.

Parameters:

item (Machine_Knit_Loop | Needle_Specification | slice | int) – The needle position to get, loop to find needle for, or slice for multiple needles.

Returns:

The needle(s) at the specified position(s) or holding the specified loop.

Return type:

Needle | list[Needle] | None

Raises:

KeyError – If needle position is out of range or the loop is not held on this bed.

__iter__()

Iterate over the needles in this bed.

Returns:

Iterator over the needles on this bed.

Return type:

Iterator[Needle]

__len__()
Returns:

Number of needles on the bed.

Return type:

int

property active_loops: set[Machine_LoopT]

Returns: set[Machine_Knit_Loop]: The set of loops held on needles on the needle bed.

property active_needle_count: int

Returns: int: The number of active needles that hold loops.

property active_slider_count: int

Returns: int: The number of active sliders that hold loops.

property active_slider_loops: set[Machine_LoopT]

Returns: set[Machine_Knit_Loop]: The set of loops held on slider needles on the needle bed.

get_needle_of_loop(loop)
Parameters:

loop (Machine_Knit_Loop) – The loop being searched for.

Returns:

None if the bed does not hold the loop, otherwise the needle position that holds it.

Return type:

Needle | 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.

property is_back: bool

Returns: bool: True if this is the back bed, False if front bed.

property loop_holding_needles: list[Needle[Machine_LoopT]]

Returns: list[Needle]: List of needles on bed that actively hold loops.

property loop_holding_sliders: list[Slider_Needle[Machine_LoopT]]

Returns: list[Slider_Needle]: List of sliders on bed that actively hold loops.

loop_is_active(loop)
Parameters:

loop (Machine_Knit_Loop) – The loop to check if it is held by this bed.

Returns:

True if the given loop is on held on a needle or slider needle, False otherwise.

Return type:

bool

loop_on_needle(loop)
Parameters:

loop (Machine_Knit_Loop) – The loop to check if it is actively held by a needle

Returns:

True if the loop is held by a needle. False, otherwise.

Return type:

bool

loop_on_slider(loop)
Parameters:

loop (Machine_Knit_Loop) – The loop to check if it is actively on a slider needle

Returns:

True if the loop is on held on a slider needle. False, otherwise.

Return type:

bool

property needle_count: int

Returns: int: The number of needles on the bed.

needle_is_active(needle)
Parameters:

needle (int | Needle_Specification) – The needle or index of a needl on this needle bed.

Returns:

True if the given needle is on this bed and holds at least one loop, False otherwise.

Return type:

bool

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.

slider_is_active(slider)
Parameters:

slider (int | Needle_Specification) – The slider or index of a slider on this needle bed.

Returns:

True if the given slider is on this bed and holds at least one loop, False otherwise.

Return type:

bool

property sliders_are_clear: bool

Check if no loops are on any slider needle.

Returns:

True if no loops are on a slider needle, False otherwise.

Return type:

bool

property violation_policy: Knitting_Machine_Error_Policy

Returns: Knitting_Machine_Error_Policy: The policy for handling machine state errors.

add_loops(needle, loops, drop_prior_loops=True)[source]

Add loops to a given needle, optionally dropping existing loops as if a knit operation took place.

Parameters:
  • needle (Needle) – The needle to add the loops on.

  • loops (list[Machine_Knit_Loop]) – The loops to put on the needle if not creating with the yarn carrier.

  • drop_prior_loops (bool, optional) – If True, any loops currently held on this needle are dropped. Defaults to True.

Returns:

Returns the list of loops made with the carrier on this needle.

Return type:

list[Machine_Knit_Loop]

Warns:

Needle_Holds_Too_Many_Loops – If adding these loops would exceed maximum loop count.

drop(needle)[source]

Clear the loops held at this position as though a drop operation has been done.

Parameters:

needle (Needle_Specification) – The position to drop loops from main and slider needles.

Returns:

List of loops that were dropped.

Return type:

list[Machine_Knit_Loop]