Skip to content

context_manager

Classes:

WithItem

WithItem(
    context_expr: Expr,
    instance_name: Optional[Identifier] = None,
)

Methods:

Source code in packages/astx/src/astx/context_manager.py
39
40
41
42
43
44
45
46
47
48
49
50
51
def __init__(
    self, context_expr: Expr, instance_name: Optional[Identifier] = None
) -> None:
    """
    title: Initialize a WithItem instance.
    parameters:
      context_expr:
        type: Expr
      instance_name:
        type: Optional[Identifier]
    """
    self.context_expr = context_expr
    self.instance_name = instance_name

get_struct

get_struct(
    simplified: bool = False,
) -> dict[str, DataTypesStruct]
Source code in packages/astx/src/astx/context_manager.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def get_struct(
    self, simplified: bool = False
) -> dict[str, DataTypesStruct]:
    """
    title: Get structural representation of the WithItem.
    parameters:
      simplified:
        type: bool
    returns:
      type: dict[str, DataTypesStruct]
    """
    key = (
        "CONTEXT"
        if not self.instance_name
        else f"CONTEXT[{self.context_expr!s}]"
    )
    value = cast(DataTypesStruct, self.context_expr.get_struct(simplified))
    return self._prepare_struct(key, value, simplified)

WithStmt

WithStmt(
    items: list[WithItem],
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/context_manager.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def __init__(
    self,
    items: list[WithItem],
    body: Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize WithStmt instance.
    parameters:
      items:
        type: list[WithItem]
      body:
        type: Block
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.items = items
    self.body = body
    self.kind = ASTKind.WithStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/context_manager.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Get structural representation of the WithStmt.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    items_structs: list[dict[str, DataTypesStruct]] = [
        item.get_struct(simplified) for item in self.items
    ]

    return cast(
        ReprStruct,
        {
            "WITH-STMT": {
                "items": items_structs,
                "body": self.body.get_struct(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)
    )