Skip to content

base

Classes:

AST

AST(
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Methods:

Source code in packages/astx/src/astx/base.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the AST instance.
    parameters:
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct abstractmethod

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
376
377
378
379
380
381
382
383
384
385
@abstractmethod
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a structure that represents the node object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """

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

ASTKind

Bases: Enum

ASTMeta

Bases: type

ASTNodes

ASTNodes(
    name: str = "entry",
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Generic[ASTType], AST

Methods:

Source code in packages/astx/src/astx/base.py
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
def __init__(
    self,
    name: str = "entry",
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the AST instance.
    parameters:
      name:
        type: str
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.name = name
    self.nodes: list[ASTType] = []
    self.position: int = 0

append

append(value: ASTType) -> None
Source code in packages/astx/src/astx/base.py
484
485
486
487
488
489
490
491
def append(self, value: ASTType) -> None:
    """
    title: Append a new node to the stack.
    parameters:
      value:
        type: ASTType
    """
    self.nodes.append(value)

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    args_nodes = []

    for node in self.nodes:
        args_nodes.append(node.get_struct(simplified))

    key = str(self)
    value = cast(ReprStruct, args_nodes)
    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)
    )

DataType

DataType(
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: ExprType

Methods:

Source code in packages/astx/src/astx/base.py
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the data type.
    parameters:
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.name = f"temp_{DataType._tmp_id}"
    DataType._tmp_id += 1
    # set it as a generic data type
    self.type_: ExprType = ExprType()

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
685
686
687
688
689
690
691
692
693
694
695
696
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a simple structure that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"DATA-TYPE[{self.__class__.__name__}]"
    value = self.name
    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)
    )

Expr

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

Bases: AST

Methods:

Source code in packages/astx/src/astx/base.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the AST instance.
    parameters:
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct abstractmethod

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
376
377
378
379
380
381
382
383
384
385
@abstractmethod
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a structure that represents the node object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """

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

ExprType

ExprType(
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Expr

Methods:

Source code in packages/astx/src/astx/base.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the AST instance.
    parameters:
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
576
577
578
579
580
581
582
583
584
585
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a structure that represents the node object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    return {"Type": self.__class__.__name__}

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

OperatorType

OperatorType(
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: DataType

Methods:

Source code in packages/astx/src/astx/base.py
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the data type.
    parameters:
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.name = f"temp_{DataType._tmp_id}"
    DataType._tmp_id += 1
    # set it as a generic data type
    self.type_: ExprType = ExprType()

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
685
686
687
688
689
690
691
692
693
694
695
696
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a simple structure that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"DATA-TYPE[{self.__class__.__name__}]"
    value = self.name
    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)
    )

ParenthesizedExpr

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

Bases: DataType

Methods:

Source code in packages/astx/src/astx/base.py
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
def __init__(
    self,
    value: Expr,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the ParenthesizedExpr instance.
    parameters:
      value:
        type: Expr
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.type_ = getattr(value, "type_", DataType())
    self.value = value
    self.kind = ASTKind.ParenthesizedExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
807
808
809
810
811
812
813
814
815
816
817
818
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = "PARENTHESIZED-EXPR"
    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)
    )

SourceLocation

SourceLocation(line: int, col: int)
Source code in packages/astx/src/astx/base.py
63
64
65
66
67
68
69
70
71
72
73
def __init__(self, line: int, col: int):
    """
    title: Initialize the source location.
    parameters:
      line:
        type: int
      col:
        type: int
    """
    self.line = line
    self.col = col

StatementType

StatementType(
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: AST

Methods:

Source code in packages/astx/src/astx/base.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the AST instance.
    parameters:
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct abstractmethod

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
376
377
378
379
380
381
382
383
384
385
@abstractmethod
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a structure that represents the node object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """

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

Undefined

Undefined(
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Expr

Methods:

Source code in packages/astx/src/astx/base.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the AST instance.
    parameters:
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/base.py
608
609
610
611
612
613
614
615
616
617
618
619
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return a simple structure that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    value = "UNDEFINED"
    key = "UNDEFINED"
    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)
    )

is_using_jupyter_notebook

is_using_jupyter_notebook() -> bool
Source code in packages/astx/src/astx/base.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def is_using_jupyter_notebook() -> bool:
    """
    title: Check if it is executed in a jupyter notebook.
    returns:
      type: bool
    """
    try:
        from IPython import get_ipython  # type: ignore

        if "IPKernelApp" in get_ipython().config:  # type: ignore
            return True
    except Exception:
        pass
    return False