quilt_knit.quilt.Quilt module

The module containing the Quilt class.

exception Blocked_Swatch_Connection_Exception(connection, blocking_connections)[source]

Bases: Exception

An Exception raised when attempting to add a blocked connection to a quilt.

connection

The blocked connection.

Type:

Swatch_Connection

blocking_connections

The connections in the quilt that blocked the new connection.

Type:

set[Swatch_Connection]

Parameters:
__init__(connection, blocking_connections)[source]
Parameters:
add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception Unconnected_Swatches_Exception(a_swatch, b_swatch)[source]

Bases: Exception

An Exception raised when attempting to merge two unconnected swatches in a quilt.

a_swatch

The first swatch.

Type:

Swatch

b_swatch

The second swatch.

Type:

Swatch

Parameters:
__init__(a_swatch, b_swatch)[source]
Parameters:
  • a_swatch (Swatch) – The first swatch.

  • b_swatch (Swatch) – The second swatch.

add_note()

Exception.add_note(note) – add a note to the exception

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class Quilt[source]

Bases: object

A data structure of a dynamic grid of connected swatches which can be merged to form a unified swatch.

course_wise_connections

A directed graph of the course wise connections between swatches in the quilt.

Type:

DiGraph

wale_wise_connections

A directed graph of the wale wise connections between swatches in the quilt.

Type:

DiGraph

swatch_neighborhoods

A dictionary of swatches keyed to their neighborhoods.

Type:

dict[Swatch, Swatch_Neighborhood]

swatches_to_rightward_shifts

A dictionary of swatches keyed to the number of needles to shift them by rightward when merging the quilt.

Type:

dict[Swatch, int]

__init__()[source]
connect_swatches_wale_wise(bottom_swatch, top_swatch, bottom_leftmost_needle_position=0, bottom_rightmost_needle_position=None, top_leftmost_needle_position=0, top_rightmost_needle_position=None, remove_cast_ons=True)[source]

Forms a wale-wise connection with the given specification and adds it to the Quilt.

Parameters:
  • bottom_swatch (Swatch) – The bottom swatch in the connection.

  • top_swatch (Swatch) – The top swatch in the connection.

  • bottom_leftmost_needle_position (int, optional) – The leftmost needle position to merge from the bottom swatch. Defaults to 0.

  • bottom_rightmost_needle_position (int, optional) – The rightmost needle position to merge from the bottom swatch. Defaults to the width of the bottom swatch.

  • top_leftmost_needle_position (int, optional) – The leftmost needle position to merge into the top swatch. Defaults to 0.

  • top_rightmost_needle_position (int, optional) – The rightmost needle position to merge into the top swatch. Defaults to the width of the top swatch.

  • remove_cast_ons (bool, optional) – Whether to remove cast-on operations from the top swatch before merging. Defaults to True.

Returns:

The connection formed and added to the quilt.

Return type:

Wale_Wise_Connection

Raises:

Blocked_Swatch_Connection_Exception – If the new connection is blocked by existing connections in the quilt.

connect_swatches_course_wise(left_swatch, right_swatch, first_carriage_pass_on_left=0, last_carriage_pass_on_left=None, first_carriage_pass_on_right=0, last_carriage_pass_on_right=None)[source]

Forms a course-wise connection with the given specification and adds it to the Quilt. :type left_swatch: Swatch :param left_swatch: The left swatch in the connection. :type left_swatch: Swatch :type right_swatch: Swatch :param right_swatch: The right swatch in the connection. :type right_swatch: Swatch :type first_carriage_pass_on_left: int :param first_carriage_pass_on_left: The first carriage pass to merge from on the left side. Defaults to 0. :type first_carriage_pass_on_left: int, optional :type last_carriage_pass_on_left: Optional[int] :param last_carriage_pass_on_left: The last carriage pass to merge from on the left side. Defaults to the height of the left swatch. :type last_carriage_pass_on_left: int, optional :type first_carriage_pass_on_right: int :param first_carriage_pass_on_right: The first carriage pass to merge from on the right side. Defaults to 0. :type first_carriage_pass_on_right: int, optional :type last_carriage_pass_on_right: Optional[int] :param last_carriage_pass_on_right: The last carriage pass to merge from on the right side. Defaults to the height of the right swatch. :type last_carriage_pass_on_right: int, optional

Returns:

The connection formed and added to the quilt.

Return type:

Course_Wise_Connection

Raises:

Blocked_Swatch_Connection_Exception – If the new connection is blocked by existing connections in the quilt.

__contains__(item)[source]
Parameters:

item (None | Swatch | tuple[Swatch, Swatch] | Swatch_Neighborhood | Swatch_Connection) – The item to search for in the swatch.

Returns:

True if the item is in the quilt. False otherwise. * A None item returns False. * If given a swatch, returns True if the swatch is in the quilt. * If given a tuple of two swatches, returns True if there is a wale-wise or course-wise connection between these swatches in the quilt. * If given a swatch neighborhood, returns True if the swatch of that neighborhood is in the quilt. * If given a swatch connections, returns True if the connections is in the quilt.

Return type:

bool

get_connection(a_swatch, b_swatch, force_direction=True)[source]
Parameters:
  • a_swatch (Swatch) – The first swatch in the connection.

  • b_swatch (Swatch) – The second swatch in the connection.

  • force_direction (bool, optional) – If True, only returns connections from a_swatch to b_swatch. Defaults to True.

Returns:

The connection in the quilt between the given swatches or None if no connection exists.

Return type:

Swatch_Connection | None

get_course_wise_connection(a_swatch, b_swatch, force_direction=True)[source]
Parameters:
  • a_swatch (Swatch) – The first swatch in the connection.

  • b_swatch (Swatch) – The second swatch in the connection.

  • force_direction (bool, optional) – If True, only returns connections from a_swatch to b_swatch. Defaults to True.

Returns:

The course wise connection in the quilt between the given swatches or None if no connection exists.

Return type:

Course_Wise_Connection | None

get_wale_wise_connection(a_swatch, b_swatch, force_direction=True)[source]
Parameters:
  • a_swatch (Swatch) – The first swatch in the connection.

  • b_swatch (Swatch) – The second swatch in the connection.

  • force_direction (bool, optional) – If True, only returns connections from a_swatch to b_swatch. Defaults to True.

Returns:

The wale-wise connection in the quilt between the given swatches or None if no connection exists.

Return type:

Wale_Wise_Connection | None

add_swatch(swatch)[source]

Adds the given swatch to the quilt with no connections to other swatches.

Parameters:

swatch (Swatch) – The swatch to add to the quilt.

Return type:

None

merge_swatches_course_wise(left_swatch, right_swatch, discard_unconnected_lower_courses=False, discard_unconnected_upper_courses=False)[source]

Merges the given swatches based on their connection in the quilt. The merged swatch is re-attached to the quilt in their place.

Parameters:
  • left_swatch (Swatch) – The left swatch to merge.

  • right_swatch (Swatch) – The right swatch to merge.

  • discard_unconnected_lower_courses (bool, optional) – If True, The lower courses of the swatch that have no connections in the quilt will be discarded. Defaults to False.

  • discard_unconnected_upper_courses (bool, optional) – If True, The upper courses of the swatch that have no connections in the quilt will be discarded. Defaults to False.

Returns:

A tuple containing: * The swatch resulting from the merge. * The set of swatches created by slicing off the lower portions of the merging swatches. These remain in the quilt. * The set of swatches created by slicing off the upper portions of the merging swatches. These remain in the quilt.

Return type:

tuple[Swatch, set[Swatch], set[Swatch]

Raises:

Unconnected_Swatches_Exception – If the given swatches are not connected in the quilt.

convert_quilt_to_course_bands()[source]

Merge all the swatches in course-wise bands of the quilt until there are no more course wise connections to merge.

Returns:

The list, sorted from the bottom to the top of the quilt, of course-wise bands resulting from merging the swatches.

Return type:

list[set[Swatch]]

merge_quilt(compile_merges=False, compile_bands=False)[source]

Merges all connected swatches in the quilt.

Parameters:
  • compile_merges (bool, optional) – If set to True, interstitial swatch merges are compiled to DAT files. Defaults to False.

  • compile_bands (bool, optional) – If set to True, all bands of merged swatches are compiled to DAT files. Defaults to False.

Returns:

The set of swatches remaining in the quilt after the merge is complete.

Return type:

set[Swatch]