Skip to content

classes

Classes:

ClassDeclStmt

ClassDeclStmt(
    name: str,
    bases: Iterable[Expr] | ASTNodes[Expr] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    visibility: VisibilityKind = public,
    is_abstract: bool = False,
    metaclass: Optional[Expr] = None,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    methods: Iterable[FunctionDef]
    | ASTNodes[FunctionDef] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/classes.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def __init__(
    self,
    name: str,
    bases: Iterable[Expr] | ASTNodes[Expr] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    visibility: VisibilityKind = VisibilityKind.public,
    is_abstract: bool = False,
    metaclass: Optional[Expr] = None,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    methods: Iterable[FunctionDef] | ASTNodes[FunctionDef] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize ClassDeclStmt instance.
    parameters:
      name:
        type: str
      bases:
        type: Iterable[Expr] | ASTNodes[Expr]
      decorators:
        type: Iterable[Expr] | ASTNodes[Expr]
      visibility:
        type: VisibilityKind
      is_abstract:
        type: bool
      metaclass:
        type: Optional[Expr]
      attributes:
        type: Iterable[VariableDeclaration] | ASTNodes[VariableDeclaration]
      methods:
        type: Iterable[FunctionDef] | ASTNodes[FunctionDef]
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.name = name

    if isinstance(bases, ASTNodes):
        self.bases = bases
    else:
        self.bases = ASTNodes()
        for base in bases:
            self.bases.append(base)

    if isinstance(decorators, ASTNodes):
        self.decorators = decorators
    else:
        self.decorators = ASTNodes[Expr]()
        for decorator in decorators:
            self.decorators.append(decorator)

    if isinstance(attributes, ASTNodes):
        self.attributes = attributes
    else:
        self.attributes = ASTNodes[VariableDeclaration]()
        for a in attributes:
            self.attributes.append(a)

    if isinstance(methods, ASTNodes):
        self.methods = methods
    else:
        self.methods = ASTNodes[FunctionDef]()
        for m in methods:
            self.methods.append(m)

    self.visibility = visibility
    self.is_abstract = is_abstract
    self.metaclass = metaclass
    self.kind = ASTKind.ClassDeclStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/classes.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    vis = dict(zip(("public", "private", "protected"), ("+", "-", "#")))
    abstract = ", abstract" if self.is_abstract else ""

    key = f"CLASS-DECL[{vis[self.visibility.name]}{self.name}{abstract}]"
    value = self._get_struct_wrapper(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)
    )

ClassDefStmt

ClassDefStmt(
    name: str,
    bases: Iterable[Expr] | ASTNodes[Expr] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    body: Block = CLASS_BODY_DEFAULT,
    visibility: VisibilityKind = public,
    is_abstract: bool = False,
    metaclass: Optional[Expr] = None,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    methods: Iterable[FunctionDef]
    | ASTNodes[FunctionDef] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: ClassDeclStmt

Methods:

Source code in packages/astx/src/astx/classes.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
def __init__(
    self,
    name: str,
    bases: Iterable[Expr] | ASTNodes[Expr] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    body: Block = CLASS_BODY_DEFAULT,
    visibility: VisibilityKind = VisibilityKind.public,
    is_abstract: bool = False,
    metaclass: Optional[Expr] = None,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    methods: Iterable[FunctionDef] | ASTNodes[FunctionDef] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize ClassDefStmt instance.
    parameters:
      name:
        type: str
      bases:
        type: Iterable[Expr] | ASTNodes[Expr]
      decorators:
        type: Iterable[Expr] | ASTNodes[Expr]
      body:
        type: Block
      visibility:
        type: VisibilityKind
      is_abstract:
        type: bool
      metaclass:
        type: Optional[Expr]
      attributes:
        type: Iterable[VariableDeclaration] | ASTNodes[VariableDeclaration]
      methods:
        type: Iterable[FunctionDef] | ASTNodes[FunctionDef]
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(
        name=name,
        bases=bases,
        decorators=decorators,
        visibility=visibility,
        is_abstract=is_abstract,
        metaclass=metaclass,
        attributes=attributes,
        methods=methods,
        loc=loc,
        parent=parent,
    )

    if body != CLASS_BODY_DEFAULT:
        self.body = body
    else:
        self.body = copy.deepcopy(body)
        self.body.name = f"{name}_body"
    self.kind = ASTKind.ClassDefStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/classes.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    vis = dict(zip(("public", "private", "protected"), ("+", "-", "#")))
    abstract = ", abstract" if self.is_abstract else ""

    key = f"CLASS-DEF[{vis[self.visibility.name]}{self.name}{abstract}]"
    value = self._get_struct_wrapper(simplified)

    if self.body != CLASS_BODY_DEFAULT:
        value["body"] = self.body.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)
    )

EnumDeclStmt

EnumDeclStmt(
    name: str,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    visibility: VisibilityKind = public,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/classes.py
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
def __init__(
    self,
    name: str,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    visibility: VisibilityKind = VisibilityKind.public,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize EnumDeclStmt instance.
    parameters:
      name:
        type: str
      attributes:
        type: Iterable[VariableDeclaration] | ASTNodes[VariableDeclaration]
      visibility:
        type: VisibilityKind
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.name = name

    if isinstance(attributes, ASTNodes):
        self.attributes = attributes
    else:
        self.attributes = ASTNodes[VariableDeclaration]()
        for a in attributes:
            self.attributes.append(a)

    self.visibility = visibility
    self.kind = ASTKind.EnumDeclStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/classes.py
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    vis = dict(zip(("public", "private", "protected"), ("+", "-", "#")))
    key = f"ENUM-DECL[{vis[self.visibility.name]}{self.name}]"

    attrs_dict: ReprStruct = {}
    if self.attributes:
        attrs_dict = {"attributes": self.attributes.get_struct(simplified)}

    value = {
        **cast(DictDataTypesStruct, attrs_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)
    )

StructDeclStmt

StructDeclStmt(
    name: str,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    methods: Iterable[FunctionDef]
    | ASTNodes[FunctionDef] = [],
    visibility: VisibilityKind = public,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/classes.py
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
def __init__(
    self,
    name: str,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    methods: Iterable[FunctionDef] | ASTNodes[FunctionDef] = [],
    visibility: VisibilityKind = VisibilityKind.public,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize StructDeclStmt instance.
    parameters:
      name:
        type: str
      attributes:
        type: Iterable[VariableDeclaration] | ASTNodes[VariableDeclaration]
      decorators:
        type: Iterable[Expr] | ASTNodes[Expr]
      methods:
        type: Iterable[FunctionDef] | ASTNodes[FunctionDef]
      visibility:
        type: VisibilityKind
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.name = name

    if isinstance(attributes, ASTNodes):
        self.attributes = attributes
    else:
        self.attributes = ASTNodes[VariableDeclaration]()
        for a in attributes:
            self.attributes.append(a)

    if isinstance(decorators, ASTNodes):
        self.decorators = decorators
    else:
        self.decorators = ASTNodes[Expr]()
        for decorator in decorators:
            self.decorators.append(decorator)

    if isinstance(methods, ASTNodes):
        self.methods = methods
    else:
        self.methods = ASTNodes[FunctionDef]()
        for m in methods:
            self.methods.append(m)

    self.visibility = visibility
    self.kind = ASTKind.StructDeclStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/classes.py
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    vis = dict(zip(("public", "private", "protected"), ("+", "-", "#")))
    key = f"STRUCT-DECL[{vis[self.visibility.name]}{self.name}]"

    decors_dict: ReprStruct = {}
    if self.decorators:
        decors_dict = {
            "decorators": self.decorators.get_struct(simplified)
        }

    attrs_dict: ReprStruct = {}
    if self.attributes:
        attrs_dict = {"attributes": self.attributes.get_struct(simplified)}

    methods_dict: ReprStruct = {}
    if self.methods:
        methods_dict = {"methods": self.methods.get_struct(simplified)}

    value: DictDataTypesStruct = {
        **cast(DictDataTypesStruct, decors_dict),
        **cast(DictDataTypesStruct, attrs_dict),
        **cast(DictDataTypesStruct, methods_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)
    )

StructDefStmt

StructDefStmt(
    name: str,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    methods: Iterable[FunctionDef]
    | ASTNodes[FunctionDef] = [],
    visibility: VisibilityKind = public,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: StructDeclStmt

Methods:

Source code in packages/astx/src/astx/classes.py
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
def __init__(
    self,
    name: str,
    attributes: Iterable[VariableDeclaration]
    | ASTNodes[VariableDeclaration] = [],
    decorators: Iterable[Expr] | ASTNodes[Expr] = [],
    methods: Iterable[FunctionDef] | ASTNodes[FunctionDef] = [],
    visibility: VisibilityKind = VisibilityKind.public,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize StructDefStmt instance.
    parameters:
      name:
        type: str
      attributes:
        type: Iterable[VariableDeclaration] | ASTNodes[VariableDeclaration]
      decorators:
        type: Iterable[Expr] | ASTNodes[Expr]
      methods:
        type: Iterable[FunctionDef] | ASTNodes[FunctionDef]
      visibility:
        type: VisibilityKind
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(
        name=name,
        attributes=attributes,
        decorators=decorators,
        methods=methods,
        visibility=visibility,
        loc=loc,
        parent=parent,
    )
    self.kind = ASTKind.StructDefStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/classes.py
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    vis = dict(zip(("public", "private", "protected"), ("+", "-", "#")))
    key = f"STRUCT-DEF[{vis[self.visibility.name]}{self.name}]"

    decors_dict: ReprStruct = {}
    if self.decorators:
        decors_dict = {
            "decorators": self.decorators.get_struct(simplified)
        }

    attrs_dict: ReprStruct = {}
    if self.attributes:
        attrs_dict = {"attributes": self.attributes.get_struct(simplified)}

    methods_dict: ReprStruct = {}
    if self.methods:
        methods_dict = {"methods": self.methods.get_struct(simplified)}

    value: DictDataTypesStruct = {
        **cast(DictDataTypesStruct, decors_dict),
        **cast(DictDataTypesStruct, attrs_dict),
        **cast(DictDataTypesStruct, methods_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)
    )