drepr.program_generation.alignment_fn#

Classes

AlignmentFn(desc, program)

OnStepCallback(*args, **kwargs)

PathAccessor(program)

Generate code to access elements (indices & values) of an attribute

class AlignmentFn(desc: DRepr, program: Program)[source]#

Bases: object

Parameters:
  • desc (DRepr) –

  • program (Program) –

align(ast: AST, aligns: list[Union[drepr.models.align.RangeAlignment, drepr.models.align.ValueAlignment, drepr.models.align.IdenticalAlign, drepr.models.align.AutoAlignment]], validate_path: bool, on_missing_key: Optional[Callable[[AST], Any]], iter_final_list: bool)[source]#
Parameters:
align_by_range(ast: AST, align: RangeAlignment, validate_path: bool, on_missing_key: Optional[Callable[[AST], Any]], iter_final_list: bool) AST[source]#

Generate a piece of code that will generate variables (of target attr) to complete this alignment, if the alignment is one/many to one, then return ast is the same as we do not introduce nested statements. If the alignment is one/many to many, then we will need to have a for loop, hence, we have nested statements -> nested AST.

Parameters:
Return type:

AST

class PathAccessor(program: Program)[source]#

Bases: object

Generate code to access elements (indices & values) of an attribute

Parameters:

program (Program) –

iterate_elements(ast: AST, attr: Attr, aligned_attr: Optional[Attr] = None, to_aligned_dim: Optional[dict[int, int]] = None, validate_path: bool = False, on_missing_key: Optional[Callable[[AST], Any]] = None, iter_final_list: bool = False, on_step_callback: Optional[OnStepCallback] = None)[source]#
Parameters:
next_dimensions(ast: AST, attr: Attr, dim: int, aligned_attr: Optional[Attr], to_aligned_dim: Optional[dict[int, int]], validate_path: bool, on_missing_key: Optional[Callable[[AST], Any]] = None, iter_final_list: bool = False, on_step_callback: Optional[OnStepCallback] = None)[source]#

Generate code to access elements of dimensions of attr started at dim. Return the next ast, remaining dimension index, and whether it has stopped.

Parameters:
  • mem – memory to store variables

  • ast (AST) – current ast

  • attr (Attr) – attribute

  • dim (int) – starting dimension

  • aligned_attr (Optional[Attr]) –

  • to_aligned_dim (Optional[dict[int, int]]) –

  • validate_path (bool) – whether to generate code to check if values of each dimension is correct. If the attribute is annotated with missing_path = True, then setting this does not have any effect.

  • on_missing_key (Optional[Callable[[AST], Any]]) – a function that will be called when the key does not exist. If it is None, then we will raise an exception.

  • iter_final_list (bool) – if value type of attribute is a list, and this flag is true, we will iterate over the list to yield each item

  • on_step_callback (Optional[OnStepCallback]) – a function that can be called to generate code at each time we step into a new dimension (except dimension 0, which is the resource data)

access_key(ast: AST, attr: Attr, collection: Expr, key: ExprConstant, result: codegen.models.var.DeferredVar | codegen.models.var.Var, dim: int, handle_missing_key: Union[Literal['safe', 'no_missing_key'], Callable[[AST], None]], on_step_callback: Optional[OnStepCallback] = None)[source]#
Parameters:
  • ast (AST) –

  • attr (Attr) –

  • collection (Expr) –

  • key (ExprConstant) –

  • result (codegen.models.var.DeferredVar | codegen.models.var.Var) –

  • dim (int) –

  • handle_missing_key (Union[Literal['safe', 'no_missing_key'], ~typing.Callable[[~codegen.models.ast.AST], None]]) –

  • on_step_callback (Optional[OnStepCallback]) –

static skip_on_missing_key(parent_tree: AST, tree: AST)[source]#
Parameters:
  • parent_tree (AST) –

  • tree (AST) –

class OnStepCallback(*args, **kwargs)[source]#

Bases: Protocol