Skip to content

classes

Module for classes definitions/declarations.

Classes:

  • ClassDeclStmt

    AST class for class declaration.

  • ClassDefStmt

    AST class for class definition, including attributes and methods.

ClassDeclStmt

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

Bases: StatementType

AST class for class 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/classes.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def __init__(
    self,
    name: str,
    bases: Iterable[Expr] | ASTNodes = [],
    decorators: Iterable[Expr] | ASTNodes = [],
    visibility: VisibilityKind = VisibilityKind.public,
    is_abstract: bool = False,
    metaclass: Optional[Expr] = None,
    attributes: Iterable[VariableDeclaration] | ASTNodes = [],
    methods: Iterable[Function] | ASTNodes = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize ClassDeclStmt instance."""
    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()
        for decorator in decorators:
            self.decorators.append(decorator)

    self.attributes = ASTNodes()
    for a in attributes:
        self.attributes.append(a)

    self.methods = ASTNodes()
    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

Return the AST structure of the object.

Source code in src/astx/classes.py
144
145
146
147
148
149
150
151
152
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    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

Return an json string that represents the object.

Source code in src/astx/base.py
265
266
267
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
259
260
261
262
263
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)
    )

ClassDefStmt

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

Bases: ClassDeclStmt

AST class for class definition, including attributes and methods.

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/classes.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def __init__(
    self,
    name: str,
    bases: Iterable[Expr] | ASTNodes = [],
    decorators: Iterable[Expr] | ASTNodes = [],
    body: Block = CLASS_BODY_DEFAULT,
    visibility: VisibilityKind = VisibilityKind.public,
    is_abstract: bool = False,
    metaclass: Optional[Expr] = None,
    attributes: Iterable[VariableDeclaration] | ASTNodes = [],
    methods: Iterable[Function] | ASTNodes = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize ClassDefStmt instance."""
    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

Return the AST structure of the object.

Source code in src/astx/classes.py
209
210
211
212
213
214
215
216
217
218
219
220
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    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

Return an json string that represents the object.

Source code in src/astx/base.py
265
266
267
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
259
260
261
262
263
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)
    )