knit_script package
Subpackages
- knit_script.knit_script_exceptions package
- Submodules
- knit_script.knit_script_exceptions.Knit_Script_Exception module
- knit_script.knit_script_exceptions.gauge_sheet_exceptions module
- knit_script.knit_script_exceptions.ks_exceptions module
- knit_script.knit_script_exceptions.parsing_exception module
- knit_script.knit_script_exceptions.python_style_exceptions module
- Module contents
- Submodules
- knit_script.knit_script_interpreter package
- Subpackages
- Submodules
- knit_script.knit_script_interpreter.Knit_Script_Interpreter module
- knit_script.knit_script_interpreter.Knit_Script_Parser module
- knit_script.knit_script_interpreter.Machine_Specification module
- knit_script.knit_script_interpreter.knit_script_actions module
program()
identifier()
declare_variable()
declare_global()
assertion()
print_statement()
try_catch()
exception_assignment()
pause_statement()
assignment()
float_exp()
int_exp()
direction_exp()
string()
f_string_section()
formatted_string()
call_list()
function_call()
list_expression()
list_comp()
indexed_value()
slice_index()
dict_assign()
dict_expression()
dict_comp()
unpack()
code_block()
elif_statement()
else_statement()
if_statement()
while_statement()
for_each_statement()
as_assignment()
with_statement()
needle_instruction()
instruction_assignment()
carriage_pass()
needle_id()
sheet_id()
carrier()
return_statement()
param_list()
function_declaration()
expression()
negation()
xfer_rack()
xfer_pass()
accessor()
exp_statement()
cut_statement()
release_statement()
remove_statement()
gauge_exp()
drop_pass()
push_to()
push_dir()
push_statement()
swap_statement()
pass_second()
import_statement()
- knit_script.knit_script_interpreter.knit_script_context module
- knit_script.knit_script_interpreter.ks_element module
- Module contents
- knit_script.knit_script_std_library package
- knit_script.knit_script_warnings package
Submodules
Module contents
knit_script: A comprehensive library for interpreting Knit Script files.
This package provides tools for parsing, validating, and executing knit script files used to control automatic V-Bed knitting machines.
- Core Functionality:
knit_script_to_knitout(): Simple function to interpret knitscript programs into knitout programs.
Example
Basic usage - execute a knitout file:
>>> from knit_script.interpret_knit_script import knit_script_to_knitout
>>> knit_graph, knitting_machine = knit_script_to_knitout("pattern.ks", "pattern.k", pattern_is_filename= True)
Advanced usage with variables from python preloaded into the knitscript interpreter:
>>> from knit_script.interpret_knit_script import knit_script_to_knitout
>>> width = 10
>>> knit_graph, knitting_machine = knit_script_to_knitout("pattern.ks", "pattern.k", pattern_is_filename= True, width=width)
- knit_script_to_knitout(pattern, out_file_name, pattern_is_filename=True, **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) – Determines how to interpret the pattern parameter. If True, treats pattern as a filename to read from. If False, treats pattern as the actual knit script code. Defaults to True.
**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.
ValueError – If the pattern contains invalid knit script syntax or operations.
TypeError – If the provided arguments are of incorrect types.
Example
Basic usage - execute a knitout file:
>>> from knit_script.interpret_knit_script import knit_script_to_knitout >>> knitgraph, knitting_machine = knit_script_to_knitout("pattern.ks", "pattern.k", pattern_is_filename= True)
Advanced usage with variables from python preloaded into the knitscript interpreter:
>>> from knit_script.interpret_knit_script import knit_script_to_knitout >>> width = 10 >>> knitgraph, knitting_machine = knit_script_to_knitout("pattern.ks", "pattern.k", pattern_is_filename= True, width=width)
Note
The function creates intermediate representations and machine states that can be used for further analysis or debugging of the knitting pattern. The knitout file is automatically written during the conversion process.
See also
Knit_Script_Interpreter: The underlying interpreter that performs the conversion. Knit_Graph: The graph representation of knitting patterns. Knitting_Machine: The virtual machine state representation.