knit_graphs.Loop module

Module containing the Loop Class.

This module defines the Loop class which represents individual loops in a knitting pattern. Loops are the fundamental building blocks of knitted structures and maintain relationships with parent loops, yarn connections, and float positions.

class Loop(loop_id, yarn)[source]

Bases: object

A class to represent a single loop structure for modeling a single loop in a knitting pattern.

The Loop class manages yarn relationships, parent-child connections for stitches, and float positioning for complex knitting structures. Each loop maintains its position in the yarn sequence and its relationships to other loops through stitch connections and floating elements.

yarn

The yarn that creates and holds this loop.

Type:

Yarn

parent_loops

The list of parent loops that this loop is connected to through stitch edges.

Type:

list[Loop]

front_floats

A dictionary tracking loops that this loop floats in front of.

Type:

dict[Loop, set[Loop]]

back_floats

A dictionary tracking loops that this loop floats behind.

Type:

dict[Loop, set[Loop]]

Parameters:
__init__(loop_id, yarn)[source]

Construct a Loop object with the specified identifier and yarn.

Parameters:
  • loop_id (int) – A unique identifier for the loop, must be non-negative.

  • yarn (Yarn) – The yarn that creates and holds this loop.

add_loop_in_front_of_float(u, v)[source]

Set this loop to be in front of the float between loops u and v.

This method establishes that this loop passes in front of a floating yarn segment between two other loops.

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

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

Return type:

None

add_loop_behind_float(u, v)[source]

Set this loop to be behind the float between loops u and v.

This method establishes that this loop passes behind a floating yarn segment between two other loops.

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

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

Return type:

None

is_in_front_of_float(u, v)[source]

Check if this loop is positioned in front of the float between loops u and v.

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

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

Returns:

True if the float between u and v passes behind this loop, False otherwise.

Return type:

bool

is_behind_float(u, v)[source]

Check if this loop is positioned behind the float between loops u and v.

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

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

Returns:

True if the float between u and v passes in front of this loop, False otherwise.

Return type:

bool

prior_loop_on_yarn()[source]

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

Returns:

The prior loop on the yarn, or None if this is the first loop on the yarn.

Return type:

Loop | None

next_loop_on_yarn()[source]

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

Returns:

The next loop on the yarn, or None if this is the last loop on the yarn.

Return type:

Loop

has_parent_loops()[source]

Check if this loop has any parent loops connected through stitch edges.

Returns:

True if the loop has stitch-edge parents, False otherwise.

Return type:

bool

add_parent_loop(parent, stack_position=None)[source]

Add a parent loop to this loop’s parent stack.

Parameters:
  • parent (Loop) – The loop to be added as a parent to this loop.

  • stack_position (int | None, optional) – The position to insert the parent into the parent stack. If None, adds the parent on top of the stack. Defaults to None.

Return type:

None

property loop_id: int

Get the unique identifier of this loop.

Returns:

The id of the loop.

Return type:

int

__hash__()[source]

Return hash value based on loop_id for use in sets and dictionaries.

Returns:

Hash value of the loop_id.

Return type:

int

__int__()[source]

Convert loop to integer representation using loop_id.

Returns:

The loop_id as an integer.

Return type:

int

__eq__(other)[source]

Check equality with another base loop based on loop_id and type.

Parameters:

other (Loop) – The other loop to compare with.

Returns:

True if both loops have the same class and loop_id, False otherwise.

Return type:

bool

__lt__(other)[source]

Compare loop_id with another loop or integer for ordering.

Parameters:

other (Loop | int) – The other loop or integer to compare with.

Returns:

True if this loop’s id is less than the other’s id.

Return type:

bool

__repr__()[source]

Return string representation of the loop.

Returns:

String representation showing “Loop {loop_id}”.

Return type:

str