knit_script.knit_script_interpreter.expressions.needle_expression module

Expression for identifying needles.

This module provides the Needle_Expression class, which handles the parsing and evaluation of needle identifier strings in knit script programs. It converts needle string literals into actual Needle objects that respect the current gauging configuration.

class Needle_Expression(parser_node, needle_str)[source]

Bases: Expression

Expression that evaluates to a Needle.

The Needle_Expression class handles the conversion of needle string identifiers in knit script source code into actual Needle objects. It parses needle identifiers that specify bed position, slider status, and needle position, creating the appropriate needle type based on the current gauging configuration.

Needle identifiers follow the format: [bed][slider][position] where: - bed: ‘f’ for front bed, ‘b’ for back bed - slider: ‘s’ for slider needles (optional) - position: numeric position on the bed

Examples: “f5” (front needle 5), “bs3” (back slider needle 3), “f10” (front needle 10)

_needle_str

The original needle string identifier from the source code.

Type:

str

__init__(parser_node, needle_str)[source]

Initialize the Needle_Expression.

Parameters:
  • parser_node (LRStackNode) – The parser node from the parse tree.

  • needle_str (str) – String to parse into a needle identifier, following the format [bed][slider][position].

evaluate(context)[source]

Evaluate the expression to create a needle object.

Parses the needle string identifier to extract bed position, slider status, and needle position,

then creates the appropriate Needle object using the current gauging configuration from the context.

Parameters:

context (Knit_Script_Context) – The current context of the knit_script_interpreter.

Returns:

The evaluated Needle object based on the string identifier and current gauging configuration.

Return type:

Needle

Note

The needle type (regular or sheet needle) depends on the current gauge setting in the context. The position is interpreted relative to the current sheet and gauge configuration.

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]): ….

classmethod __init_subclass__(**kwargs)

Automatically wrap evaluate methods in subclasses with appropriate error handling decorator.

This method is called whenever a class inherits from Expression. It checks if the subclass defines its own evaluate method and wraps it with the appropriate decorator.

Parameters:

**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__

property file_name: str | None

Returns: str | None: The file name of the knitscript program this was parsed from or None if the program was passed as a string.

property line_number: int

Get the line number of the symbol that generated this statement.

Returns:

The line number where this element appears in the source file.

Return type:

int

property local_path: str | None

Returns: str | None: The path to the directory containing the file from which this element was parsed or None if the value was parsed from a python string.

property location: Location

Get the location of this symbol in KnitScript file.

Returns:

The location of this symbol in the source file, including file name, line number, and position information.

Return type:

Location

property location_str: str

Returns: str: The string referencing the line number and possible file name information about this element.

property position_context: str

The position context string is the string from the knitscript program from which this element was parsed. The context string will begin at the start of this element and continue to the end of the line of knitscript or a semicolon on new line are reached.

Returns:

The string used to contextualize this element in the knitscript program.

Return type:

str