knit_graphs.Yarn module
The Yarn Data Structure.
This module contains the Yarn class and Yarn_Properties dataclass which together represent the physical yarn used in knitting patterns. The Yarn class manages the sequence of loops along a yarn and their floating relationships.
- class Yarn_Properties(name='yarn', plies=2, weight=28, color='green')[source]
Bases:
object
Dataclass structure for maintaining relevant physical properties of a yarn.
This frozen dataclass contains all the physical and visual properties that characterize a yarn, including its structure, weight, and appearance.
- __str__()[source]
Get a formatted string representation of the yarn properties.
- Returns:
String representation in format “name(plies-weight,color)”.
- Return type:
- __repr__()[source]
Get the name of the yarn for debugging purposes.
- Returns:
The name of the yarn.
- Return type:
- static default_yarn()[source]
Create a default yarn properties instance.
- Returns:
A default yarn instance with standard properties.
- Return type:
- __eq__(other)[source]
Check equality with another Yarn_Properties instance.
- Parameters:
other (Yarn_Properties) – The other yarn properties to compare with.
- Returns:
True if all properties (name, plies, weight, color) are equal, False otherwise.
- Return type:
- class Yarn(yarn_properties=None, knit_graph=None)[source]
Bases:
object
A class to represent a yarn structure as a sequence of connected loops.
The Yarn class manages a directed graph of loops representing the physical yarn path through a knitted structure. It maintains the sequential order of loops and their floating relationships, providing methods for navigation and manipulation of the yarn structure.
- loop_graph
The directed graph loops connected by yarn-wise float edges.
- Type:
DiGraph
- properties
The physical and visual properties of this yarn.
- Type:
- Parameters:
yarn_properties (
Optional
[Yarn_Properties
])knit_graph (
Optional
[Knit_Graph
])
- __init__(yarn_properties=None, knit_graph=None)[source]
Initialize a yarn with the specified properties and optional knit graph association.
- Parameters:
yarn_properties (None | Yarn_Properties, optional) – The properties defining this yarn. If None, uses default properties. Defaults to standard properties.
knit_graph (None | Knit_Graph, optional) – The knit graph that will own this yarn. Can be None for standalone yarns. Defaults to None.
- property knit_graph: None | Knit_Graph
Get the knit graph that owns this yarn.
- Returns:
The knit graph that owns this yarn, or None if not associated with a graph.
- Return type:
None | Knit_Graph
- add_loop_in_front_of_float(front_loop, u, v)[source]
Record that a loop falls in front of the float between two other loops.
- add_loop_behind_float(back_loop, u, v)[source]
Record that a loop falls behind the float between two other loops.
- get_loops_in_front_of_float(u, v)[source]
Get all loops positioned in front of the float between two loops.
- property last_loop: Loop | None
Get the most recently added loop at the end of the yarn.
- Returns:
The last loop on this yarn, or None if no loops have been added.
- Return type:
Loop | None
- property first_loop: Loop | None
Get the first loop at the beginning of the yarn.
- Returns:
The first loop on this yarn, or None if no loops have been added.
- Return type:
Loop | None
- property has_loops: bool
Check if the yarn has any loops on it.
- Returns:
True if the yarn has at least one loop, False otherwise.
- Return type:
- property yarn_id: str
Get the string identifier for this yarn.
- Returns:
The string representation of the yarn properties.
- Return type:
- __str__()[source]
Get the string representation of this yarn.
- Returns:
The yarn identifier string.
- Return type:
- __repr__()[source]
Get the representation string of this yarn for debugging.
- Returns:
The representation of the yarn properties.
- Return type:
- __hash__()[source]
Get the hash value of this yarn for use in sets and dictionaries.
- Returns:
Hash value based on the yarn properties.
- Return type:
- __len__()[source]
Get the number of loops on this yarn.
- Returns:
The total number of loops on this yarn.
- Return type:
- make_loop_on_end()[source]
Create and add a new loop at the end of the yarn.
- Returns:
The newly created loop that was added to the end of this yarn.
- Return type:
- add_loop_to_end(loop)[source]
Add an existing loop to the end of this yarn and associated knit graph.
- insert_loop(loop, prior_loop=None)[source]
Insert a loop into the yarn sequence after the specified prior loop.
- __iter__()[source]
Iterate over loops on this yarn in sequence from first to last.
- Returns:
An iterator over the loops on this yarn in their natural sequence order.
- Return type:
Iterator[Loop]