knit_script.knit_script_interpreter.knit_script_context module
Manages variable scope and machine state of knit pass during execution.
This module provides the Knit_Script_Context class, which serves as the primary execution context for knit script programs. It manages variable scoping, machine state, and provides convenient access to machine configuration parameters. The context integrates scope management with machine operations to provide a unified execution environment for knit script programs.
- class Knit_Script_Context(parent_scope=None, machine_specification=None, ks_file=None, parser=None, knitout_version=2, debugger=None, info_logger=None, warning_logger=None, error_logger=None)[source]
Bases:
Knit_Script_Debuggable_ProtocolManages the state of the Knitting machine during program execution.
The Knit_Script_Context class serves as the primary execution context for knit script programs. It extends the base context with variable scoping capabilities and provides convenient access to machine configuration parameters. The context manages the integration between variable scopes and machine state, ensuring that machine settings are properly maintained across different execution scopes.
This class provides the execution environment for knit script programs, handling variable management, scope transitions, and machine state coordination. It offers convenient properties for accessing and modifying machine parameters while maintaining proper scope isolation and inheritance.
- machine_state
The current state of the knitting machine.
- Type:
Knitting_Machine
- parser
Parser instance used for processing knit script code.
- Type:
- last_carriage_pass_result
Results from the most recent carriage pass operation.
- __init__(parent_scope=None, machine_specification=None, ks_file=None, parser=None, knitout_version=2, debugger=None, info_logger=None, warning_logger=None, error_logger=None)[source]
Initialize the knit script context.
- Parameters:
parent_scope (Knit_Script_Scope | None, optional) – Parent scope for variable management inheritance. Defaults to None.
machine_specification (Knitting_Machine_Specification, optional) – Specification for the knitting machine configuration. Defaults to Knitting_Machine_Specification().
ks_file (str | None, optional) – Path to the knit script file being executed. Defaults to None.
parser (Knit_Script_Parser | None, optional) – Parser instance for processing knit script syntax. Defaults to None.
knitout_version (int, optional) – Version number of the knitout format to generate. Defaults to 2.
debugger (Knit_Script_Debugger_Protocol, optional) – The optional debugger to attach to this context. Defaults to 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.
- variable_scope: Knit_Script_Scope
- debugger: Knit_Script_Debugger_Protocol | None
- property version: int
Get the knitout version being written.
- Returns:
The knitout version number currently in use for output generation.
- Return type:
- property gauged_sheet_record: Gauged_Sheet_Record
Get the current record of loops stored on each sheet in the current gauge.
- Returns:
The current record of loops stored on each sheet in the current gauge configuration.
- Return type:
- print(message, source=None, log_type=KnitScript_Logging_Level.info)[source]
Prints the given message to the appropriate log based on the given log type. :param message: The message to print. :type message: str :param source: An optional knitscript element that is the source of the printed information. Defaults to having no known source. :type source: KS_Element, optional :param log_type: The log type to use for printing. Defaults to printing information. :type log_type: KnitScript_Logging_Level, optional
- add_variable(key, value)[source]
Add a variable to the variable scope by the name of key.
- Parameters:
key (str) – Name of variable to be used in the knit script.
value (Any) – Value of the variable to store in the scope.
- enter_sub_scope(function_name=None, module_name=None, module_scope=None)[source]
Create a child scope and set it as the current variable scope.
- Parameters:
function_name (str | None, optional) – The name of the function owning this scope. Defaults to None.
module_name (str | None, optional) – The name of the module owning this scope. Defaults to None.
module_scope (Knit_Script_Scope | None, optional) – The scope of the function declaration context. Defaults to None.
- Returns:
The scope that was entered and is now active.
- Return type:
- exit_current_scope(collapse_into_parent=False)[source]
Exit the lowest level variable scope and reset the current variable scope up a level.
- property direction: Carriage_Pass_Direction
Get the carriage pass direction at current scope.
- Returns:
The current carriage pass direction.
- Return type:
Carriage_Pass_Direction
- property carrier: Yarn_Carrier_Set | None
Get the carrier in use at current scope.
- Returns:
The current carrier set or None if no carrier is active.
- Return type:
Yarn_Carrier_Set | None
- property racking: float
Get the racking at current scope.
- Returns:
The current racking value.
- Return type:
- property sheet: Sheet_Identifier
Get the current sheet at current scope.
- Returns:
The current sheet identifier.
- Return type:
- property gauge: int
Get the gauge (number of layers) that is being worked.
Note
Resetting gauge will cause the machine state to forget all current layer records.
- Returns:
The current gauge value.
- Return type:
- classmethod __class_getitem__(params)
Parameterizes a generic class.
At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.
However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….
- attach_debugger(debugger)[source]
Attaches the given debugger to this knitout execution. :param debugger: The debugger to attach to this context. :type debugger: Knit_Script_Debugger
- execute_statements(statements)[source]
Execute the statements in the current context.
- Parameters:
statements (Iterable[Statement]) – Statements to execute in the current context.
- Returns:
Any return value from the executed program.
- Return type:
Any | None
- Raises:
AssertionError – If assertions in the script fail during execution.
Knit_Script_Exception – If knit script specific errors occur during interpretation or execution.
Knitting_Machine_Exception – If machine operation errors occur during the knitting process.
- execute_statement(statement)[source]
Execute the given statement in the current context.
- Parameters:
statement (Statement) – The statement to execute.
- get_needle(is_front, pos, is_slider=False, global_needle=False, sheet=None, gauge=None)[source]
Get a needle based on current gauging configuration.
- Parameters:
is_front (bool) – Whether this is a front needle.
pos (int) – Position within the current layer.
is_slider (bool, optional) – Whether this is a slider needle. Defaults to False.
global_needle (bool, optional) – If true, ignore the gauging scheme. Defaults to False.
sheet (int | None, optional) – Specify the sheet to get needles from, defaults to the current sheet. Defaults to None.
gauge (int | None, optional) – Specify gauging to get needles from, defaults to current gauge. Defaults to None.
- Returns:
Needle based on current gauging configuration.
- Return type:
Needle
- get_machine_needle(is_front, pos, is_slider=False, global_needle=False, sheet=None, gauge=None)[source]
Get the exact needle instance in use on the machine state.
- Parameters:
is_front (bool) – Whether this is a front needle.
pos (int) – Position in current layer.
is_slider (bool, optional) – Whether this is a slider needle. Defaults to False.
global_needle (bool, optional) – If true, ignore the gauging scheme. Defaults to False.
sheet (int | None, optional) – Specify the sheet to get needles from, defaults to the current sheet. Defaults to None.
gauge (int | None, optional) – Specify gauging to get needles from, defaults to current gauge. Defaults to None.
- Returns:
The exact needle instance in use on the machine state.
- Return type:
Needle
- report_locals()[source]
- Returns:
A tuple containing the following values from the state of the debugged protocol: * The current state of the knitting machine. * The active yarn-carrier set or None if no carrier set is active. * The current sheet that the machine is set to. * The gauge that the machine is set to.
- Return type:
tuple[Knitting_Machine, Yarn_Carrier_Set | None, Sheet_Identifier, int]