mapFolding 0.9.5__py3-none-any.whl → 0.10.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. mapFolding/someAssemblyRequired/Z0Z_makeSomeModules.py +43 -42
  2. mapFolding/someAssemblyRequired/__init__.py +12 -21
  3. mapFolding/someAssemblyRequired/_astTypes.py +117 -0
  4. mapFolding/someAssemblyRequired/_theTypes.py +4 -27
  5. mapFolding/someAssemblyRequired/_toolBe.py +524 -0
  6. mapFolding/someAssemblyRequired/_toolDOT.py +493 -0
  7. mapFolding/someAssemblyRequired/_toolGrab.py +653 -0
  8. mapFolding/someAssemblyRequired/_toolIfThis.py +193 -0
  9. mapFolding/someAssemblyRequired/_toolMake.py +339 -0
  10. mapFolding/someAssemblyRequired/_toolThen.py +63 -0
  11. mapFolding/someAssemblyRequired/_toolboxAST.py +3 -3
  12. mapFolding/someAssemblyRequired/_toolboxContainers.py +4 -4
  13. mapFolding/someAssemblyRequired/makeJobTheorem2Numba.py +9 -9
  14. mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +9 -9
  15. mapFolding/someAssemblyRequired/toolboxNumba.py +1 -1
  16. mapFolding/someAssemblyRequired/transformationTools.py +39 -36
  17. mapFolding/toolFactory/astFactory.py +493 -0
  18. mapFolding/toolFactory/astFactory_annex.py +63 -0
  19. mapFolding/toolFactory/astFactory_docstrings.py +63 -0
  20. {mapfolding-0.9.5.dist-info → mapfolding-0.10.0.dist-info}/METADATA +2 -1
  21. {mapfolding-0.9.5.dist-info → mapfolding-0.10.0.dist-info}/RECORD +25 -18
  22. {mapfolding-0.9.5.dist-info → mapfolding-0.10.0.dist-info}/WHEEL +1 -1
  23. mapFolding/someAssemblyRequired/_tool_Make.py +0 -132
  24. mapFolding/someAssemblyRequired/_tool_Then.py +0 -152
  25. mapFolding/someAssemblyRequired/_toolboxAntecedents.py +0 -404
  26. {mapfolding-0.9.5.dist-info → mapfolding-0.10.0.dist-info}/entry_points.txt +0 -0
  27. {mapfolding-0.9.5.dist-info → mapfolding-0.10.0.dist-info}/licenses/LICENSE +0 -0
  28. {mapfolding-0.9.5.dist-info → mapfolding-0.10.0.dist-info}/top_level.txt +0 -0
@@ -1,404 +0,0 @@
1
- """
2
- AST Node Predicate and Access Utilities for Pattern Matching and Traversal
3
-
4
- This module provides utilities for accessing and matching AST nodes in a consistent way. It contains three primary
5
- classes:
6
-
7
- 1. DOT: Provides consistent accessor methods for AST node attributes across different node types, simplifying the access
8
- to node properties.
9
-
10
- 2. be: Offers type-guard functions that verify AST node types, enabling safe type narrowing for static type checking and
11
- improving code safety.
12
-
13
- 3. ifThis: Contains predicate functions for matching AST nodes based on various criteria, enabling precise targeting of
14
- nodes for analysis or transformation.
15
-
16
- These utilities form the foundation of the pattern-matching component in the AST manipulation framework, working in
17
- conjunction with the NodeChanger and NodeTourist classes to enable precise and targeted code transformations. Together,
18
- they implement a declarative approach to AST manipulation that separates node identification (ifThis), type verification
19
- (be), and data access (DOT).
20
- """
21
-
22
- from collections.abc import Callable
23
- from mapFolding.someAssemblyRequired import (
24
- ast_Identifier,
25
- astClassHasDOTbody,
26
- astClassHasDOTbody_expr,
27
- astClassHasDOTbodyList_stmt,
28
- astClassHasDOTnameNotName,
29
- astClassHasDOTnameNotNameAlways,
30
- astClassHasDOTnameNotNameOptionally,
31
- astClassHasDOTtarget,
32
- astClassHasDOTtarget_expr,
33
- astClassHasDOTtargetAttributeNameSubscript,
34
- astClassHasDOTvalue,
35
- astClassHasDOTvalue_expr,
36
- astClassHasDOTvalue_exprNone,
37
- ImaCallToName,
38
- )
39
- from typing import Any, overload, TypeGuard
40
- import ast
41
-
42
- class DOT:
43
- """
44
- Access attributes and sub-nodes of AST elements via consistent accessor methods.
45
-
46
- The DOT class provides static methods to access specific attributes of different types of AST nodes in a consistent
47
- way. This simplifies attribute access across various node types and improves code readability by abstracting the
48
- underlying AST structure details.
49
-
50
- DOT is designed for safe, read-only access to node properties, unlike the grab class which is designed for modifying
51
- node attributes.
52
- """
53
- @staticmethod
54
- @overload
55
- def annotation(node: ast.AnnAssign) -> ast.expr:...
56
- @staticmethod
57
- @overload
58
- def annotation(node: ast.arg) -> ast.expr | None:...
59
- @staticmethod
60
- def annotation(node: ast.AnnAssign | ast.arg) -> ast.expr | None:
61
- return node.annotation
62
-
63
- @staticmethod
64
- @overload
65
- def arg(node: ast.arg) -> ast_Identifier:...
66
- @staticmethod
67
- @overload
68
- def arg(node: ast.keyword) -> ast_Identifier | None:...
69
- @staticmethod
70
- def arg(node: ast.arg | ast.keyword) -> ast_Identifier | None:
71
- return node.arg
72
-
73
- @staticmethod
74
- def attr(node: ast.Attribute) -> ast_Identifier:
75
- return node.attr
76
-
77
- @staticmethod
78
- @overload
79
- def body(node: astClassHasDOTbodyList_stmt) -> list[ast.stmt]:...
80
- @staticmethod
81
- @overload
82
- def body(node: astClassHasDOTbody_expr) -> ast.expr:...
83
- @staticmethod
84
- def body(node: astClassHasDOTbody) -> ast.expr | list[ast.stmt]:
85
- return node.body
86
-
87
- @staticmethod
88
- @overload
89
- def func(node: ImaCallToName) -> ast.Name:...
90
- @staticmethod
91
- @overload
92
- def func(node: ast.Call) -> ast.expr:...
93
- @staticmethod
94
- def func(node: ast.Call | ImaCallToName) -> ast.expr | ast.Name:
95
- return node.func
96
-
97
- @staticmethod
98
- def id(node: ast.Name) -> ast_Identifier:
99
- return node.id
100
-
101
- @staticmethod
102
- @overload
103
- def name(node: astClassHasDOTnameNotNameAlways) -> ast_Identifier:...
104
- @staticmethod
105
- @overload
106
- def name(node: astClassHasDOTnameNotNameOptionally) -> ast_Identifier | None:...
107
- @staticmethod
108
- def name(node: astClassHasDOTnameNotName) -> ast_Identifier | None:
109
- return node.name
110
-
111
- @staticmethod
112
- @overload
113
- def target(node: ast.NamedExpr) -> ast.Name:...
114
- @staticmethod
115
- @overload
116
- def target(node: astClassHasDOTtarget_expr) -> ast.expr:...
117
- @staticmethod
118
- @overload
119
- def target(node: astClassHasDOTtargetAttributeNameSubscript) -> ast.Attribute | ast.Name | ast.Subscript:...
120
- @staticmethod
121
- def target(node: astClassHasDOTtarget) -> ast.Attribute | ast.expr | ast.Name | ast.Subscript:
122
- return node.target
123
-
124
- @staticmethod
125
- @overload
126
- def value(node: ast.Constant) -> Any:...
127
- @staticmethod
128
- @overload
129
- def value(node: ast.MatchSingleton) -> bool | None:...
130
- @staticmethod
131
- @overload
132
- def value(node: astClassHasDOTvalue_expr) -> ast.expr:...
133
- @staticmethod
134
- @overload
135
- def value(node: astClassHasDOTvalue_exprNone) -> ast.expr | None:...
136
- @staticmethod
137
- def value(node: astClassHasDOTvalue) -> Any | ast.expr | bool | None:
138
- return node.value
139
-
140
- class be:
141
- """
142
- Provide type-guard functions for safely verifying AST node types during manipulation.
143
-
144
- The be class contains static methods that perform runtime type verification of AST nodes, returning TypeGuard
145
- results that enable static type checkers to narrow node types in conditional branches. These type-guards:
146
-
147
- 1. Improve code safety by preventing operations on incompatible node types.
148
- 2. Enable IDE tooling to provide better autocompletion and error detection.
149
- 3. Document expected node types in a way that's enforced by the type system.
150
- 4. Support pattern-matching workflows where node types must be verified before access.
151
-
152
- When used with conditional statements, these type-guards allow for precise, type-safe manipulation of AST nodes
153
- while maintaining full static type checking capabilities, even in complex transformation scenarios.
154
- """
155
- @staticmethod
156
- def AnnAssign(node: ast.AST) -> TypeGuard[ast.AnnAssign]:
157
- return isinstance(node, ast.AnnAssign)
158
-
159
- @staticmethod
160
- def arg(node: ast.AST) -> TypeGuard[ast.arg]:
161
- return isinstance(node, ast.arg)
162
-
163
- @staticmethod
164
- def Assign(node: ast.AST) -> TypeGuard[ast.Assign]:
165
- return isinstance(node, ast.Assign)
166
-
167
- @staticmethod
168
- def Attribute(node: ast.AST) -> TypeGuard[ast.Attribute]:
169
- return isinstance(node, ast.Attribute)
170
-
171
- @staticmethod
172
- def AugAssign(node: ast.AST) -> TypeGuard[ast.AugAssign]:
173
- return isinstance(node, ast.AugAssign)
174
-
175
- @staticmethod
176
- def Call(node: ast.AST) -> TypeGuard[ast.Call]:
177
- return isinstance(node, ast.Call)
178
-
179
- @staticmethod
180
- def ClassDef(node: ast.AST) -> TypeGuard[ast.ClassDef]:
181
- return isinstance(node, ast.ClassDef)
182
-
183
- @staticmethod
184
- def Compare(node: ast.AST) -> TypeGuard[ast.Compare]:
185
- return isinstance(node, ast.Compare)
186
-
187
- @staticmethod
188
- def Constant(node: ast.AST) -> TypeGuard[ast.Constant]:
189
- return isinstance(node, ast.Constant)
190
-
191
- @staticmethod
192
- def FunctionDef(node: ast.AST) -> TypeGuard[ast.FunctionDef]:
193
- return isinstance(node, ast.FunctionDef)
194
-
195
- @staticmethod
196
- def keyword(node: ast.AST) -> TypeGuard[ast.keyword]:
197
- return isinstance(node, ast.keyword)
198
-
199
- @staticmethod
200
- def If(node: ast.AST) -> TypeGuard[ast.If]:
201
- return isinstance(node, ast.If)
202
-
203
- @staticmethod
204
- def Gt(node: ast.AST) -> TypeGuard[ast.Gt]:
205
- return isinstance(node, ast.Gt)
206
-
207
- @staticmethod
208
- def LtE(node: ast.AST) -> TypeGuard[ast.LtE]:
209
- return isinstance(node, ast.LtE)
210
-
211
- @staticmethod
212
- def Name(node: ast.AST) -> TypeGuard[ast.Name]:
213
- return isinstance(node, ast.Name)
214
-
215
- @staticmethod
216
- def Not(node: ast.AST) -> TypeGuard[ast.Not]:
217
- return isinstance(node, ast.Not)
218
-
219
- @staticmethod
220
- def Return(node: ast.AST) -> TypeGuard[ast.Return]:
221
- return isinstance(node, ast.Return)
222
-
223
- @staticmethod
224
- def Starred(node: ast.AST) -> TypeGuard[ast.Starred]:
225
- return isinstance(node, ast.Starred)
226
-
227
- @staticmethod
228
- def Subscript(node: ast.AST) -> TypeGuard[ast.Subscript]:
229
- return isinstance(node, ast.Subscript)
230
-
231
- @staticmethod
232
- def Tuple(node: ast.AST) -> TypeGuard[ast.Tuple]:
233
- return isinstance(node, ast.Tuple)
234
-
235
- @staticmethod
236
- def UnaryOp(node: ast.AST) -> TypeGuard[ast.UnaryOp]:
237
- return isinstance(node, ast.UnaryOp)
238
-
239
- @staticmethod
240
- def While(node: ast.AST) -> TypeGuard[ast.While]:
241
- return isinstance(node, ast.While)
242
-
243
- class ifThis:
244
- """
245
- Provide predicate functions for matching and filtering AST nodes based on various criteria.
246
-
247
- The ifThis class contains static methods that generate predicate functions used to test whether AST nodes match
248
- specific criteria. These predicates can be used with NodeChanger and NodeTourist to identify and process specific
249
- patterns in the AST.
250
-
251
- The class provides predicates for matching various node types, attributes, identifiers, and structural patterns,
252
- enabling precise targeting of AST elements for analysis or transformation.
253
- """
254
- @staticmethod
255
- def _Identifier(identifier: ast_Identifier) -> Callable[[ast_Identifier | None], TypeGuard[ast_Identifier] | bool]:
256
- return lambda node: node == identifier
257
- @staticmethod
258
- def _nested_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool]:
259
- def workhorse(node: ast.AST) -> TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool:
260
- return ifThis.isName_Identifier(identifier)(node) or ifThis.isAttribute_Identifier(identifier)(node) or ifThis.isSubscript_Identifier(identifier)(node) or ifThis.isStarred_Identifier(identifier)(node)
261
- return workhorse
262
-
263
- @staticmethod
264
- def is_arg_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.arg] | bool]:
265
- """see also `isArgument_Identifier`"""
266
- return lambda node: be.arg(node) and ifThis._Identifier(identifier)(DOT.arg(node))
267
- @staticmethod
268
- def is_keyword_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.keyword] | bool]:
269
- """see also `isArgument_Identifier`"""
270
- return lambda node: be.keyword(node) and ifThis._Identifier(identifier)(DOT.arg(node))
271
-
272
- @staticmethod
273
- def isAnnAssign_targetIs(targetPredicate: Callable[[ast.expr], TypeGuard[ast.expr] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
274
- def workhorse(node: ast.AST) -> TypeGuard[ast.AnnAssign] | bool:
275
- return be.AnnAssign(node) and targetPredicate(DOT.target(node))
276
- return workhorse
277
-
278
- @staticmethod
279
- def isArgument_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.arg | ast.keyword] | bool]:
280
- return lambda node: (be.arg(node) or be.keyword(node)) and ifThis._Identifier(identifier)(DOT.arg(node))
281
-
282
- @staticmethod
283
- def isAssignAndTargets0Is(targets0Predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
284
- """node is Assign and node.targets[0] matches `targets0Predicate`."""
285
- return lambda node: be.Assign(node) and targets0Predicate(node.targets[0])
286
- @staticmethod
287
- def isAssignAndValueIs(valuePredicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.Assign] | bool]:
288
- """node is ast.Assign and node.value matches `valuePredicate`. """
289
- return lambda node: be.Assign(node) and valuePredicate(DOT.value(node))
290
-
291
- @staticmethod
292
- def isAttribute_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
293
- """node is `ast.Attribute` and the top-level `ast.Name` is `identifier`"""
294
- def workhorse(node: ast.AST) -> TypeGuard[ast.Attribute]:
295
- return be.Attribute(node) and ifThis._nested_Identifier(identifier)(DOT.value(node))
296
- return workhorse
297
- @staticmethod
298
- def isAttributeName(node: ast.AST) -> TypeGuard[ast.Attribute]:
299
- """ Displayed as Name.attribute."""
300
- return be.Attribute(node) and be.Name(DOT.value(node))
301
- @staticmethod
302
- def isAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
303
- return lambda node: ifThis.isAttributeName(node) and ifThis.isName_Identifier(namespace)(DOT.value(node)) and ifThis._Identifier(identifier)(DOT.attr(node))
304
-
305
- @staticmethod
306
- def isAttributeNamespace_IdentifierGreaterThan0(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Compare] | bool]:
307
- return lambda node: (be.Compare(node)
308
- and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(node.left)
309
- and be.Gt(node.ops[0])
310
- and ifThis.isConstant_value(0)(node.comparators[0]))
311
-
312
- @staticmethod
313
- def isUnaryNotAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.UnaryOp] | bool]:
314
- return lambda node: (be.UnaryOp(node)
315
- and be.Not(node.op)
316
- and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(node.operand))
317
-
318
- @staticmethod
319
- def isIfUnaryNotAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.If] | bool]:
320
- return lambda node: (be.If(node)
321
- and ifThis.isUnaryNotAttributeNamespace_Identifier(namespace, identifier)(node.test))
322
-
323
- @staticmethod
324
- def isIfAttributeNamespace_IdentifierGreaterThan0(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.If] | bool]:
325
- return lambda node: (be.If(node)
326
- and ifThis.isAttributeNamespace_IdentifierGreaterThan0(namespace, identifier)(node.test))
327
-
328
- @staticmethod
329
- def isWhileAttributeNamespace_IdentifierGreaterThan0(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.While] | bool]:
330
- return lambda node: (be.While(node)
331
- and ifThis.isAttributeNamespace_IdentifierGreaterThan0(namespace, identifier)(node.test))
332
-
333
- @staticmethod
334
- def isAttributeNamespace_IdentifierLessThanOrEqual(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Compare] | bool]:
335
- return lambda node: (be.Compare(node)
336
- and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(node.left)
337
- and be.LtE(node.ops[0]))
338
-
339
- @staticmethod
340
- def isAugAssign_targetIs(targetPredicate: Callable[[ast.expr], TypeGuard[ast.expr] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AugAssign] | bool]:
341
- def workhorse(node: ast.AST) -> TypeGuard[ast.AugAssign] | bool:
342
- return be.AugAssign(node) and targetPredicate(DOT.target(node))
343
- return workhorse
344
-
345
- @staticmethod
346
- def isCall_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ImaCallToName] | bool]:
347
- def workhorse(node: ast.AST) -> TypeGuard[ImaCallToName] | bool:
348
- return ifThis.isCallToName(node) and ifThis._Identifier(identifier)(DOT.id(DOT.func(node)))
349
- return workhorse
350
-
351
- @staticmethod
352
- def isCallAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
353
- def workhorse(node: ast.AST) -> TypeGuard[ast.Call] | bool:
354
- return be.Call(node) and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(DOT.func(node))
355
- return workhorse
356
- @staticmethod
357
- def isCallToName(node: ast.AST) -> TypeGuard[ImaCallToName]:
358
- return be.Call(node) and be.Name(DOT.func(node))
359
-
360
- @staticmethod
361
- def isClassDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.ClassDef] | bool]:
362
- return lambda node: be.ClassDef(node) and ifThis._Identifier(identifier)(DOT.name(node))
363
-
364
- @staticmethod
365
- def isConstant_value(value: Any) -> Callable[[ast.AST], TypeGuard[ast.Constant] | bool]:
366
- return lambda node: be.Constant(node) and DOT.value(node) == value
367
-
368
- @staticmethod
369
- def isFunctionDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.FunctionDef] | bool]:
370
- return lambda node: be.FunctionDef(node) and ifThis._Identifier(identifier)(DOT.name(node))
371
-
372
- @staticmethod
373
- def isName_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Name] | bool]:
374
- return lambda node: be.Name(node) and ifThis._Identifier(identifier)(DOT.id(node))
375
-
376
- @staticmethod
377
- def isStarred_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Starred] | bool]:
378
- """node is `ast.Starred` and the top-level `ast.Name` is `identifier`"""
379
- def workhorse(node: ast.AST) -> TypeGuard[ast.Starred]:
380
- return be.Starred(node) and ifThis._nested_Identifier(identifier)(DOT.value(node))
381
- return workhorse
382
- @staticmethod
383
- def isSubscript_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Subscript] | bool]:
384
- """node is `ast.Subscript` and the top-level `ast.Name` is `identifier`"""
385
- def workhorse(node: ast.AST) -> TypeGuard[ast.Subscript]:
386
- return be.Subscript(node) and ifThis._nested_Identifier(identifier)(DOT.value(node))
387
- return workhorse
388
-
389
- @staticmethod
390
- def matchesMeButNotAnyDescendant(predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], bool]:
391
- return lambda node: predicate(node) and ifThis.matchesNoDescendant(predicate)(node)
392
- @staticmethod
393
- def matchesNoDescendant(predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], bool]:
394
- def workhorse(node: ast.AST) -> bool:
395
- for descendant in ast.walk(node):
396
- if descendant is not node and predicate(descendant):
397
- return False
398
- return True
399
- return workhorse
400
-
401
- @staticmethod
402
- def Z0Z_unparseIs(astAST: ast.AST) -> Callable[[ast.AST], bool]:
403
- def workhorse(node: ast.AST) -> bool: return ast.unparse(node) == ast.unparse(astAST)
404
- return workhorse