knitout_interpreter.knitout_execution_structures.knitout_program module

Module containing the Knitout_Program class for organizing and maintaining the indices of knitout instructions.

class Knitout_Program(lines, source_program_name=None, default_version=2)[source]

Bases: Sequence[Knitout_Line]

Class for organizing and maintaining the ordering of knitout lines in a knitout program.

__init__(lines, source_program_name=None, default_version=2)[source]
property header: list[Knitout_Header_Line]

Returns: list[Knitout_Header_Line]: The list of header lines in the program.

property program_body: list[Knitout_Instruction | Knitout_Comment_Line]

Returns: list[Knitout_Instruction | Knitout_Comment_Line]: The list of instructions and comments in the body of the program.

property instructions: list[Knitout_Instruction]

Returns: list[Knitout_Instruction]: The list of instructions in the body of the program.

property needle_instructions: list[Needle_Instruction]

Returns: list[Needle_Instruction]: The list of instructions that operate on a needle in the body of the program.

property loop_forming_instructions: list[Loop_Making_Instruction]

Returns: list[Loop_Making_Instruction]: The list of loop forming instructions in the body of the program.

property comments: list[Knitout_Comment_Line]

Returns: list[Knitout_Comment_Line]: The list of comment lines in the program. Excludes breakpoints.

property version_line: Knitout_Version_Line

Returns: Knitout_Version_Line: Knitout_Version_Line for the program.

instruction_pauses_after_breakpoint(instruction)[source]
Parameters:

instruction (Knitout_Instruction) – The instruction to pause.

Returns:

True if the given instruction follows a breakpoint in the original program.

Return type:

bool

next_loop_forming_instruction_index(index)[source]
Parameters:

index (int) – The index in the program to search forward form for the next loop forming instruction.

Returns:

Index where the next loop forming instruction is or None if no future loop forming instruction is found.

Return type:

int | None

Notes

Excludes loop forming instruction at the given index.

new_program()[source]
Returns:

A knitout program initiated to the current state of the program.

Return type:

Knitout_Program

new_header_program()[source]
Returns:

A knitout program copied from the version and header lines of this program.

Return type:

Knitout_Program

swap_line(line_index, new_line, new_source=None)[source]

Swaps the line at the given index with the given new line. The line number of the new line is set to its current position, but any original line number or source remain the same.

Parameters:
  • line_index (int) – An index in the program to swap out.

  • new_line (Knitout_Line) – The new line to swap at the given position.

  • new_source (str, optional) – The source program of the new line. Defaults to taking the source name from this program or the source name already defined in the given new_line.

insert_line(index, new_line, new_source=None)[source]

Inserts the given line into the program at the given index.

Parameters:
  • index (int) – The index to insert into the program. The new line number for the given new line.

  • new_line (Knitout_Line) – The line to insert into the program.

  • new_source (str, optional) – The name of the source program for the new line if it is not already set.

append_line(line)[source]

Add the given line to the end of the program. :param line: The line to append. :type line: Knitout_Line

remove_line(line_index_to_remove)[source]

Removes the line at the given index of the program and updates the line numbers of all remaining lines in the program. :param line_index_to_remove: The index of the knitout line to remove from the program :type line_index_to_remove: int

Returns:

The removed line from the program.

Return type:

Knitout_Line

swap_lines(start_index, new_lines, new_source=None)[source]

Swaps the lines starting from the given index with the given new line. The line number of the new lines are set to their current position, but any original line number or source remain the same.

Parameters:
  • start_index (int) – An index to start swapping lines from.

  • new_lines (Sequence[Knitout_Line]) – The new lines to swap at the given positions.

  • new_source (str, optional) – The source program of the new lines. Defaults to taking the source name from this program or the source name already defined in the given new_lines.

insert_lines(start_index, new_lines, new_source=None)[source]

Inserts the given line into the program at the given index.

Parameters:
  • start_index (int) – The index to start inserting into the program.

  • new_lines (Sequence[Knitout_Line]) – The lines to insert into the program.

  • new_source (str, optional) – The name of the source program for the new line if it is not already set.

append_lines(lines)[source]

Add the given line to the end of the program. :param lines: The line to append. :type lines: Knitout_Line

remove_lines(start_index, end_index)[source]

Removes the lines at the given range of indices of the program and updates the line numbers of all remaining lines in the program. :param start_index: The starting index of the knitout lines to remove from the program. This index will be removed (inclusive). :type start_index: int :param end_index: The ending index of the knitout lines to remove from the program. This index will be removed (inclusive). :type end_index: int

shift_needle_positions(delta=0)[source]

Sets all needles in the current program to left align to provided needle number.

Parameters:

delta (int, optional) – The number by which to offset (+ or -) compared to the original position of the needles. Defaults to 0 with no effect on the program.

organize_program(remove_comments=True, remove_no_op=True, remove_pause=True, remove_breakpoint=True)[source]

Organizes the program to a standard ordering (version line, header, knitout program). :param remove_comments: If True, excludes all comments from the program. :type remove_comments: bool, optional :param remove_no_op: If True, excludes all no-op operation from the program. :type remove_no_op: bool, optional :param remove_pause: If True, excludes all pause operations from the program. :type remove_pause: bool, optional :param remove_breakpoint: If True, excludes all breakpoints from the program. :type remove_breakpoint: bool, optional

instructions_within_range(range_slice)[source]
Parameters:

range_slice (slice) – The slice of the program to gather instructions from. These indices include all program lines, not just instructions.

Returns:

The list of instructions within the given range of program indices.

Return type:

list[Knitout_Instruction]

__contains__(item)[source]
Parameters:

item (int | Knitout_Line) – The line or line index to check for in the program.

Returns:

True if the given index or line is in the program.

Return type:

bool

count(value) integer -- return number of occurrences of value
index(value[, start[, stop]]) integer -- return first index of value.

Raises ValueError if the value is not present.

Supporting start and stop arguments is optional, but recommended.