knitout_interpreter.knitout_execution module
Module containing the knitout executer class
- class Knitout_Executer(knitout_program, knitting_machine=None, debugger=None, snapshot_targets=None, relax_violations=False, knitout_version=2)[source]
Bases:
Machine_Component[Knitout_LoopT]A class used to execute a set of knitout instructions on a virtual knitting machine. The instructions are processed, organized to isolate the header, verified on the knitting machine, and no-op operations are converted into comments. The execution process can optionally be debugged by attaching a debugger. The execution process can have optional snapshots of the knitting machine taken at specified line numbers.
- process
The ordered list of instructions and carriage passes executed in the knitting process.
- Type:
- executed_header
The header that creates this knitting machine based on the header lines specified and the knitting machine specified at initialization.
- Type:
- executed_instructions
The instructions that have been executed so far and updated the knitting machine state.
- Type:
- debugger
The optional debugger attached to this knitout process.
- Type:
Knitout_Debugger | None
- __init__(knitout_program, knitting_machine=None, debugger=None, snapshot_targets=None, relax_violations=False, knitout_version=2)[source]
Initialize the knitout executer.
- Parameters:
knitout_program (Sequence[Knitout_Line] | str) – The knitout lines to executed or a filename to parse into a knitout program.
knitting_machine (Knitting_Machine, optional) – The virtual knitting machine to execute instructions on. Defaults to the default Knitting Machine with no prior operations.
debugger (Knitout_Debugger, optional) – The debugger to attach to this knitout execution process. Defaults to having no debugger.
snapshot_targets (Sequence[int] | set[int], optional) – The line numbers to create machine snapshots from. Defaults to no snapshot targets.
relax_violations (bool | Iterable[Violation], optional) – If True, all violations are relaxed. If violations are given, those violations are relaxed. Defaults to False (full validation).
knitout_version (int, optional) – The knitout version to use. Defaults to 2.
- Raises:
FileNotFoundError – If knitout_program is given as a filename but the file cannot be found.
Knitout_ParseError – If the knitout_program cannot be parsed from the given file.
Incomplete_Knitout_Line_Error – If a line in the knitout_program is incomplete and cannot be parsed into a full instruction.
- property version_line: Knitout_Version_Line
Get the version line for the executed knitout.
- Returns:
The version line for the executed knitout.
- Return type:
- property knitting_machine: Knitout_Knitting_Machine[Knitout_LoopT]
Returns: Knitout_Knitting_Machine[Knitout_LoopT]: The knitting machine that the program is executing on.
- property resulting_knit_graph: Knit_Graph[Knitout_LoopT]
Get the knit graph resulting from instruction execution.
- Returns:
Knit Graph that results from execution of these instructions.
- Return type:
Knit_Graph[Knitout_Loop]
- property carriage_passes: list[Carriage_Pass]
Get the carriage passes from this execution.
- Returns:
The carriage passes resulting from this execution in execution order.
- Return type:
- 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.
- Return type:
- property execution_length: int
Returns: int: The number of lines in the completed execution. This includes the lines for the header, all comments, and all executed instructions.
- 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.
- Return type:
int | None
- 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.
- Return type:
int | None
- property current_carriage_pass: Carriage_Pass | None
Returns: None | Carriage_Pass: The carriage pass currently being formed in the knitout execution or None if no carriage pass is being formed.
- property starting_new_carriage_pass: bool
Returns: bool: True if the next instruction to be processed will initiate a new carriage pass, False otherwise.
- attach_debugger(debugger=None)[source]
Attaches the given debugger to this knitout execution. :param debugger: The debugger to attach to this knitout execution process. Defaults to attaching a new default debugger. :type debugger: Knitout_Debugger, optional
- enable_snapshot(target_line)[source]
Sets the execution to take a snapshot when it reaches the given line.
- Parameters:
target_line (int) – The target line number to take a snapshot on
- Warns:
Missed_Snapshot_Warning – If the target line has been passed.
- disable_snapshot(target_line, remove_existing_snapshot=False)[source]
Removes target snapshot on given line number. :param target_line: The target line number that should not have a snapshot taken. :type target_line: int :param remove_existing_snapshot: If True, any snapshot already taken on the given line is deleted. Defaults to False and keeping that snapshot. :type remove_existing_snapshot: bool, optional
- relax_validation_policy(violations=<enum 'Violation'>, include_warnings=False)[source]
Update the Knitting_Machine_Error_Policy of the execution knitting machine to relax the given violations.
- Parameters:
violations (Sequence[Violation], optional) – The violations to relax. Defaults to all violations.
include_warnings (bool, optional) – If True, warning will be raised for violations. Defaults to False.
- test_and_organize_instructions()[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.
- 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.
- 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]): ….
- __len__()[source]
- Returns:
The length of the executed knitout program, including headers, comments, and executed instructions.
- Return type:
- handle_violations(response_policy=None, handler=None)
Context manager that catches exceptions and routes them through the violation policy.
- Parameters:
response_policy (ViolationResponse | Violation, optional) – The ViolationResponse policy for this code block. If given a Violation, the response will be the policy assigned to that violation in the violation policy. Defaults to the response will be the default response of the violation policy.
handler (Callable[..., Any], optional) – Optional handler to attempt before falling back to the policy response.
- property machine_is_all_needle_racked: bool
Returns: bool: True if the knitting machine is racked for all needle knitting.
- property machine_specification: Knitting_Machine_Specification
Returns: Knitting_Machine_Specification: The machine specification of the machine associated with this component.
- property needle_count_of_machine: int
Returns: int: The number of needles on each bed of the knitting machine. Defaults to the bed size in the knitting machine specification.
- property rightmost_slot_on_machine: int
Returns: int: The rightmost slot on this knitting machine. Defaults to the standard bed size in the knitting machine specification.
- set_response_for(violation, response=None)
Sets the response for the given violation to the given response.
- Parameters:
violation (Violation) – The violation to set the response for.
response (ViolationResponse, optional) – The response to set for the given violation. Defaults to the default response of this policy.
- property violation_policy: Knitting_Machine_Error_Policy
Returns: Knitting_Machine_Error_Policy: The policy for handling machine state errors.
- execute_knitout(knitout_program, knitting_machine=None, knitout_version=2, debugger=None, write_to_file=None, relax_violations=False)[source]
Executes, verifies, and organizes the given knitout program and optionally writes it to a file.
- Parameters:
knitout_program (Sequence[Knitout_Line] | str) – The knitout program to be executed. If a string is provided, it will be interpreted as a file to read knitout from.
knitting_machine (Knitting_Machine | Knitting_Machine_Specification, optional) – The knitting machine to execute the program in. If a specification is provided, a new machine is created from the specification. Defaults to a new standard knitting machine.
knitout_version (int, optional) – The knitout version to execute in and write out. Defaults to 2.
debugger (Knitout_Debugger, optional) – An optional debugger to attach to the knitout process. Defaults to no debugger.
write_to_file (str, optional) – The name of the file to write knitout instructions to. Defaults to not writing a knitout file.
relax_violations (bool | Iterable[Violation], optional) – If True, all violations are relaxed. If violations are given, those violations are relaxed. Defaults to False (full validation).
- Returns:
A Tuple containing the following: * The verified and organized knitout program * The knitting machine after completion of the process. * The knitgraph graph after completion of the process.
- Return type:
tuple[Knitout_Program, Knitting_Machine, Knit_Graph]