Skip to content

subscript

Module for subscripts definitions/declarations.

Classes:

SubscriptExpr

SubscriptExpr(value: Expr, index: Optional[Expr] = None, lower: Optional[Expr] = None, upper: Optional[Expr] = None, step: Optional[Expr] = None, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for subscript expressions.

Parameters:

  • value (Expr) –

    The expression representing the object being indexed (e.g.,

  • an
  • index (Optional[Expr], default: None ) –

    The index of the variable.

  • lower (Optional[Expr], default: None ) –

    The lower bound of the slice (inclusive).

  • upper (Optional[Expr], default: None ) –

    The upper bound of the slice (exclusive).

  • step (Optional[Expr], default: None ) –

    The step size for the slice.

  • loc (SourceLocation, default: NO_SOURCE_LOCATION ) –

    The source location of the expression.

  • parent (Optional[ASTNodes], default: None ) –

    The parent AST node.

Methods:

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/subscript.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def __init__(
    self,
    value: Expr,
    index: Optional[Expr] = None,
    lower: Optional[Expr] = None,
    upper: Optional[Expr] = None,
    step: Optional[Expr] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    Initialize the SubscriptExpr instance.

    Parameters
    ----------
    value: Expr
        The expression representing the object being indexed (e.g.,
    an array or list).
    index (optional): Expr
        The index of the variable.
    lower (optional): Expr
        The lower bound of the slice (inclusive).
    upper (optional): Expr
        The upper bound of the slice (exclusive).
    step (optional): Expr
        The step size for the slice.
    loc: SourceLocation
        The source location of the expression.
    parent (optional): ASTNodes
        The parent AST node.
    """
    super().__init__(loc=loc, parent=parent)
    self.value: Expr = value if value is not None else LiteralNone()
    self.index: Expr = index if index is not None else LiteralNone()
    self.lower: Expr = lower if lower is not None else LiteralNone()
    self.upper: Expr = upper if upper is not None else LiteralNone()
    self.step: Expr = step if step is not None else LiteralNone()
    self.kind = ASTKind.SubscriptExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/subscript.py
118
119
120
121
122
123
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "SubscriptExpr"
    value = self._get_struct_wrapper(simplified)

    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
294
295
296
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
288
289
290
291
292
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )