knit_script.knit_script_interpreter.expressions.list_expression module
Used for container structures (tuples, lists, dicts, comprehensions).
This module provides expression classes for handling various container data structures in knit script programs. It includes support for lists, dictionaries, tuples, list comprehensions, dictionary comprehensions, and unpacking operations, following Python conventions for syntax and behavior.
- class Unpack(parser_node, exp)[source]
Bases:
ExpressionUsed to unpack values into a tuple with * function.
The Unpack class implements the unpacking operator (*) functionality for knit script expressions. It takes an iterable expression and unpacks its elements into a tuple, similar to Python’s unpacking behavior.
- _expression
The expression to unpack into individual elements.
- Type:
- __init__(parser_node, exp)[source]
Initialize the Unpack expression.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
exp (Expression) – Expression to unpack into individual elements.
- evaluate(context)[source]
Evaluate the expression to unpack the contained expression.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Tuple with unpacked values from the expression.
- Return type:
tuple[Any, …]
- 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:
- 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:
- class Knit_Script_List(parser_node, expressions)[source]
Bases:
ExpressionEvaluates to list of expression values. Lists are not typed following python style conventions.
The Knit_Script_List class implements list literal expressions in knit script programs. It supports mixed-type elements and handles unpacking operations within list construction, following Python’s list syntax and behavior.
- expressions
The expressions to evaluate and include in the list.
- Type:
- __init__(parser_node, expressions)[source]
Initialize the Knit_Script_List.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
expressions (list[Expression]) – Expressions to fill the list with.
- evaluate(context)[source]
Evaluate the expression to create a list.
Evaluates each expression in the list and handles unpacking operations. If an expression is an Unpack, its elements are extended into the list rather than added as a nested structure.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
List of expression evaluations at current context, with unpacked elements properly expanded.
- Return type:
list[Any]
- 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:
- 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:
- class Sliced_List(parser_node, iter_exp, start=None, start_to_end=False, end=None, end_to_spacer=False, spacer=None, is_index=False)[source]
Bases:
ExpressionSlices a list using standard python syntax.
The Sliced_List class implements Python-style slicing and indexing operations for iterable expressions. It supports start, end, and step parameters for slicing, as well as simple indexing operations. Special handling is provided for knitting machine objects.
- _spacer
The step/spacer expression for slicing.
- Type:
Expression | None
- _end
The end index expression for slicing.
- Type:
Expression | None
- _start
The start index expression for slicing.
- Type:
Expression | None
- _iter_exp
The iterable expression to slice.
- Type:
- __init__(parser_node, iter_exp, start=None, start_to_end=False, end=None, end_to_spacer=False, spacer=None, is_index=False)[source]
Initialize the Sliced_List.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
iter_exp (Expression) – Iterable expression to slice.
start (Expression | None, optional) – Start of slice, inclusive, defaults to 0. Defaults to None.
start_to_end (bool, optional) – Whether to include start to end range. Defaults to False.
end (Expression | None, optional) – End of slice, exclusive, defaults to last element. Defaults to None.
end_to_spacer (bool, optional) – Whether to include end to spacer range. Defaults to False.
spacer (Expression | None, optional) – Step/spacer of slice, defaults to 1. Defaults to None.
is_index (bool, optional) – Whether this is an index operation rather than a slice. Defaults to False.
- evaluate(context)[source]
Evaluate the expression to perform slicing or indexing.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
The list of values in the given slice or the indexed value for single index operations.
- Return type:
Iterable[Any] | Any
- Raises:
TypeError – If attempting to slice a knitting machine or if the target is not iterable.
- 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:
- 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:
- class Knit_Script_Dictionary(parser_node, kwargs)[source]
Bases:
ExpressionUsed to process dictionary structures.
- The Knit_Script_Dictionary class implements dictionary literal expressions in knit script programs.
It evaluates key-value pairs and constructs dictionaries following Python’s dictionary syntax and behavior.
- _kwargs
List of key-value expression pairs for the dictionary.
- Type:
- __init__(parser_node, kwargs)[source]
Initialize the Knit_Script_Dictionary.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
kwargs (list[tuple[Expression, Expression]]) – The key-value pairs of expressions for the dictionary.
- evaluate(context)[source]
Evaluate the expression to create a dictionary.
Evaluates each key-value pair and constructs a dictionary with the results.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Dictionary with evaluated keys mapped to evaluated values.
- Return type:
dict[Any, Any]
- 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:
- 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:
- class Comprehension(parser_node, value_expressions, variables, iter_exp, comp_cond)[source]
Bases:
Expression- __init__(parser_node, value_expressions, variables, iter_exp, comp_cond)[source]
Initialize the base expression.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree that created this expression.
- evaluate_iterable(context)[source]
Evaluate the expression to generate an iterable sequence of values.
- Creates a new scope for iteration variables,
- iterates over the iterable,
and builds a sequence by evaluating the value expressions for each iteration that passes the optional condition.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Result of the comprehension with filtered and transformed values.
- Return type:
list[Any]
- Raises:
ValueError – If the iterable is not actually iterable or if variable unpacking doesn’t match the provided variables.
- 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__
- evaluate(context)
Evaluate the expression to produce a value.
This method must be overridden by subclasses to define how the expression produces its value during program execution. The base implementation returns None and should not be used directly.
- Parameters:
context (Knit_Script_Context) – The execution context used to evaluate expressions, containing variable scopes, machine state, and other runtime information.
- Returns:
The evaluated result of the expression. The specific type depends on the expression implementation.
- Return type:
Any
- 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:
- 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:
- class List_Comp(parser_node, value_expression, variables, iter_exp, comp_cond)[source]
Bases:
ComprehensionRuns a list comprehension over an iterator.
The List_Comp class implements Python-style list comprehensions for knit script programs. It supports iteration variables, optional filtering conditions, and custom spacing patterns for iteration control.
- __init__(parser_node, value_expression, variables, iter_exp, comp_cond)[source]
Initialize the List_Comp.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
value_expression (Expression) – Expression that fills the list for each iteration.
variables (list[Variable_Expression]) – Variables to bind from the iterable.
iter_exp (Expression) – The iterable expression to iterate over.
comp_cond (Expression | None) – Optional condition expression for filtering values.
- evaluate(context)[source]
Evaluate the expression to create a dictionary.
Creates a new scope for iteration variables, iterates over the iterable, and builds a list by evaluating the fill expression for each iteration that passes the optional condition.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Result of list comprehension with filtered and transformed values.
- Return type:
list[Any]
- Raises:
KeyError – If variable unpacking doesn’t match the provided variables.
- 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__
- evaluate_iterable(context)
Evaluate the expression to generate an iterable sequence of values.
- Creates a new scope for iteration variables,
- iterates over the iterable,
and builds a sequence by evaluating the value expressions for each iteration that passes the optional condition.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Result of the comprehension with filtered and transformed values.
- Return type:
list[Any]
- Raises:
ValueError – If the iterable is not actually iterable or if variable unpacking doesn’t match the provided variables.
- 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:
- 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:
- class Dictionary_Comprehension(parser_node, key, value, variables, iter_exp, comp_cond=None)[source]
Bases:
ComprehensionUsed for supporting dictionary comprehension.
The Dictionary_Comprehension class implements Python-style dictionary comprehensions for knit script programs. It supports iteration variables, optional filtering conditions, and custom spacing patterns, generating dictionaries through iteration.
- __init__(parser_node, key, value, variables, iter_exp, comp_cond=None)[source]
Initialize the Dictionary_Comprehension.
- Parameters:
parser_node (LRStackNode) – The parser node from the parse tree.
key (Expression) – Expression that generates keys for each dictionary entry.
value (Expression) – Expression that generates values for each dictionary entry.
variables (list[Variable_Expression]) – Variables to bind from the iterable.
iter_exp (Expression) – The iterable expression to iterate over.
comp_cond (Expression | None, optional) – Optional condition expression for filtering entries. Defaults to None.
- evaluate(context)[source]
Evaluate the expression to generate a dictionary through comprehension.
- Creates a new scope for iteration variables, iterates over the iterable,
and builds a dictionary by evaluating the key and value expressions for each iteration that passes the optional condition.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Result of dictionary comprehension with filtered and transformed key-value pairs.
- Return type:
dict[Any, Any]
- Raises:
KeyError – If variable unpacking doesn’t match the provided variables.
- 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__
- evaluate_iterable(context)
Evaluate the expression to generate an iterable sequence of values.
- Creates a new scope for iteration variables,
- iterates over the iterable,
and builds a sequence by evaluating the value expressions for each iteration that passes the optional condition.
- Parameters:
context (Knit_Script_Context) – The current context of the knit_script_interpreter.
- Returns:
Result of the comprehension with filtered and transformed values.
- Return type:
list[Any]
- Raises:
ValueError – If the iterable is not actually iterable or if variable unpacking doesn’t match the provided variables.
- 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:
- 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: