knit_script.knit_script_interpreter.expressions.accessors module

Used to access attributes from instances in code.

This module provides the Attribute_Accessor_Expression class, which handles attribute access operations for knit script expressions. It supports accessing attributes from both knit script objects and underlying Python objects, including method calls, property access, and specialized handling for machine and sheet-specific needle collections.

class Attribute_Accessor_Expression(parser_node, parent_path, attribute)[source]

Bases: Expression

Accesses attributes of expression either from knit-script or underlying python.

This class handles complex attribute access operations in knit script, supporting both direct attribute access and method calls. It can access attributes from Python objects as well as specialized knit script objects like machines and sheet identifiers. The class supports chained attribute access and provides specialized handling for needle set expressions.

parent

List of parent expressions in the access chain.

Type:

list[Expression]

attribute

The attribute being accessed from the parent.

Type:

Expression

__init__(parser_node, parent_path, attribute)[source]

Initialize the Attribute_Accessor_Expression.

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

  • parent_path (list[Expression] | Expression) – The expression to access an attribute from. Can be a single expression or a chain of expressions.

  • attribute (Expression) – The attribute of the parent expression. May produce more nested accessors.

property is_method_call: bool

Returns: bool: True if the attribute being accessed is a method call. False otherwise.

parent_path(require_var_names=True)[source]
Parameters:

require_var_names (bool, optional) – If True, raise an attribute error if any parents are not variable names. Defaults to True.

Returns:

Path to parent value with expressions separated by dots.

Return type:

str

Raises:

AttributeError – If a parent in the path is not a variable name.

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.

parent_path_list(require_var_names=True)[source]
Parameters:

require_var_names (bool, optional) – If True, raise an attribute error if any parents are not variable names. Defaults to True.

Returns:

List of the names of all the parents in this accessor.

Return type:

list[str]

Raises:

AttributeError – If a parent in the path is not a variable name.

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

evaluate(context)[source]

Evaluate the expression to access the specified attribute.

Parameters:

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

Returns:

The accessed attribute value from the parent expression.

Return type:

Any

Raises:

AttributeError – If the attribute cannot be accessed from the parent object.