Skip to content

operators

Classes:

AssignmentExpr

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

Bases: Expr

Methods:

Source code in packages/astx/src/astx/operators.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
Source code in packages/astx/src/astx/operators.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    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
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

AugAssign

AugAssign(
    target: Identifier,
    op_code: OpCodeAugAssign,
    value: DataType,
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: DataType

Methods:

Source code in packages/astx/src/astx/operators.py
268
269
270
271
272
273
274
275
276
277
278
279
def __init__(
    self,
    target: Identifier,
    op_code: OpCodeAugAssign,
    value: DataType,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    super().__init__(loc=loc)
    self.target = target
    self.op_code = op_code
    self.value = value
    self.kind = ASTKind.AugmentedAssignKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/operators.py
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = str(self)
    value: ReprStruct = {
        "target": self.target.get_struct(simplified),
        "value": self.value.get_struct(simplified),
    }
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

CompareOp

CompareOp(
    left: DataType,
    ops: Iterable[
        Literal["==", "!=", "<", ">", "<=", ">="]
    ],
    comparators: Iterable[DataType],
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: DataType

Methods:

Source code in packages/astx/src/astx/operators.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
def __init__(
    self,
    left: DataType,
    ops: Iterable[Literal["==", "!=", "<", ">", "<=", ">="]],
    comparators: Iterable[DataType],
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize the CompareOp instance.
    parameters:
      left:
        type: DataType
      ops:
        type: Iterable[Literal[==, !=, <, >, <=, >=]]
      comparators:
        type: Iterable[DataType]
      loc:
        type: SourceLocation
    """
    super().__init__(loc=loc)
    self.ops = list(ops)
    self.comparators = list(comparators)
    if len(self.ops) != len(self.comparators):
        raise ValueError(
            "Number of operators must equal number of comparators."
        )
    for op in self.ops:
        if op not in ["==", "!=", "<", ">", "<=", ">="]:
            raise ValueError(f"Invalid comparison operator: {op}")
    self.left = left
    self.kind = ASTKind.CompareOpKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/operators.py
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    ops_str = ", ".join(self.ops)
    key = f"COMPARE[{ops_str}]"
    content: ReprStruct = {
        "left": self.left.get_struct(simplified),
        "comparators": [
            comp.get_struct(simplified) for comp in self.comparators
        ],
    }
    return self._prepare_struct(key, content, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

Starred

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

Bases: Expr

Methods:

Source code in packages/astx/src/astx/operators.py
404
405
406
407
408
409
410
411
412
def __init__(
    self,
    value: Expr,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.value = value
    self.kind = ASTKind.StarredKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/operators.py
422
423
424
425
426
427
428
429
430
431
432
433
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = "STARRED[*]"
    content: ReprStruct = {"value": self.value.get_struct(simplified)}
    return self._prepare_struct(key, content, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    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

Methods:

Source code in packages/astx/src/astx/operators.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def __init__(
    self,
    name: str,
    value: Expr,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the VarExprAST instance.
    parameters:
      name:
        type: str
      value:
        type: Expr
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    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
Source code in packages/astx/src/astx/operators.py
216
217
218
219
220
221
222
223
224
225
226
227
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = str(self)
    value = self.value.get_struct(simplified)
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    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

Methods:

Source code in packages/astx/src/astx/operators.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def __init__(
    self,
    lhs: Variable,
    rhs: DataType,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize the WalrusOp instance.
    parameters:
      lhs:
        type: Variable
      rhs:
        type: DataType
      loc:
        type: SourceLocation
    """
    super().__init__(loc=loc)
    self.lhs = lhs
    self.rhs = rhs
    self.kind = ASTKind.WalrusOpKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/operators.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    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
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )