knit_script.interpret_knit_script module

Module containing the knit_script_to_knitout function

knit_script_to_knitout(pattern, out_file_name, pattern_is_filename=True, info_logger=None, warning_logger=None, error_logger=None, debugger=None, **python_variables)[source]

Convert a knit script pattern into knitout format.

This function serves as the main entry point for converting knit script patterns into knitout code. It processes the input pattern through the Knit Script Interpreter, which builds a knit graph representation and maintains the state of a virtual knitting machine throughout the conversion process.

The function supports both file-based patterns and direct pattern strings, allowing for flexible input methods. Additional Python variables can be passed to extend the knit script execution environment.

Parameters:
  • pattern (str) – The knit script pattern to process. This can be either: - A filename containing the knit script pattern (when pattern_is_filename=True) - A string containing the actual knit script code (when pattern_is_filename=False)

  • out_file_name (str) – The output file path where the generated knitout code will be written. The file should have a ‘.k’ extension by convention.

  • pattern_is_filename (bool, optional) – If True, treats pattern as a filename to read from. Otherwise, treats pattern as the actual knit script code. Defaults expecting files for the pattern.

  • debugger (Knit_Script_Debugger, optional) – The optional debugger to attach to this process. Defaults to having no debugger.

  • info_logger (Knit_Script_Logger, optional) – The logger to attach to this context. Defaults to a standard logger which outputs only to console.

  • warning_logger (KnitScript_Warning_Log, optional) – The warning logger to attach to this context. Defaults to a standard warning logger which outputs only to console.

  • error_logger (KnitScript_Error_Log, optional) – The error logger to attach to this context. Defaults to a standard error logger which outputs only to console.

  • **python_variables (Any) – Additional keyword arguments that will be loaded into the knit script execution scope as Python variables. These can be referenced within the knit script pattern.

Returns:

A tuple containing: - Knit_Graph: The constructed knit graph representing the pattern structure, including all stitches, connections, and knitting operations. - Knitting_Machine: The final state of the virtual knitting machine after processing the pattern, including needle positions, yarn carriers, and machine configuration.

Return type:

Tuple[Knit_Graph, Knitting_Machine]

Raises:

FileNotFoundError – If pattern_is_filename is True and the specified pattern file cannot be found.

knit_script_to_knitout_with_return(pattern, out_file_name, pattern_is_filename=True, info_logger=None, warning_logger=None, error_logger=None, debugger=None, **python_variables)[source]

Convert a knit script pattern into knitout format and return any return value from the execution.

This function serves as the main entry point for converting knit script patterns into knitout code. It processes the input pattern through the Knit Script Interpreter, which builds a knit graph representation and maintains the state of a virtual knitting machine throughout the conversion process.

The function supports both file-based patterns and direct pattern strings, allowing for flexible input methods. Additional Python variables can be passed to extend the knit script execution environment.

Parameters:
  • pattern (str) – The knit script pattern to process. This can be either: - A filename containing the knit script pattern (when pattern_is_filename=True) - A string containing the actual knit script code (when pattern_is_filename=False)

  • out_file_name (str) – The output file path where the generated knitout code will be written. The file should have a ‘.k’ extension by convention.

  • pattern_is_filename (bool, optional) – If True, treats pattern as a filename to read from. Otherwise, treats pattern as the actual knit script code. Defaults expecting files for the pattern.

  • debugger (Knit_Script_Debugger, optional) – The optional debugger to attach to this process. Defaults to having no debugger.

  • info_logger (Knit_Script_Logger, optional) – The logger to attach to this context. Defaults to a standard logger which outputs only to console.

  • warning_logger (KnitScript_Warning_Log, optional) – The warning logger to attach to this context. Defaults to a standard warning logger which outputs only to console.

  • error_logger (KnitScript_Error_Log, optional) – The error logger to attach to this context. Defaults to a standard error logger which outputs only to console.

  • **python_variables (Any) – Additional keyword arguments that will be loaded into the knit script execution scope as Python variables. These can be referenced within the knit script pattern.

Returns:

A tuple containing: - Knit_Graph: The constructed knit graph representing the pattern structure, including all stitches, connections, and knitting operations. - Knitting_Machine: The final state of the virtual knitting machine after processing the pattern, including needle positions, yarn carriers, and machine configuration. - Any | None: The return value from the knitscript execution.

Return type:

Tuple[Knit_Graph, Knitting_Machine, Any | None]

Raises:

FileNotFoundError – If pattern_is_filename is True and the specified pattern file cannot be found.