mapFolding 0.10.0__py3-none-any.whl → 0.11.1__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.
- mapFolding/__init__.py +12 -27
- mapFolding/someAssemblyRequired/Z0Z_makeSomeModules.py +9 -11
- mapFolding/someAssemblyRequired/__init__.py +20 -24
- mapFolding/someAssemblyRequired/_toolIfThis.py +5 -139
- mapFolding/someAssemblyRequired/_toolboxContainers.py +20 -293
- mapFolding/someAssemblyRequired/makeJobTheorem2Numba.py +6 -6
- mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +7 -6
- mapFolding/someAssemblyRequired/toolboxNumba.py +2 -2
- mapFolding/someAssemblyRequired/transformationTools.py +9 -216
- {mapfolding-0.10.0.dist-info → mapfolding-0.11.1.dist-info}/METADATA +2 -1
- {mapfolding-0.10.0.dist-info → mapfolding-0.11.1.dist-info}/RECORD +15 -27
- {mapfolding-0.10.0.dist-info → mapfolding-0.11.1.dist-info}/WHEEL +1 -1
- mapFolding/someAssemblyRequired/_astTypes.py +0 -117
- mapFolding/someAssemblyRequired/_theTypes.py +0 -34
- mapFolding/someAssemblyRequired/_toolBe.py +0 -524
- mapFolding/someAssemblyRequired/_toolDOT.py +0 -493
- mapFolding/someAssemblyRequired/_toolGrab.py +0 -653
- mapFolding/someAssemblyRequired/_toolMake.py +0 -339
- mapFolding/someAssemblyRequired/_toolThen.py +0 -63
- mapFolding/someAssemblyRequired/_toolboxAST.py +0 -57
- mapFolding/someAssemblyRequired/_toolboxPython.py +0 -188
- mapFolding/toolFactory/astFactory.py +0 -493
- mapFolding/toolFactory/astFactory_annex.py +0 -63
- mapFolding/toolFactory/astFactory_docstrings.py +0 -63
- {mapfolding-0.10.0.dist-info → mapfolding-0.11.1.dist-info}/entry_points.txt +0 -0
- {mapfolding-0.10.0.dist-info → mapfolding-0.11.1.dist-info}/licenses/LICENSE +0 -0
- {mapfolding-0.10.0.dist-info → mapfolding-0.11.1.dist-info}/top_level.txt +0 -0
|
@@ -18,294 +18,15 @@ The containers work in conjunction with transformation tools that manipulate the
|
|
|
18
18
|
specific optimizations and transformations.
|
|
19
19
|
"""
|
|
20
20
|
|
|
21
|
-
from collections import
|
|
22
|
-
from collections.abc import Callable, Sequence
|
|
21
|
+
from collections.abc import Callable
|
|
23
22
|
from copy import deepcopy
|
|
24
|
-
from
|
|
25
|
-
from mapFolding.someAssemblyRequired import ast_Identifier, DOT, IfThis, Make, NodeTourist, parseLogicalPath2astModule, str_nameDOTname, Then
|
|
23
|
+
from mapFolding.someAssemblyRequired import ast_Identifier, DOT, IfThis, Make, NodeTourist, parseLogicalPath2astModule, str_nameDOTname, Then, LedgerOfImports
|
|
26
24
|
from mapFolding.theSSOT import raiseIfNoneGitHubIssueNumber3, The
|
|
27
25
|
from pathlib import Path, PurePosixPath
|
|
28
|
-
from
|
|
26
|
+
from typing import Any, cast
|
|
29
27
|
import ast
|
|
30
28
|
import dataclasses
|
|
31
29
|
|
|
32
|
-
class LedgerOfImports:
|
|
33
|
-
"""
|
|
34
|
-
Track and manage import statements for programmatically generated code.
|
|
35
|
-
|
|
36
|
-
LedgerOfImports acts as a registry for import statements, maintaining a clean separation between the logical
|
|
37
|
-
structure of imports and their textual representation. It enables:
|
|
38
|
-
|
|
39
|
-
1. Tracking regular imports and import-from statements.
|
|
40
|
-
2. Adding imports programmatically during code transformation.
|
|
41
|
-
3. Merging imports from multiple sources.
|
|
42
|
-
4. Removing unnecessary or conflicting imports.
|
|
43
|
-
5. Generating optimized AST import nodes for the final code.
|
|
44
|
-
|
|
45
|
-
This class forms the foundation of dependency management in generated code, ensuring that all required libraries are
|
|
46
|
-
available without duplication or conflict.
|
|
47
|
-
"""
|
|
48
|
-
# TODO When resolving the ledger of imports, remove self-referential imports
|
|
49
|
-
|
|
50
|
-
def __init__(self, startWith: ast.AST | None = None, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
51
|
-
self.dictionaryImportFrom: dict[str_nameDOTname, list[tuple[ast_Identifier, ast_Identifier | None]]] = defaultdict(list)
|
|
52
|
-
self.listImport: list[str_nameDOTname] = []
|
|
53
|
-
self.type_ignores = [] if type_ignores is None else list(type_ignores)
|
|
54
|
-
if startWith:
|
|
55
|
-
self.walkThis(startWith)
|
|
56
|
-
|
|
57
|
-
def addAst(self, astImport____: ast.Import | ast.ImportFrom, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
58
|
-
match astImport____:
|
|
59
|
-
case ast.Import():
|
|
60
|
-
for alias in astImport____.names:
|
|
61
|
-
self.listImport.append(alias.name)
|
|
62
|
-
case ast.ImportFrom():
|
|
63
|
-
# TODO fix the mess created by `None` means '.'. I need a `str_nameDOTname` to replace '.'
|
|
64
|
-
if astImport____.module is None:
|
|
65
|
-
astImport____.module = '.'
|
|
66
|
-
for alias in astImport____.names:
|
|
67
|
-
self.dictionaryImportFrom[astImport____.module].append((alias.name, alias.asname))
|
|
68
|
-
case _:
|
|
69
|
-
raise ValueError(f"I received {type(astImport____) = }, but I can only accept {ast.Import} and {ast.ImportFrom}.")
|
|
70
|
-
if type_ignores:
|
|
71
|
-
self.type_ignores.extend(type_ignores)
|
|
72
|
-
|
|
73
|
-
def addImport_asStr(self, moduleWithLogicalPath: str_nameDOTname, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
74
|
-
self.listImport.append(moduleWithLogicalPath)
|
|
75
|
-
if type_ignores:
|
|
76
|
-
self.type_ignores.extend(type_ignores)
|
|
77
|
-
|
|
78
|
-
def addImportFrom_asStr(self, moduleWithLogicalPath: str_nameDOTname, name: ast_Identifier, asname: ast_Identifier | None = None, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
79
|
-
self.dictionaryImportFrom[moduleWithLogicalPath].append((name, asname))
|
|
80
|
-
if type_ignores:
|
|
81
|
-
self.type_ignores.extend(type_ignores)
|
|
82
|
-
|
|
83
|
-
def removeImportFromModule(self, moduleWithLogicalPath: str_nameDOTname) -> None:
|
|
84
|
-
"""Remove all imports from a specific module."""
|
|
85
|
-
self.removeImportFrom(moduleWithLogicalPath, None, None)
|
|
86
|
-
|
|
87
|
-
def removeImportFrom(self, moduleWithLogicalPath: str_nameDOTname, name: ast_Identifier | None, asname: ast_Identifier | None = None) -> None:
|
|
88
|
-
"""
|
|
89
|
-
name, asname Action
|
|
90
|
-
None, None : remove all matches for the module
|
|
91
|
-
ast_Identifier, ast_Identifier : remove exact matches
|
|
92
|
-
ast_Identifier, None : remove exact matches
|
|
93
|
-
None, ast_Identifier : remove all matches for asname and if entry_asname is None remove name == ast_Identifier
|
|
94
|
-
"""
|
|
95
|
-
if moduleWithLogicalPath in self.dictionaryImportFrom:
|
|
96
|
-
if name is None and asname is None:
|
|
97
|
-
# Remove all entries for the module
|
|
98
|
-
self.dictionaryImportFrom.pop(moduleWithLogicalPath)
|
|
99
|
-
else:
|
|
100
|
-
if name is None:
|
|
101
|
-
self.dictionaryImportFrom[moduleWithLogicalPath] = [(entry_name, entry_asname) for entry_name, entry_asname in self.dictionaryImportFrom[moduleWithLogicalPath]
|
|
102
|
-
if not (entry_asname == asname) and not (entry_asname is None and entry_name == asname)]
|
|
103
|
-
else:
|
|
104
|
-
self.dictionaryImportFrom[moduleWithLogicalPath] = [(entry_name, entry_asname) for entry_name, entry_asname in self.dictionaryImportFrom[moduleWithLogicalPath]
|
|
105
|
-
if not (entry_name == name and entry_asname == asname)]
|
|
106
|
-
if not self.dictionaryImportFrom[moduleWithLogicalPath]:
|
|
107
|
-
self.dictionaryImportFrom.pop(moduleWithLogicalPath)
|
|
108
|
-
|
|
109
|
-
def exportListModuleIdentifiers(self) -> list[ast_Identifier]:
|
|
110
|
-
listModuleIdentifiers: list[ast_Identifier] = list(self.dictionaryImportFrom.keys())
|
|
111
|
-
listModuleIdentifiers.extend(self.listImport)
|
|
112
|
-
return sorted(set(listModuleIdentifiers))
|
|
113
|
-
|
|
114
|
-
def makeList_ast(self) -> list[ast.ImportFrom | ast.Import]:
|
|
115
|
-
listImportFrom: list[ast.ImportFrom] = []
|
|
116
|
-
for moduleWithLogicalPath, listOfNameTuples in sorted(self.dictionaryImportFrom.items()):
|
|
117
|
-
listOfNameTuples = sorted(list(set(listOfNameTuples)), key=lambda nameTuple: nameTuple[0])
|
|
118
|
-
list_alias: list[ast.alias] = []
|
|
119
|
-
for name, asname in listOfNameTuples:
|
|
120
|
-
list_alias.append(Make.alias(name, asname))
|
|
121
|
-
if list_alias:
|
|
122
|
-
listImportFrom.append(Make.ImportFrom(moduleWithLogicalPath, list_alias))
|
|
123
|
-
list_astImport: list[ast.Import] = [Make.Import(moduleWithLogicalPath) for moduleWithLogicalPath in sorted(set(self.listImport))]
|
|
124
|
-
return listImportFrom + list_astImport
|
|
125
|
-
|
|
126
|
-
def update(self, *fromLedger: 'LedgerOfImports') -> None:
|
|
127
|
-
"""Update this ledger with imports from one or more other ledgers.
|
|
128
|
-
Parameters:
|
|
129
|
-
*fromLedger: One or more other `LedgerOfImports` objects from which to merge.
|
|
130
|
-
"""
|
|
131
|
-
updatedDictionary = updateExtendPolishDictionaryLists(self.dictionaryImportFrom, *(ledger.dictionaryImportFrom for ledger in fromLedger), destroyDuplicates=True, reorderLists=True)
|
|
132
|
-
self.dictionaryImportFrom = defaultdict(list, updatedDictionary)
|
|
133
|
-
for ledger in fromLedger:
|
|
134
|
-
self.listImport.extend(ledger.listImport)
|
|
135
|
-
self.type_ignores.extend(ledger.type_ignores)
|
|
136
|
-
|
|
137
|
-
def walkThis(self, walkThis: ast.AST, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
138
|
-
for nodeBuffalo in ast.walk(walkThis):
|
|
139
|
-
if isinstance(nodeBuffalo, (ast.Import, ast.ImportFrom)):
|
|
140
|
-
self.addAst(nodeBuffalo)
|
|
141
|
-
if type_ignores:
|
|
142
|
-
self.type_ignores.extend(type_ignores)
|
|
143
|
-
|
|
144
|
-
# Consolidate settings classes through inheritance https://github.com/hunterhogan/mapFolding/issues/15
|
|
145
|
-
@dataclasses.dataclass
|
|
146
|
-
class IngredientsFunction:
|
|
147
|
-
"""
|
|
148
|
-
Package a function definition with its import dependencies for code generation.
|
|
149
|
-
|
|
150
|
-
IngredientsFunction encapsulates an AST function definition along with all the imports required for that function to
|
|
151
|
-
operate correctly. This creates a modular, portable unit that can be:
|
|
152
|
-
|
|
153
|
-
1. Transformed independently (e.g., by applying Numba decorators).
|
|
154
|
-
2. Transplanted between modules while maintaining dependencies.
|
|
155
|
-
3. Combined with other functions to form complete modules.
|
|
156
|
-
4. Analyzed for optimization opportunities.
|
|
157
|
-
|
|
158
|
-
This class forms the primary unit of function manipulation in the code generation system, enabling targeted
|
|
159
|
-
transformations while preserving function dependencies.
|
|
160
|
-
|
|
161
|
-
Parameters:
|
|
162
|
-
astFunctionDef: The AST representation of the function definition
|
|
163
|
-
imports: Import statements needed by the function
|
|
164
|
-
type_ignores: Type ignore comments associated with the function
|
|
165
|
-
"""
|
|
166
|
-
astFunctionDef: ast.FunctionDef
|
|
167
|
-
imports: LedgerOfImports = dataclasses.field(default_factory=LedgerOfImports)
|
|
168
|
-
type_ignores: list[ast.TypeIgnore] = dataclasses.field(default_factory=list)
|
|
169
|
-
|
|
170
|
-
# Consolidate settings classes through inheritance https://github.com/hunterhogan/mapFolding/issues/15
|
|
171
|
-
@dataclasses.dataclass
|
|
172
|
-
class IngredientsModule:
|
|
173
|
-
"""
|
|
174
|
-
Assemble a complete Python module from its constituent AST components.
|
|
175
|
-
|
|
176
|
-
IngredientsModule provides a structured container for all elements needed to generate a complete Python module,
|
|
177
|
-
including:
|
|
178
|
-
|
|
179
|
-
1. Import statements aggregated from all module components.
|
|
180
|
-
2. Prologue code that runs before function definitions.
|
|
181
|
-
3. Function definitions with their dependencies.
|
|
182
|
-
4. Epilogue code that runs after function definitions.
|
|
183
|
-
5. Entry point code executed when the module runs as a script.
|
|
184
|
-
6. Type ignores and other annotations.
|
|
185
|
-
|
|
186
|
-
This class enables programmatic assembly of Python modules with a clear separation between different structural
|
|
187
|
-
elements, while maintaining the proper ordering and relationships between components.
|
|
188
|
-
|
|
189
|
-
The modular design allows transformations to be applied to specific parts of a module while preserving the overall
|
|
190
|
-
structure.
|
|
191
|
-
|
|
192
|
-
Parameters:
|
|
193
|
-
ingredientsFunction (None): One or more `IngredientsFunction` that will appended to `listIngredientsFunctions`.
|
|
194
|
-
"""
|
|
195
|
-
ingredientsFunction: dataclasses.InitVar[Sequence[IngredientsFunction] | IngredientsFunction | None] = None
|
|
196
|
-
|
|
197
|
-
# init var with an existing module? method to deconstruct an existing module?
|
|
198
|
-
|
|
199
|
-
# `body` attribute of `ast.Module`
|
|
200
|
-
"""NOTE
|
|
201
|
-
- Bare statements in `prologue` and `epilogue` are not 'protected' by `if __name__ == '__main__':` so they will be executed merely by loading the module.
|
|
202
|
-
- The dataclass has methods for modifying `prologue`, `epilogue`, and `launcher`.
|
|
203
|
-
- However, `prologue`, `epilogue`, and `launcher` are `ast.Module` (as opposed to `list[ast.stmt]`), so that you may use tools such as `ast.walk` and `ast.NodeVisitor` on the fields.
|
|
204
|
-
"""
|
|
205
|
-
imports: LedgerOfImports = dataclasses.field(default_factory=LedgerOfImports)
|
|
206
|
-
"""Modify this field using the methods in `LedgerOfImports`."""
|
|
207
|
-
prologue: ast.Module = Make.Module([],[])
|
|
208
|
-
"""Statements after the imports and before the functions in listIngredientsFunctions."""
|
|
209
|
-
listIngredientsFunctions: list[IngredientsFunction] = dataclasses.field(default_factory=list)
|
|
210
|
-
epilogue: ast.Module = Make.Module([],[])
|
|
211
|
-
"""Statements after the functions in listIngredientsFunctions and before `launcher`."""
|
|
212
|
-
launcher: ast.Module = Make.Module([],[])
|
|
213
|
-
"""`if __name__ == '__main__':`"""
|
|
214
|
-
|
|
215
|
-
# `ast.TypeIgnore` statements to supplement those in other fields; `type_ignores` is a parameter for `ast.Module` constructor
|
|
216
|
-
supplemental_type_ignores: list[ast.TypeIgnore] = dataclasses.field(default_factory=list)
|
|
217
|
-
|
|
218
|
-
def __post_init__(self, ingredientsFunction: Sequence[IngredientsFunction] | IngredientsFunction | None = None) -> None:
|
|
219
|
-
if ingredientsFunction is not None:
|
|
220
|
-
if isinstance(ingredientsFunction, IngredientsFunction):
|
|
221
|
-
self.appendIngredientsFunction(ingredientsFunction)
|
|
222
|
-
else:
|
|
223
|
-
self.appendIngredientsFunction(*ingredientsFunction)
|
|
224
|
-
|
|
225
|
-
def _append_astModule(self, self_astModule: ast.Module, astModule: ast.Module | None, statement: Sequence[ast.stmt] | ast.stmt | None, type_ignores: list[ast.TypeIgnore] | None) -> None:
|
|
226
|
-
"""Append one or more statements to `prologue`."""
|
|
227
|
-
list_body: list[ast.stmt] = []
|
|
228
|
-
listTypeIgnore: list[ast.TypeIgnore] = []
|
|
229
|
-
if astModule is not None and isinstance(astModule, ast.Module): # type: ignore
|
|
230
|
-
list_body.extend(astModule.body)
|
|
231
|
-
listTypeIgnore.extend(astModule.type_ignores)
|
|
232
|
-
if type_ignores is not None:
|
|
233
|
-
listTypeIgnore.extend(type_ignores)
|
|
234
|
-
if statement is not None:
|
|
235
|
-
if isinstance(statement, Sequence):
|
|
236
|
-
list_body.extend(statement)
|
|
237
|
-
else:
|
|
238
|
-
list_body.append(statement)
|
|
239
|
-
self_astModule.body.extend(list_body)
|
|
240
|
-
self_astModule.type_ignores.extend(listTypeIgnore)
|
|
241
|
-
ast.fix_missing_locations(self_astModule)
|
|
242
|
-
|
|
243
|
-
def appendPrologue(self, astModule: ast.Module | None = None, statement: Sequence[ast.stmt] | ast.stmt | None = None, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
244
|
-
"""Append one or more statements to `prologue`."""
|
|
245
|
-
self._append_astModule(self.prologue, astModule, statement, type_ignores)
|
|
246
|
-
|
|
247
|
-
def appendEpilogue(self, astModule: ast.Module | None = None, statement: Sequence[ast.stmt] | ast.stmt | None = None, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
248
|
-
"""Append one or more statements to `epilogue`."""
|
|
249
|
-
self._append_astModule(self.epilogue, astModule, statement, type_ignores)
|
|
250
|
-
|
|
251
|
-
def appendLauncher(self, astModule: ast.Module | None = None, statement: Sequence[ast.stmt] | ast.stmt | None = None, type_ignores: list[ast.TypeIgnore] | None = None) -> None:
|
|
252
|
-
"""Append one or more statements to `launcher`."""
|
|
253
|
-
self._append_astModule(self.launcher, astModule, statement, type_ignores)
|
|
254
|
-
|
|
255
|
-
def appendIngredientsFunction(self, *ingredientsFunction: IngredientsFunction) -> None:
|
|
256
|
-
"""Append one or more `IngredientsFunction`."""
|
|
257
|
-
for allegedIngredientsFunction in ingredientsFunction:
|
|
258
|
-
self.listIngredientsFunctions.append(allegedIngredientsFunction)
|
|
259
|
-
|
|
260
|
-
def removeImportFromModule(self, moduleWithLogicalPath: str_nameDOTname) -> None:
|
|
261
|
-
self.removeImportFrom(moduleWithLogicalPath, None, None)
|
|
262
|
-
"""Remove all imports from a specific module."""
|
|
263
|
-
|
|
264
|
-
def removeImportFrom(self, moduleWithLogicalPath: str_nameDOTname, name: ast_Identifier | None, asname: ast_Identifier | None = None) -> None:
|
|
265
|
-
"""
|
|
266
|
-
This method modifies all `LedgerOfImports` in this `IngredientsModule` and all `IngredientsFunction` in `listIngredientsFunctions`.
|
|
267
|
-
It is not a "blacklist", so the `import from` could be added after this modification.
|
|
268
|
-
"""
|
|
269
|
-
self.imports.removeImportFrom(moduleWithLogicalPath, name, asname)
|
|
270
|
-
for ingredientsFunction in self.listIngredientsFunctions:
|
|
271
|
-
ingredientsFunction.imports.removeImportFrom(moduleWithLogicalPath, name, asname)
|
|
272
|
-
|
|
273
|
-
def _consolidatedLedger(self) -> LedgerOfImports:
|
|
274
|
-
"""Consolidate all ledgers of imports."""
|
|
275
|
-
sherpaLedger = LedgerOfImports()
|
|
276
|
-
listLedgers: list[LedgerOfImports] = [self.imports]
|
|
277
|
-
for ingredientsFunction in self.listIngredientsFunctions:
|
|
278
|
-
listLedgers.append(ingredientsFunction.imports)
|
|
279
|
-
sherpaLedger.update(*listLedgers)
|
|
280
|
-
return sherpaLedger
|
|
281
|
-
|
|
282
|
-
@property
|
|
283
|
-
def list_astImportImportFrom(self) -> list[ast.Import | ast.ImportFrom]:
|
|
284
|
-
return self._consolidatedLedger().makeList_ast()
|
|
285
|
-
|
|
286
|
-
@property
|
|
287
|
-
def body(self) -> list[ast.stmt]:
|
|
288
|
-
list_stmt: list[ast.stmt] = []
|
|
289
|
-
list_stmt.extend(self.list_astImportImportFrom)
|
|
290
|
-
list_stmt.extend(self.prologue.body)
|
|
291
|
-
for ingredientsFunction in self.listIngredientsFunctions:
|
|
292
|
-
list_stmt.append(ingredientsFunction.astFunctionDef)
|
|
293
|
-
list_stmt.extend(self.epilogue.body)
|
|
294
|
-
list_stmt.extend(self.launcher.body)
|
|
295
|
-
# TODO `launcher`, if it exists, must start with `if __name__ == '__main__':` and be indented
|
|
296
|
-
return list_stmt
|
|
297
|
-
|
|
298
|
-
@property
|
|
299
|
-
def type_ignores(self) -> list[ast.TypeIgnore]:
|
|
300
|
-
listTypeIgnore: list[ast.TypeIgnore] = self.supplemental_type_ignores
|
|
301
|
-
listTypeIgnore.extend(self._consolidatedLedger().type_ignores)
|
|
302
|
-
listTypeIgnore.extend(self.prologue.type_ignores)
|
|
303
|
-
for ingredientsFunction in self.listIngredientsFunctions:
|
|
304
|
-
listTypeIgnore.extend(ingredientsFunction.type_ignores)
|
|
305
|
-
listTypeIgnore.extend(self.epilogue.type_ignores)
|
|
306
|
-
listTypeIgnore.extend(self.launcher.type_ignores)
|
|
307
|
-
return listTypeIgnore
|
|
308
|
-
|
|
309
30
|
# Consolidate settings classes through inheritance https://github.com/hunterhogan/mapFolding/issues/15
|
|
310
31
|
@dataclasses.dataclass
|
|
311
32
|
class RecipeSynthesizeFlow:
|
|
@@ -436,10 +157,10 @@ class ShatteredDataclass:
|
|
|
436
157
|
countingVariableName: ast.Name
|
|
437
158
|
"""AST name node representing the counting variable identifier."""
|
|
438
159
|
|
|
439
|
-
field2AnnAssign: dict[ast_Identifier, ast.AnnAssign | ast.Assign] = dataclasses.field(default_factory=dict)
|
|
160
|
+
field2AnnAssign: dict[ast_Identifier, ast.AnnAssign | ast.Assign] = dataclasses.field(default_factory=lambda: dict[ast_Identifier, ast.AnnAssign | ast.Assign]())
|
|
440
161
|
"""Maps field names to their corresponding AST call expressions."""
|
|
441
162
|
|
|
442
|
-
Z0Z_field2AnnAssign: dict[ast_Identifier, tuple[ast.AnnAssign | ast.Assign, str]] = dataclasses.field(default_factory=dict)
|
|
163
|
+
Z0Z_field2AnnAssign: dict[ast_Identifier, tuple[ast.AnnAssign | ast.Assign, str]] = dataclasses.field(default_factory=lambda: dict[ast_Identifier, tuple[ast.AnnAssign | ast.Assign, str]]())
|
|
443
164
|
|
|
444
165
|
fragments4AssignmentOrParameters: ast.Tuple = dummyTuple
|
|
445
166
|
"""AST tuple used as target for assignment to capture returned fragments."""
|
|
@@ -447,22 +168,22 @@ class ShatteredDataclass:
|
|
|
447
168
|
imports: LedgerOfImports = dataclasses.field(default_factory=LedgerOfImports)
|
|
448
169
|
"""Import records for the dataclass and its constituent parts."""
|
|
449
170
|
|
|
450
|
-
list_argAnnotated4ArgumentsSpecification: list[ast.arg] = dataclasses.field(default_factory=list)
|
|
171
|
+
list_argAnnotated4ArgumentsSpecification: list[ast.arg] = dataclasses.field(default_factory=lambda: list[ast.arg]())
|
|
451
172
|
"""Function argument nodes with annotations for parameter specification."""
|
|
452
173
|
|
|
453
|
-
list_keyword_field__field4init: list[ast.keyword] = dataclasses.field(default_factory=list)
|
|
174
|
+
list_keyword_field__field4init: list[ast.keyword] = dataclasses.field(default_factory=lambda: list[ast.keyword]())
|
|
454
175
|
"""Keyword arguments for dataclass initialization with field=field format."""
|
|
455
176
|
|
|
456
|
-
listAnnotations: list[ast.expr] = dataclasses.field(default_factory=list)
|
|
177
|
+
listAnnotations: list[ast.expr] = dataclasses.field(default_factory=lambda: list[ast.expr]())
|
|
457
178
|
"""Type annotations for each dataclass field."""
|
|
458
179
|
|
|
459
|
-
listName4Parameters: list[ast.Name] = dataclasses.field(default_factory=list)
|
|
180
|
+
listName4Parameters: list[ast.Name] = dataclasses.field(default_factory=lambda: list[ast.Name]())
|
|
460
181
|
"""Name nodes for each dataclass field used as function parameters."""
|
|
461
182
|
|
|
462
|
-
listUnpack: list[ast.AnnAssign] = dataclasses.field(default_factory=list)
|
|
183
|
+
listUnpack: list[ast.AnnAssign] = dataclasses.field(default_factory=lambda: list[ast.AnnAssign]())
|
|
463
184
|
"""Annotated assignment statements to extract fields from dataclass."""
|
|
464
185
|
|
|
465
|
-
map_stateDOTfield2Name: dict[ast.AST, ast.Name] = dataclasses.field(default_factory=dict)
|
|
186
|
+
map_stateDOTfield2Name: dict[ast.AST, ast.Name] = dataclasses.field(default_factory=lambda: dict[ast.AST, ast.Name]())
|
|
466
187
|
"""Maps AST expressions to Name nodes for find-replace operations."""
|
|
467
188
|
|
|
468
189
|
repack: ast.Assign = dummyAssign
|
|
@@ -531,7 +252,13 @@ class DeReConstructField2ast:
|
|
|
531
252
|
self.ast_keyword_field__field = Make.keyword(self.name, self.astName)
|
|
532
253
|
self.ast_nameDOTname = Make.Attribute(Make.Name(dataclassesDOTdataclassInstance_Identifier), self.name)
|
|
533
254
|
|
|
534
|
-
|
|
255
|
+
findThis = IfThis.isAnnAssign_targetIs(IfThis.isName_Identifier(self.name))
|
|
256
|
+
|
|
257
|
+
sherpa = NodeTourist(
|
|
258
|
+
findThis=findThis
|
|
259
|
+
, doThat=Then.extractIt(DOT.annotation)
|
|
260
|
+
).captureLastMatch(dataclassClassDef)
|
|
261
|
+
|
|
535
262
|
if sherpa is None: raise raiseIfNoneGitHubIssueNumber3
|
|
536
263
|
else: self.astAnnotation = sherpa
|
|
537
264
|
|
|
@@ -544,7 +271,7 @@ class DeReConstructField2ast:
|
|
|
544
271
|
self.ledger.addImportFrom_asStr(moduleWithLogicalPath, annotationType)
|
|
545
272
|
self.ledger.addImportFrom_asStr(moduleWithLogicalPath, 'dtype')
|
|
546
273
|
axesSubscript = Make.Subscript(Make.Name('tuple'), Make.Name('uint8'))
|
|
547
|
-
dtype_asnameName: ast.Name = self.astAnnotation
|
|
274
|
+
dtype_asnameName: ast.Name = cast(ast.Name, self.astAnnotation)
|
|
548
275
|
if dtype_asnameName.id == 'Array3D':
|
|
549
276
|
axesSubscript = Make.Subscript(Make.Name('tuple'), Make.Tuple([Make.Name('uint8'), Make.Name('uint8'), Make.Name('uint8')]))
|
|
550
277
|
ast_expr = Make.Subscript(Make.Name(annotationType), Make.Tuple([axesSubscript, Make.Subscript(Make.Name('dtype'), dtype_asnameName)]))
|
|
@@ -561,7 +288,7 @@ class DeReConstructField2ast:
|
|
|
561
288
|
elif isinstance(self.astAnnotation, ast.Subscript):
|
|
562
289
|
elementConstructor: ast_Identifier = self.metadata['elementConstructor']
|
|
563
290
|
self.ledger.addImportFrom_asStr(dataclassesDOTdataclassLogicalPathModule, elementConstructor)
|
|
564
|
-
takeTheTuple
|
|
291
|
+
takeTheTuple = deepcopy(self.astAnnotation.slice)
|
|
565
292
|
self.astAnnAssignConstructor = Make.AnnAssign(self.astName, self.astAnnotation, takeTheTuple)
|
|
566
293
|
self.Z0Z_hack = (self.astAnnAssignConstructor, elementConstructor)
|
|
567
294
|
if isinstance(self.astAnnotation, ast.Name):
|
|
@@ -12,10 +12,10 @@ from mapFolding.someAssemblyRequired import (
|
|
|
12
12
|
NodeTourist,
|
|
13
13
|
str_nameDOTname,
|
|
14
14
|
Then,
|
|
15
|
+
write_astModule,
|
|
15
16
|
)
|
|
16
17
|
from mapFolding.someAssemblyRequired.RecipeJob import RecipeJobTheorem2Numba
|
|
17
18
|
from mapFolding.someAssemblyRequired.toolboxNumba import parametersNumbaLight, SpicesJobNumba, decorateCallableWithNumba
|
|
18
|
-
from mapFolding.someAssemblyRequired.transformationTools import dictionaryEstimates, write_astModule, makeInitializedComputationState
|
|
19
19
|
from mapFolding.syntheticModules.initializeCount import initializeGroupsOfFolds
|
|
20
20
|
from mapFolding.dataBaskets import MapFoldingState
|
|
21
21
|
from pathlib import PurePosixPath
|
|
@@ -77,7 +77,7 @@ if __name__ == '__main__':
|
|
|
77
77
|
ast_argNumbaProgress = ast.arg(arg=spices.numbaProgressBarIdentifier, annotation=ast.Name(id=numba_progressPythonClass, ctx=ast.Load()))
|
|
78
78
|
ingredientsFunction.astFunctionDef.args.args.append(ast_argNumbaProgress)
|
|
79
79
|
|
|
80
|
-
findThis = IfThis.
|
|
80
|
+
findThis = IfThis.isAugAssignAndTargetIs(IfThis.isName_Identifier(job.shatteredDataclass.countingVariableName.id))
|
|
81
81
|
doThat = Then.replaceWith(Make.Expr(Make.Call(Make.Attribute(Make.Name(spices.numbaProgressBarIdentifier),'update'),[Make.Constant(1)])))
|
|
82
82
|
countWithProgressBar = NodeChanger(findThis, doThat)
|
|
83
83
|
countWithProgressBar.visit(ingredientsFunction.astFunctionDef)
|
|
@@ -131,19 +131,19 @@ def move_arg2FunctionDefDOTbodyAndAssignInitialValues(ingredientsFunction: Ingre
|
|
|
131
131
|
ImaAnnAssign, elementConstructor = job.shatteredDataclass.Z0Z_field2AnnAssign[ast_arg.arg]
|
|
132
132
|
match elementConstructor:
|
|
133
133
|
case 'scalar':
|
|
134
|
-
ImaAnnAssign.value.args[0].value = int(job.state.__dict__[ast_arg.arg])
|
|
134
|
+
cast(ast.Constant, cast(ast.Call, ImaAnnAssign.value).args[0]).value = int(job.state.__dict__[ast_arg.arg])
|
|
135
135
|
case 'array':
|
|
136
136
|
dataAsStrRLE: str = autoDecodingRLE(job.state.__dict__[ast_arg.arg], True)
|
|
137
137
|
dataAs_astExpr: ast.expr = cast(ast.Expr, ast.parse(dataAsStrRLE).body[0]).value
|
|
138
|
-
ImaAnnAssign.value.args = [dataAs_astExpr]
|
|
138
|
+
cast(ast.Call, ImaAnnAssign.value).args = [dataAs_astExpr]
|
|
139
139
|
case _:
|
|
140
140
|
list_exprDOTannotation: list[ast.expr] = []
|
|
141
141
|
list_exprDOTvalue: list[ast.expr] = []
|
|
142
142
|
for dimension in job.state.mapShape:
|
|
143
143
|
list_exprDOTannotation.append(Make.Name(elementConstructor))
|
|
144
144
|
list_exprDOTvalue.append(Make.Call(Make.Name(elementConstructor), [Make.Constant(dimension)]))
|
|
145
|
-
ImaAnnAssign.annotation.slice.elts = list_exprDOTannotation
|
|
146
|
-
ImaAnnAssign.value.elts = list_exprDOTvalue
|
|
145
|
+
cast(ast.Tuple, cast(ast.Subscript, cast(ast.AnnAssign, ImaAnnAssign).annotation).slice).elts = list_exprDOTannotation
|
|
146
|
+
cast(ast.Tuple, ImaAnnAssign.value).elts = list_exprDOTvalue
|
|
147
147
|
|
|
148
148
|
ingredientsFunction.astFunctionDef.body.insert(0, ImaAnnAssign)
|
|
149
149
|
|
|
@@ -32,10 +32,11 @@ from mapFolding.someAssemblyRequired import (
|
|
|
32
32
|
NodeTourist,
|
|
33
33
|
str_nameDOTname,
|
|
34
34
|
Then,
|
|
35
|
+
write_astModule,
|
|
35
36
|
)
|
|
36
37
|
from mapFolding.someAssemblyRequired.RecipeJob import RecipeJob
|
|
37
38
|
from mapFolding.someAssemblyRequired.toolboxNumba import parametersNumbaLight, SpicesJobNumba, decorateCallableWithNumba
|
|
38
|
-
from mapFolding.someAssemblyRequired.transformationTools import dictionaryEstimates,
|
|
39
|
+
from mapFolding.someAssemblyRequired.transformationTools import dictionaryEstimates, makeInitializedComputationState
|
|
39
40
|
from pathlib import PurePosixPath
|
|
40
41
|
from typing import cast, NamedTuple
|
|
41
42
|
from Z0Z_tools import autoDecodingRLE
|
|
@@ -95,7 +96,7 @@ if __name__ == '__main__':
|
|
|
95
96
|
ast_argNumbaProgress = ast.arg(arg=spices.numbaProgressBarIdentifier, annotation=ast.Name(id=numba_progressPythonClass, ctx=ast.Load()))
|
|
96
97
|
ingredientsFunction.astFunctionDef.args.args.append(ast_argNumbaProgress)
|
|
97
98
|
|
|
98
|
-
findThis = IfThis.
|
|
99
|
+
findThis = IfThis.isAugAssignAndTargetIs(IfThis.isName_Identifier(job.shatteredDataclass.countingVariableName.id))
|
|
99
100
|
doThat = Then.replaceWith(Make.Expr(Make.Call(Make.Attribute(Make.Name(spices.numbaProgressBarIdentifier),'update'),[Make.Constant(1)])))
|
|
100
101
|
countWithProgressBar = NodeChanger(findThis, doThat)
|
|
101
102
|
countWithProgressBar.visit(ingredientsFunction.astFunctionDef)
|
|
@@ -149,19 +150,19 @@ def move_arg2FunctionDefDOTbodyAndAssignInitialValues(ingredientsFunction: Ingre
|
|
|
149
150
|
ImaAnnAssign, elementConstructor = job.shatteredDataclass.Z0Z_field2AnnAssign[ast_arg.arg]
|
|
150
151
|
match elementConstructor:
|
|
151
152
|
case 'scalar':
|
|
152
|
-
ImaAnnAssign.value.args[0].value = int(job.state.__dict__[ast_arg.arg])
|
|
153
|
+
cast(ast.Constant, cast(ast.Call, ImaAnnAssign.value).args[0]).value = int(job.state.__dict__[ast_arg.arg])
|
|
153
154
|
case 'array':
|
|
154
155
|
dataAsStrRLE: str = autoDecodingRLE(job.state.__dict__[ast_arg.arg], True)
|
|
155
156
|
dataAs_astExpr: ast.expr = cast(ast.Expr, ast.parse(dataAsStrRLE).body[0]).value
|
|
156
|
-
ImaAnnAssign.value.args = [dataAs_astExpr]
|
|
157
|
+
cast(ast.Call, ImaAnnAssign.value).args = [dataAs_astExpr]
|
|
157
158
|
case _:
|
|
158
159
|
list_exprDOTannotation: list[ast.expr] = []
|
|
159
160
|
list_exprDOTvalue: list[ast.expr] = []
|
|
160
161
|
for dimension in job.state.mapShape:
|
|
161
162
|
list_exprDOTannotation.append(Make.Name(elementConstructor))
|
|
162
163
|
list_exprDOTvalue.append(Make.Call(Make.Name(elementConstructor), [Make.Constant(dimension)]))
|
|
163
|
-
ImaAnnAssign.annotation.slice.elts = list_exprDOTannotation
|
|
164
|
-
ImaAnnAssign.value.elts = list_exprDOTvalue
|
|
164
|
+
cast(ast.Tuple, cast(ast.Subscript, cast(ast.AnnAssign, ImaAnnAssign).annotation).slice).elts = list_exprDOTannotation
|
|
165
|
+
cast(ast.Tuple, ImaAnnAssign.value).elts = list_exprDOTvalue
|
|
165
166
|
|
|
166
167
|
ingredientsFunction.astFunctionDef.body.insert(0, ImaAnnAssign)
|
|
167
168
|
|
|
@@ -17,8 +17,8 @@ performance improvements while preserving code semantics and correctness.
|
|
|
17
17
|
|
|
18
18
|
from collections.abc import Callable, Sequence
|
|
19
19
|
from mapFolding import NotRequired, TypedDict
|
|
20
|
-
from mapFolding.someAssemblyRequired import ast_Identifier, IngredientsFunction, Make, RecipeSynthesizeFlow, str_nameDOTname
|
|
21
|
-
from mapFolding.someAssemblyRequired.transformationTools import makeNewFlow
|
|
20
|
+
from mapFolding.someAssemblyRequired import ast_Identifier, IngredientsFunction, Make, RecipeSynthesizeFlow, str_nameDOTname, write_astModule
|
|
21
|
+
from mapFolding.someAssemblyRequired.transformationTools import makeNewFlow
|
|
22
22
|
from numba.core.compiler import CompilerBase as numbaCompilerBase
|
|
23
23
|
from typing import Any, cast, Final
|
|
24
24
|
import ast
|