knit_graphs.directed_loop_graph module

Module containing directed loop graph class

class Directed_Loop_Graph[source]

Bases: Generic[LoopT, EdgeT]

Wrapper for networkx.DiGraphs with directed edges between loops (i.e., floats in yarns, stitches in knitgraph).

__init__() None[source]
property loop_count: int

Returns: int: The number of loops in the graph.

property edge_count: int

Returns: int: The number of edges in the graph.

property contains_loops: bool

Returns: bool: True if the graph has at least one loop, False otherwise.

property sorted_loops: list[LoopT]

Returns: list[Loop]: The list of loops in the graph sorted from the earliest formed loop to the latest formed loop.

property edge_iter: Iterator[tuple[LoopT, LoopT, EdgeT]]
Returns:

Iterator over the edges and edge-data in the graph.

Return type:

Iterator[tuple[LoopT, LoopT, EdgeT]]

Notes

No guarantees about the order of the edges.

property terminal_loops: Iterator[LoopT]

Returns: Iterator[Loop]: An iterator over all terminal loops in the graph.

has_loop(loop: int | LoopT) bool[source]
Parameters:

loop (int | LoopT) – The loop or loop id to check for in the graph.

Returns:

True if the loop id is in the graph. False, otherwise.

Return type:

bool

get_loop(loop_id: int) LoopT[source]
Parameters:

loop_id (int) – The loop id of the loop to get from the graph.

Returns:

The loop node in the graph.

Return type:

LoopT

successors(loop: int | LoopT) set[LoopT][source]
Parameters:

loop (int | LoopT) – The loop to get the successors of from the graph.

Returns:

The successors of the loop.

Return type:

set[LoopT]

has_child_loop(loop: LoopT) bool[source]
Parameters:

loop (Loop) – The loop to check for child connections.

Returns:

True if the loop has a child loop, False otherwise.

Return type:

bool

is_terminal_loop(loop: LoopT) bool[source]
Parameters:

loop (LoopT) – The loop to check for terminal status.

Returns:

True if the loop has no child loops, False otherwise.

Return type:

bool

get_child_loop(loop: LoopT) LoopT | None[source]
Parameters:

loop (LoopT) – The loop to look for a child loop from.

Returns:

The child loop if one exists, or None if no child loop is found.

Return type:

LoopT | None

predecessors(loop: int | LoopT) set[LoopT][source]
Parameters:

loop (int | LoopT) – The loop to get the predecessors of from the graph.

Returns:

The successors of the loop.

Return type:

set[LoopT]

ancestors(loop: LoopT) set[LoopT][source]
Parameters:

loop (LoopT) – The loop to get the ancestors of from the graph.

Returns:

The ancestors of the given loop.

Return type:

set[LoopT]

is_descendant(ancestor: LoopT, descendant: LoopT) bool[source]
Parameters:
  • ancestor (LoopT) – The loop to check if it is an ancestor of the other loop.

  • descendant (LoopT) – The loop to check if it is a descendant of the other loop.

Returns:

True if there is a directed path from the ancestor to the descendant, False otherwise.

Return type:

bool

source_loops(loop: LoopT) set[LoopT][source]
Parameters:

loop (LoopT) – The loop to get the sources of.

Returns:

The source loops of the given loop. These are the loops that are the source of all ancestors to the given loop.

Return type:

set[LoopT]

dfs_edges(loop: LoopT) Iterator[tuple[LoopT, LoopT]][source]
Parameters:

loop (LoopT) – The loop to start iteration over edges from.

Returns:

The depth-first-search ordering of edges starting from the given loop.

dfs_preorder_loops(loop: LoopT) Iterator[LoopT][source]
Parameters:

loop (LoopT) – The loop to start iteration from.

Returns:

The depth-first-search ordering of loops in the graph starting from the given loop.

Return type:

Iterator[LoopT]

has_edge(u: LoopT | int, v: LoopT | int) bool[source]
Parameters:
  • u (LoopT | int) – The loop or id of the first loop in the edge.

  • v (LoopT | int) – The loop or id of the second loop in the edge.

Returns:

True if the graph has an edge from loop u to v. False, otherwise.

Return type:

bool

get_edge(u: LoopT | int, v: LoopT | int) EdgeT[source]
Parameters:
  • u (LoopT | int) – The loop or id of the first loop in the edge.

  • v (LoopT | int) – The loop or id of the second loop in the edge.

Returns:

The data about the edge from loop u to v.

Return type:

EdgeT

add_loop(loop: LoopT) None[source]

Add the given loop to the loop graph. :param loop: The loop to add to the graph. :type loop: LoopT

remove_loop(loop: LoopT | int) None[source]

Remove the given loop from the graph. :param loop: The loop or id of the loop to remove. :type loop: LoopT | int

Raises:

KeyError – The given loop does not exist in the graph.

add_edge(u: LoopT | int, v: LoopT | int, edge_data: EdgeT) None[source]

Connect the given loop u to the loop v with the given edge data. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int :param edge_data: The edge data to associate with the connection. :type edge_data: EdgeT

Raises:

KeyError – Either of the given loops does not exist in the graph.

remove_edge(u: LoopT | int, v: LoopT | int) None[source]

Removes the edge from loop u to the loop v from the graph. :param u: The loop to connect to. :type u: LoopT | int :param v: The loop to connect to. :type v: LoopT | int

Raises:

KeyError – The given edge does not exist in the graph.

__contains__(item: LoopT | int | tuple[LoopT | int, LoopT | int]) bool[source]
Parameters:

item (LoopT | int | tuple[LoopT | int, LoopT | int]) – The loop, loop-id, or a pair of loops in a directed edge to search for.

Returns:

True if the graph contains the given loop or edge.

Return type:

bool

__getitem__(item: int) LoopT[source]
__getitem__(item: tuple[LoopT | int, LoopT | int]) EdgeT
Parameters:

item (int | tuple[LoopT | int, LoopT | int]) – The id of the loop or the pair of loops that form an edge in the graph.

Returns:

The loop associated with the given id. EdgeT: The edge data associated with the given pair of loops/ loop_ids.

Return type:

LoopT

Raises:

KeyError – The given loop or edge does not exist in the graph.

__iter__() Iterator[LoopT][source]
Returns:

Iterator over all the loops in the graph.

Return type:

Iterator[LoopT]

Notes

No guarantees about order of loops. Expected to be insertion order.

__len__() int[source]
Returns:

The number of loops in the graph.

Return type:

int

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]): ….

class Stitch_Edge(pull_direction: Pull_Direction)[source]

Bases: object

Common data about stitch edges.

pull_direction: Pull_Direction
__init__(pull_direction: Pull_Direction) None
class Float_Edge(front_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>, back_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>)[source]

Bases: Generic[LoopT]

The edge data for float edges between loops on a yarn.

front_loops: set[LoopT]
back_loops: set[LoopT]
loop_in_front_of_float(loop: LoopT) bool[source]
Parameters:

loop (LoopT) – The loop to find relative to this float.

Returns:

True if the loop is in the front of the float.

Return type:

bool

loop_behind_float(loop: LoopT) bool[source]
Parameters:

loop (LoopT) – The loop to find relative to this float.

Returns:

True if the loop is behind the float.

Return type:

bool

add_loop_in_front_of_float(loop: LoopT) None[source]

Adds the given loop to the set of loops in front of this float. If the loop was behind the float, it is swapped to be in front. :param loop: The loop to put in front of this float. :type loop: LoopT

add_loop_behind_float(back_loop: LoopT) None[source]

Adds the given loop to the set of loops behind this float. If the loop was in front of this float, it is swapped to be behind. :param back_loop: The loop to put behind this float. :type back_loop: LoopT

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__(front_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>, back_loops: set[~knit_graphs.directed_loop_graph.LoopT] = <factory>) None
remove_loop_relative_to_floats(loop: LoopT) None[source]

Removes the given loop from the edge data (if present), noting that it is neither in front of nor behind the float. :param loop: The loop to remove. :type loop: LoopT

__contains__(item: LoopT) bool[source]
Parameters:

item (LoopT) – The loop to find relative to this float.

Returns:

True if the loop is in front or behind this float.

Return type:

bool