Skip to content

context

Provide a small facade node for Python-like with statements until upstream ASTx exposes a canonical context-manager construct.

Classes:

WithStmt

WithStmt(
    manager: AST,
    body: Block,
    target: AST | None = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: ASTNodes | None = None,
)

Bases: StatementType

Model source forms equivalent to with manager as target: body while keeping the manager expression, optional target, and body as regular ASTx child nodes. attributes: manager: type: astx.AST body: type: astx.Block target: type: astx.AST | None

Methods:

Source code in packages/irx/src/irx/astx/context.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def __init__(
    self,
    manager: astx.AST,
    body: astx.Block,
    target: astx.AST | None = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: astx.ASTNodes | None = None,
) -> None:
    """
    title: Initialize one with statement.
    parameters:
      manager:
        type: astx.AST
      body:
        type: astx.Block
      target:
        type: astx.AST | None
      loc:
        type: SourceLocation
      parent:
        type: astx.ASTNodes | None
    """
    super().__init__(loc=loc, parent=parent)
    self.manager = manager
    self.body = body
    self.target = target

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/irx/src/irx/astx/context.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the structured representation.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"WITH-STMT[{id(self)}]" if simplified else "WITH-STMT"
    target = (
        {"target": self.target.get_struct(simplified)}
        if self.target is not None
        else {}
    )
    value = cast(
        ReprStruct,
        {
            "manager": self.manager.get_struct(simplified),
            **target,
            "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)
    )