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(yarn: Yarn[Self], loop_id: int | None = None, **_kwargs: Any)[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[Self]

parent_loops

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

Type:

list[Loop]

__init__(yarn: Yarn[Self], loop_id: int | None = None, **_kwargs: Any) None[source]

Construct a Loop object with the specified identifier and yarn.

Parameters:
  • yarn (Yarn[Self]) – The yarn that creates and holds this loop.

  • loop_id (int, optional) – A unique identifier for the loop, must be non-negative. Defaults to the next id of the knitgraph that owns the given yarn.

property loop_id: int

Get the unique identifier of this loop.

Returns:

The id of the loop.

Return type:

int

property has_parent_loops: bool

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

property parent_loop_ids: list[int]

Returns: list[int]: The ids of the parent loops of this loop in their stacking order.

property pull_directions: list[Pull_Direction]

Returns: list[Pull_Direction]: The pull direction of the stitches formed by this loop and its parents in stacking order of its parents.

property pull_direction: Pull_Direction

Returns: Pull_Direction: The majority pull-direction of the stitches formed with the parent loops or Back-To-Front (knit-stitch) if this loop has no parents.

property prior_loop_on_yarn: Self | None

Returns: Loop | None: The prior loop on the yarn, or None if this is the first loop on the yarn.

property next_loop_on_yarn: Self | None

Returns: Loop | None: The next loop on the yarn, or None if this is the last loop on the yarn.

property started_float: Float_Edge[Self] | None

Returns: Float_Edge | None: The float-edge connecting this loop to the next loop on its yarn or None if it is at the end of the yarn.

property ended_float: Float_Edge[Self] | None

Returns: Float_Edge | None: The float-edge connecting the prior loop on the yarn to this loop or None if it is at the beginning of the yarn.

property loops_in_front_of_floats: set[Self]

Returns: set[Self]: The set of all loops in front of a float that involves this loop.

property loops_behind_floats: set[Self]

Returns: set[Self]: The set of all loops behind a float that involves this loop.

property cross_floats: set[Self]

Returns: set[Self]: The set of all loops that cross a float that involves this loop.

is_in_front_of_float(u: Self, v: Self) bool[source]
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: Self, v: Self) bool[source]
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

static majority_pull_direction(loops: Sequence[Loop]) Pull_Direction[source]
Parameters:

loops (Sequence[Loop]) – The loops to find the majority pull direction of.

Returns:

The majority pull direction of the given loops or Back-to-Front (i.e., knit-stitch) there are no loops.

Return type:

Pull_Direction

add_loop_behind_started_float(back_loop: Self) None[source]

Places the given loop behind the float started by this loop. If this loop is at the end of the yarn, nothing happens.

Parameters:

back_loop (Loop) – The loop to place the loop behind the float started by this loop.

add_loop_in_front_of_started_float(front_loop: Self) None[source]

Places the given loop in front of the float started by this loop. If this loop is at the end of the yarn, nothing happens. :param front_loop: The loop to place the loop in front of the float started by this loop. :type front_loop: Loop

add_parent_loop(parent: Self, stack_position: int | None = None) 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.

remove_parent(parent: Self) None[source]

Removes the given parent loop from the set of parents of this loop. If the given loop is not a parent of this loop, nothing happens. :param parent: The parent loop to remove. :type parent: Loop

__hash__() int[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__() int[source]

Convert loop to integer representation using loop_id.

Returns:

The loop_id as an integer.

Return type:

int

__eq__(other: object) bool[source]

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

Parameters:

other (Self) – 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: Loop | int) bool[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__() str[source]

Return string representation of the loop.

Returns:

String representation showing “Loop {loop_id}”.

Return type:

str