knitout_interpreter package

Subpackages

Submodules

Module contents

knitout_interpreter: A comprehensive library for interpreting knitout files.

This package provides tools for parsing, validating, and executing knitout files used to control automatic V-Bed knitting machines. It includes support for the complete Knitout specification v2 created by McCann et al.

The library bridges the gap between high-level knitting pattern descriptions and machine-level execution, providing comprehensive analysis and simulation capabilities.

Core Functionality:
  • run_knitout(): Simple function to parse and execute knitout files

  • Knitout_Executer: Advanced class for detailed analysis and execution control

For specialized functionality, import from submodules:
  • knitout_interpreter.knitout_operations: Individual instruction types

  • knitout_interpreter.knitout_language: Parsing and grammar support

  • knitout_interpreter.knitout_execution_structures: Execution data structures

Example

Basic usage - execute a knitout file:

>>> from knitout_interpreter.run_knitout import run_knitout
>>> instructions, machine, graph = run_knitout("pattern.k")
>>> print(f"Executed {len(instructions)} instructions")

Advanced analysis with detailed control:

>>> from knitout_interpreter import Knitout_Executer
>>> from knitout_interpreter.knitout_language.Knitout_Parser import parse_knitout
>>> from virtual_knitting_machine.Knitting_Machine import Knitting_Machine
>>>
>>> instructions = parse_knitout("pattern.k", pattern_is_file=True)
>>> executer = Knitout_Executer(instructions, Knitting_Machine())
>>> print(f"Execution time: {executer.execution_time} carriage passes")
>>> print(f"Width required: {executer.left_most_position} to {executer.right_most_position}")
knitout_interpreter.run_knitout(knitout_file_name)[source]

Execute knitout instructions from a given file.

This function provides a convenient interface for processing a knitout file through the knitout interpreter, returning the executed instructions and resulting machine state and knit graph.

Parameters:

knitout_file_name (str) – Path to the file that contains knitout instructions.

Returns:

A 3-element tuple containing the executed instructions, final machine state, and knit graph. The first element is a list of Knitout_Line objects representing all processed instructions. The second element is a Knitting_Machine object containing the final state of the virtual knitting machine after execution. The third element is a Knit_Graph object representing the resulting fabric structure formed by the knitting operations.

Return type:

tuple

Example

Basic usage:

instructions, machine, graph = run_knitout("pattern.k")
print(f"Executed {len(instructions)} instructions")
print(f"Machine has {len(machine.needle_beds)} needle beds")
print(f"Graph contains {graph.node_count} nodes")

Note

The knitout file must be a valid knitout format file with proper headers and instructions.

Raises:
  • FileNotFoundError – If the specified knitout file cannot be found.

  • ValueError – If the knitout file contains invalid syntax or instructions.

class knitout_interpreter.Knitout_Executer(instructions, knitting_machine=None, accepted_error_types=None, knitout_version=2)[source]

Bases: object

A class used to execute a set of knitout instructions on a virtual knitting machine.

property carriage_passes: list[Carriage_Pass]

Get the carriage passes from this execution.

Returns:

The carriage passes resulting from this execution in execution order.

property execution_time: int

Get the execution time as measured by carriage passes.

Returns:

Count of carriage passes in process as a measure of knitting time.

property left_most_position: int | None

Get the leftmost needle position used in execution.

Returns:

The position of the left most needle used in execution, or None if no needles were used.

property resulting_knit_graph: Knit_Graph

Get the knit graph resulting from instruction execution.

Returns:

Knit Graph that results from execution of these instructions.

property right_most_position: int | None

Get the rightmost needle position used in execution.

Returns:

The position of the right most needle used in the execution, or None if no needles were used.

test_and_organize_instructions(accepted_error_types=None)[source]

Test the given execution and organize the instructions in the class structure.

This method processes all instructions, organizing them into carriage passes and handling any errors that occur during execution.

Parameters:

accepted_error_types (list | None) – A list of exceptions that instructions may throw that can be resolved by commenting them out. Defaults to None.

property version_line: Knitout_Version_Line

Get the version line for the executed knitout.

Returns:

The version line for the executed knitout.

write_executed_instructions(filename)[source]

Write a file with the organized knitout instructions.

Parameters:

filename (str) – The file path to write the executed instructions to.