Skip to content

operators

ASTx classes for the operators.

Classes:

AssignmentExpr

AssignmentExpr(targets: Iterable[Expr] | ASTNodes[Expr], value: Expr, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

AST class for assignment expressions.

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/operators.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def __init__(
    self,
    targets: Iterable[Expr] | ASTNodes[Expr],
    value: Expr,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)

    if isinstance(targets, ASTNodes):
        self.targets = targets
    else:
        self.targets = ASTNodes()
        for target in targets:
            self.targets.append(target)

    self.value = value
    self.kind = ASTKind.AssignmentExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/operators.py
86
87
88
89
90
91
92
93
94
95
96
97
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "ASSIGNMENT-EXPR"
    targets_dict = {"targets": self.targets.get_struct(simplified)}
    value_dict = {"value": self.value.get_struct(simplified)}

    value = {
        **cast(DictDataTypesStruct, targets_dict),
        **cast(DictDataTypesStruct, value_dict),
    }

    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)
    )

VariableAssignment

VariableAssignment(name: str, value: Expr, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

AST class for variable declaration.

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/operators.py
108
109
110
111
112
113
114
115
116
117
118
119
120
def __init__(
    self,
    name: str,
    value: Expr,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the VarExprAST instance."""
    super().__init__(loc=loc, parent=parent)
    self.loc = loc
    self.name = name
    self.value = value
    self.kind = ASTKind.VariableAssignmentKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/operators.py
126
127
128
129
130
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = str(self)
    value = self.value.get_struct(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)
    )

WalrusOp

WalrusOp(lhs: Variable, rhs: DataType, loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: DataType

AST class for the Walrus (assignment expression) operator.

Methods:

  • get_struct

    Return the AST structure that represents 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/operators.py
29
30
31
32
33
34
35
36
37
38
39
def __init__(
    self,
    lhs: Variable,
    rhs: DataType,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the WalrusOp instance."""
    super().__init__(loc=loc)
    self.lhs = lhs
    self.rhs = rhs
    self.kind = ASTKind.WalrusOpKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure that represents the object.

Source code in src/astx/operators.py
45
46
47
48
49
50
51
52
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure that represents the object."""
    key = "WALRUS[:=]"
    lhs = {"lhs": self.lhs.get_struct(simplified)}
    rhs = {"rhs": self.rhs.get_struct(simplified)}

    content: ReprStruct = {**lhs, **rhs}
    return self._prepare_struct(key, content, 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)
    )