Skip to content

comprehensions

Classes:

Comprehension

Comprehension(
    generators: Iterable[ComprehensionClause]
    | ASTNodes[ComprehensionClause],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Expr

Methods:

Source code in packages/astx/src/astx/comprehensions.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def __init__(
    self,
    generators: (
        Iterable[ComprehensionClause] | ASTNodes[ComprehensionClause]
    ),
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)

    if isinstance(generators, ASTNodes):
        self.generators = generators
    elif isinstance(generators, Iterable):
        self.generators = ASTNodes[ComprehensionClause]()
        for generator in generators:
            self.generators.append(generator)

get_struct abstractmethod

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/comprehensions.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@abstractmethod
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    value: ReprStruct = {
        "generators": self.generators.get_struct(simplified),
    }
    key = f"{self}" if not simplified else f"{self}#{id(self)}"
    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)
    )

ComprehensionClause

ComprehensionClause(
    target: Expr,
    iterable: Expr,
    conditions: Optional[
        Iterable[Expr] | ASTNodes[Expr]
    ] = None,
    is_async: bool = False,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Expr

Methods:

Source code in packages/astx/src/astx/comprehensions.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def __init__(
    self,
    target: Expr,
    iterable: Expr,
    conditions: Optional[Iterable[Expr] | ASTNodes[Expr]] = None,
    is_async: bool = False,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.target = target
    self.iterable = iterable
    self.is_async = is_async
    self.kind = ASTKind.ComprehensionKind

    if isinstance(conditions, ASTNodes):
        self.conditions = conditions
    elif isinstance(conditions, Iterable):
        self.conditions = ASTNodes()
        for condition in conditions:
            self.conditions.append(condition)
    else:
        self.conditions = ASTNodes[Expr]()

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/comprehensions.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    conditions = (
        {"conditions": self.conditions.get_struct(simplified)}
        if self.conditions.nodes
        else {}
    )

    value: ReprStruct = {
        "target": self.target.get_struct(simplified),
        "iterable": self.iterable.get_struct(simplified),
        **conditions,
    }

    key = f"{self}" if not simplified else f"{self}#{id(self)}"
    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)
    )

GeneratorExpr

GeneratorExpr(
    element: Expr,
    generators: Iterable[ComprehensionClause]
    | ASTNodes[ComprehensionClause],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Comprehension

Methods:

Source code in packages/astx/src/astx/comprehensions.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
def __init__(
    self,
    element: Expr,
    generators: Iterable[ComprehensionClause]
    | ASTNodes[ComprehensionClause],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the GeneratorExpr instance.
    parameters:
      element:
        type: Expr
      generators:
        type: Iterable[ComprehensionClause] | ASTNodes[ComprehensionClause]
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(generators=generators, loc=loc, parent=parent)
    self.element = element
    self.kind = ASTKind.GeneratorExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/comprehensions.py
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"GENERATOR-EXPR#{id(self)}" if simplified else "GENERATOR-EXPR"
    value: ReprStruct = {
        "element": self.element.get_struct(simplified),
        "generators": self.generators.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)
    )

ListComprehension

ListComprehension(
    element: Expr,
    generators: ASTNodes[ComprehensionClause]
    | Iterable[ComprehensionClause] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Comprehension

Methods:

Source code in packages/astx/src/astx/comprehensions.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def __init__(
    self,
    element: Expr,
    generators: ASTNodes[ComprehensionClause]
    | Iterable[ComprehensionClause] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the GeneratorExpr instance.
    parameters:
      element:
        type: Expr
      generators:
        type: ASTNodes[ComprehensionClause] | Iterable[ComprehensionClause]
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(generators=generators, loc=loc, parent=parent)
    self.element = element

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/comprehensions.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"{self}"
    key += f"#{id(self)}" if simplified else ""

    generators = (
        {"generators": self.generators.get_struct(simplified)}
        if self.generators.nodes
        else {}
    )

    value: ReprStruct = {
        "element": self.element.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)
    )

SetComprehension

SetComprehension(
    element: Expr,
    generators: ASTNodes[ComprehensionClause]
    | Iterable[ComprehensionClause] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
)

Bases: Comprehension

Methods:

Source code in packages/astx/src/astx/comprehensions.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def __init__(
    self,
    element: Expr,
    generators: ASTNodes[ComprehensionClause]
    | Iterable[ComprehensionClause] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the SetComprehension instance.
    parameters:
      element:
        type: Expr
      generators:
        type: ASTNodes[ComprehensionClause] | Iterable[ComprehensionClause]
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(generators=generators, loc=loc, parent=parent)
    self.element = element
    self.kind = ASTKind.SetComprehensionKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/comprehensions.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    generators = (
        {"generators": self.generators.get_struct(simplified)}
        if self.generators.nodes
        else {}
    )

    value: ReprStruct = {
        "element": self.element.get_struct(simplified),
        **generators,
    }
    key = f"{self}#{id(self)}" if simplified else f"{self}"
    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)
    )