Skip to content

iterables

Provide small facade nodes for iterable constructs that are not yet present in upstream ASTx but are needed by IRx semantic analysis and lowering.

Classes:

DictComprehension

DictComprehension(
    key: Expr,
    value: Expr,
    generators: ASTNodes[ComprehensionClause]
    | Iterable[ComprehensionClause] = (),
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: ASTNodes | None = None,
)

Bases: Comprehension

Represent {key: value for ...} until upstream ASTx exposes a canonical dictionary-comprehension expression node. attributes: key: type: astx.Expr value: type: astx.Expr

Methods:

Source code in packages/irx/src/irx/astx/iterables.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def __init__(
    self,
    key: astx.Expr,
    value: astx.Expr,
    generators: (
        astx.ASTNodes[astx.ComprehensionClause]
        | Iterable[astx.ComprehensionClause]
    ) = (),
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: astx.ASTNodes | None = None,
) -> None:
    """
    title: Initialize one dictionary comprehension.
    parameters:
      key:
        type: astx.Expr
      value:
        type: astx.Expr
      generators:
        type: >-
          astx.ASTNodes[astx.ComprehensionClause] |
          Iterable[astx.ComprehensionClause]
      loc:
        type: SourceLocation
      parent:
        type: astx.ASTNodes | None
    """
    super().__init__(generators=generators, loc=loc, parent=parent)
    self.key = key
    self.value = value

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/irx/src/irx/astx/iterables.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the structured representation.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"{self}#{id(self)}" if simplified else str(self)
    generators = (
        {"generators": self.generators.get_struct(simplified)}
        if self.generators.nodes
        else {}
    )
    value = cast(
        ReprStruct,
        {
            "key": self.key.get_struct(simplified),
            "value": self.value.get_struct(simplified),
            **generators,
        },
    )
    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)
    )

ForInLoopStmt

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

Bases: StatementType

Model source forms equivalent to for target in iterable: body while preserving ASTx-compatible target, iterable, and body child nodes. attributes: target: type: astx.AST iterable: type: astx.AST body: type: astx.Block

Methods:

Source code in packages/irx/src/irx/astx/iterables.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,
    target: astx.AST,
    iterable: astx.AST,
    body: astx.Block,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: astx.ASTNodes | None = None,
) -> None:
    """
    title: Initialize one for-in statement.
    parameters:
      target:
        type: astx.AST
      iterable:
        type: astx.AST
      body:
        type: astx.Block
      loc:
        type: SourceLocation
      parent:
        type: astx.ASTNodes | None
    """
    super().__init__(loc=loc, parent=parent)
    self.target = target
    self.iterable = iterable
    self.body = body

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/irx/src/irx/astx/iterables.py
75
76
77
78
79
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) -> ReprStruct:
    """
    title: Return the structured representation.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = (
        f"FOR-IN-LOOP-STMT[{id(self)}]"
        if simplified
        else "FOR-IN-LOOP-STMT"
    )
    value = cast(
        ReprStruct,
        {
            "target": self.target.get_struct(simplified),
            "iterable": self.iterable.get_struct(simplified),
            "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)
    )