mapFolding 0.2.2__py3-none-any.whl → 0.2.4__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 +1 -2
- mapFolding/babbage.py +10 -5
- mapFolding/beDRY.py +26 -15
- mapFolding/benchmarks/benchmarking.py +3 -2
- mapFolding/countInitialize.py +44 -0
- mapFolding/countParallel.py +49 -0
- mapFolding/countSequential.py +43 -0
- mapFolding/importSelector.py +12 -0
- mapFolding/inlineAfunction.py +124 -0
- mapFolding/lovelace.py +206 -138
- mapFolding/reference/flattened.py +376 -0
- mapFolding/startHere.py +7 -16
- mapFolding/theSSOT.py +14 -6
- {mapFolding-0.2.2.dist-info → mapFolding-0.2.4.dist-info}/METADATA +6 -4
- mapFolding-0.2.4.dist-info/RECORD +35 -0
- tests/__init__.py +1 -1
- tests/conftest.py +31 -6
- tests/pythons_idiotic_namespace.py +1 -0
- tests/test_other.py +193 -30
- tests/test_temporary.py +25 -0
- mapFolding/benchmarks/test_benchmarks.py +0 -74
- mapFolding-0.2.2.dist-info/RECORD +0 -28
- {mapFolding-0.2.2.dist-info → mapFolding-0.2.4.dist-info}/WHEEL +0 -0
- {mapFolding-0.2.2.dist-info → mapFolding-0.2.4.dist-info}/entry_points.txt +0 -0
- {mapFolding-0.2.2.dist-info → mapFolding-0.2.4.dist-info}/top_level.txt +0 -0
mapFolding/__init__.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from .theSSOT import *
|
|
2
2
|
from Z0Z_tools import defineConcurrencyLimit, intInnit, oopsieKwargsie
|
|
3
|
-
from .beDRY import
|
|
4
|
-
from .beDRY import getLeavesTotal, parseDimensions, validateListDimensions
|
|
3
|
+
from .beDRY import getFilenameFoldsTotal, outfitCountFolds
|
|
5
4
|
from .startHere import countFolds
|
|
6
5
|
from .oeis import oeisIDfor_n, getOEISids, clearOEIScache
|
|
7
6
|
|
mapFolding/babbage.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
from mapFolding.
|
|
1
|
+
from mapFolding.importSelector import countSequential, countParallel, countInitialize
|
|
2
|
+
from mapFolding import indexThe
|
|
2
3
|
from numpy import integer
|
|
3
4
|
from numpy.typing import NDArray
|
|
4
5
|
from typing import Any, Tuple
|
|
@@ -6,7 +7,7 @@ import numba
|
|
|
6
7
|
import numpy
|
|
7
8
|
|
|
8
9
|
@numba.jit(cache=True)
|
|
9
|
-
def _countFolds(connectionGraph: NDArray[integer[Any]],
|
|
10
|
+
def _countFolds(connectionGraph: NDArray[integer[Any]], foldsSubTotals: NDArray[integer[Any]], gapsWhere: NDArray[integer[Any]], mapShape: Tuple[int, ...], my: NDArray[integer[Any]], the: NDArray[integer[Any]], track: NDArray[integer[Any]]):
|
|
10
11
|
"""
|
|
11
12
|
What in tarnation is this stupid module and function?
|
|
12
13
|
|
|
@@ -25,6 +26,10 @@ def _countFolds(connectionGraph: NDArray[integer[Any]], foldsTotal: NDArray[inte
|
|
|
25
26
|
- and just a few dozen-jillion other things.
|
|
26
27
|
|
|
27
28
|
"""
|
|
28
|
-
#
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
# print("babbage")
|
|
30
|
+
countInitialize(connectionGraph=connectionGraph, gapsWhere=gapsWhere, my=my, the=the, track=track)
|
|
31
|
+
|
|
32
|
+
if the[indexThe.taskDivisions.value] > 0:
|
|
33
|
+
countParallel(connectionGraph=connectionGraph, foldsSubTotals=foldsSubTotals, gapsWherePARALLEL=gapsWhere, myPARALLEL=my, the=the, trackPARALLEL=track)
|
|
34
|
+
else:
|
|
35
|
+
countSequential(connectionGraph=connectionGraph, foldsSubTotals=foldsSubTotals, gapsWhere=gapsWhere, my=my, the=the, track=track)
|
mapFolding/beDRY.py
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
"""A relatively stable API for oft-needed functionality."""
|
|
2
|
-
from mapFolding import
|
|
2
|
+
from mapFolding import dtypeDefault, dtypeLarge
|
|
3
3
|
from mapFolding import indexMy, indexThe, indexTrack, computationState
|
|
4
|
-
from mapFolding import
|
|
4
|
+
from mapFolding import intInnit, defineConcurrencyLimit, oopsieKwargsie
|
|
5
|
+
from numpy import integer
|
|
6
|
+
from numpy.typing import NDArray
|
|
5
7
|
from typing import Any, List, Optional, Sequence, Type, Union
|
|
6
|
-
import numpy
|
|
7
8
|
import numba
|
|
8
|
-
|
|
9
|
-
from numpy import integer
|
|
9
|
+
import numpy
|
|
10
10
|
import sys
|
|
11
|
-
|
|
11
|
+
|
|
12
|
+
def getFilenameFoldsTotal(listDimensions: Sequence[int]) -> str:
|
|
13
|
+
return str(sorted(listDimensions)).replace(' ', '') + '.foldsTotal'
|
|
12
14
|
|
|
13
15
|
def getLeavesTotal(listDimensions: Sequence[int]) -> int:
|
|
14
16
|
"""
|
|
@@ -146,7 +148,7 @@ def makeDataContainer(shape, datatype: Optional[Type] = None):
|
|
|
146
148
|
datatype = dtypeDefault
|
|
147
149
|
return numpy.zeros(shape, dtype=datatype)
|
|
148
150
|
|
|
149
|
-
def
|
|
151
|
+
def outfitCountFolds(listDimensions: Sequence[int], computationDivisions: Optional[Union[int, str]] = None, CPUlimit: Optional[Union[bool, float, int]] = None, **keywordArguments: Optional[Type]) -> computationState:
|
|
150
152
|
"""
|
|
151
153
|
Initializes and configures the computation state for map folding computations.
|
|
152
154
|
|
|
@@ -155,21 +157,33 @@ def outfitFoldings(listDimensions: Sequence[int], computationDivisions: Optional
|
|
|
155
157
|
listDimensions:
|
|
156
158
|
The dimensions of the map to be folded
|
|
157
159
|
computationDivisions (None):
|
|
158
|
-
Specifies how to divide
|
|
160
|
+
Specifies how to divide computations:
|
|
161
|
+
- None: no division of the computation into tasks; sets task divisions to 0
|
|
162
|
+
- int: direct set the number of task divisions; cannot exceed the map's total leaves
|
|
163
|
+
- "maximum": divides into `leavesTotal`-many `taskDivisions`
|
|
164
|
+
- "cpu": limits the divisions to the number of available CPUs, i.e. `concurrencyLimit`
|
|
159
165
|
CPUlimit (None):
|
|
160
|
-
|
|
166
|
+
Whether and how to limit the CPU usage. See notes for details.
|
|
161
167
|
|
|
162
168
|
Returns
|
|
163
169
|
-------
|
|
164
170
|
computationState
|
|
165
171
|
An initialized computation state containing:
|
|
166
172
|
- connectionGraph: Graph representing connections in the map
|
|
167
|
-
-
|
|
173
|
+
- foldsSubTotals: Array tracking total folds
|
|
168
174
|
- mapShape: Validated and sorted dimensions of the map
|
|
169
175
|
- my: Array for internal state tracking
|
|
170
176
|
- gapsWhere: Array tracking gap positions
|
|
171
177
|
- the: Static settings and metadata
|
|
172
178
|
- track: Array for tracking computation progress
|
|
179
|
+
|
|
180
|
+
Limits on CPU usage `CPUlimit`:
|
|
181
|
+
- `False`, `None`, or `0`: No limits on CPU usage; uses all available CPUs. All other values will potentially limit CPU usage.
|
|
182
|
+
- `True`: Yes, limit the CPU usage; limits to 1 CPU.
|
|
183
|
+
- Integer `>= 1`: Limits usage to the specified number of CPUs.
|
|
184
|
+
- Decimal value (`float`) between 0 and 1: Fraction of total CPUs to use.
|
|
185
|
+
- Decimal value (`float`) between -1 and 0: Fraction of CPUs to *not* use.
|
|
186
|
+
- Integer `<= -1`: Subtract the absolute value from total CPUs.
|
|
173
187
|
"""
|
|
174
188
|
datatypeDefault = keywordArguments.get('datatypeDefault', dtypeDefault)
|
|
175
189
|
datatypeLarge = keywordArguments.get('datatypeLarge', dtypeLarge)
|
|
@@ -181,10 +195,10 @@ def outfitFoldings(listDimensions: Sequence[int], computationDivisions: Optional
|
|
|
181
195
|
the[indexThe.dimensionsTotal] = len(mapShape)
|
|
182
196
|
concurrencyLimit = setCPUlimit(CPUlimit)
|
|
183
197
|
the[indexThe.taskDivisions] = getTaskDivisions(computationDivisions, concurrencyLimit, CPUlimit, listDimensions)
|
|
184
|
-
|
|
198
|
+
|
|
185
199
|
stateInitialized = computationState(
|
|
186
200
|
connectionGraph = makeConnectionGraph(mapShape, datatype=datatypeDefault),
|
|
187
|
-
|
|
201
|
+
foldsSubTotals = makeDataContainer(the[indexThe.leavesTotal], datatypeLarge),
|
|
188
202
|
mapShape = mapShape,
|
|
189
203
|
my = makeDataContainer(len(indexMy), datatypeLarge),
|
|
190
204
|
gapsWhere = makeDataContainer(int(the[indexThe.leavesTotal]) * int(the[indexThe.leavesTotal]) + 1, datatypeDefault),
|
|
@@ -216,9 +230,6 @@ def parseDimensions(dimensions: Sequence[int], parameterName: str = 'unnamed par
|
|
|
216
230
|
raise ValueError(f"Dimension {dimension} must be non-negative")
|
|
217
231
|
listNonNegative.append(dimension)
|
|
218
232
|
|
|
219
|
-
if not listNonNegative:
|
|
220
|
-
raise ValueError("At least one dimension must be non-negative")
|
|
221
|
-
|
|
222
233
|
return listNonNegative
|
|
223
234
|
|
|
224
235
|
def setCPUlimit(CPUlimit: Union[bool, float, int, None]) -> int:
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
"""An incompetent benchmarking module for mapFolding."""
|
|
2
2
|
from typing import Callable
|
|
3
|
+
import multiprocessing
|
|
3
4
|
import numpy
|
|
4
5
|
import pathlib
|
|
5
6
|
import time
|
|
@@ -57,7 +58,7 @@ def runBenchmarks(benchmarkIterations: int = 30) -> None:
|
|
|
57
58
|
listCartesianProduct = list(itertools.product(listParametersOEIS, range(benchmarkIterations)))
|
|
58
59
|
with ProcessPoolExecutor(max_workers) as concurrencyManager:
|
|
59
60
|
listConcurrency = [concurrencyManager.submit(oeisIDfor_n, *parameters[0]) for parameters in listCartesianProduct]
|
|
60
|
-
for
|
|
61
|
+
for _complete in tqdm(as_completed(listConcurrency), total=len(listCartesianProduct)):
|
|
61
62
|
pass
|
|
62
63
|
|
|
63
64
|
if __name__ == '__main__':
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import numba
|
|
2
|
+
|
|
3
|
+
@numba.jit((numba.int64[:, :, ::1], numba.int64[::1], numba.int64[::1], numba.int64[::1], numba.int64[:, ::1]), parallel=False, boundscheck=False, error_model='numpy', fastmath=True, looplift=False, nogil=True, nopython=True)
|
|
4
|
+
def countInitialize(connectionGraph, gapsWhere, my, the, track):
|
|
5
|
+
while my[6] > 0:
|
|
6
|
+
if my[6] <= 1 or track[1, 0] == 1:
|
|
7
|
+
my[1] = 0
|
|
8
|
+
my[3] = track[3, my[6] - 1]
|
|
9
|
+
my[0] = 1
|
|
10
|
+
while my[0] <= the[0]:
|
|
11
|
+
if connectionGraph[my[0], my[6], my[6]] == my[6]:
|
|
12
|
+
my[1] += 1
|
|
13
|
+
else:
|
|
14
|
+
my[7] = connectionGraph[my[0], my[6], my[6]]
|
|
15
|
+
while my[7] != my[6]:
|
|
16
|
+
gapsWhere[my[3]] = my[7]
|
|
17
|
+
if track[2, my[7]] == 0:
|
|
18
|
+
my[3] += 1
|
|
19
|
+
track[2, my[7]] += 1
|
|
20
|
+
my[7] = connectionGraph[my[0], my[6], track[1, my[7]]]
|
|
21
|
+
my[0] += 1
|
|
22
|
+
if my[1] == the[0]:
|
|
23
|
+
my[4] = 0
|
|
24
|
+
while my[4] < my[6]:
|
|
25
|
+
gapsWhere[my[3]] = my[4]
|
|
26
|
+
my[3] += 1
|
|
27
|
+
my[4] += 1
|
|
28
|
+
my[5] = my[2]
|
|
29
|
+
while my[5] < my[3]:
|
|
30
|
+
gapsWhere[my[2]] = gapsWhere[my[5]]
|
|
31
|
+
if track[2, gapsWhere[my[5]]] == the[0] - my[1]:
|
|
32
|
+
my[2] += 1
|
|
33
|
+
track[2, gapsWhere[my[5]]] = 0
|
|
34
|
+
my[5] += 1
|
|
35
|
+
if my[6] > 0:
|
|
36
|
+
my[2] -= 1
|
|
37
|
+
track[0, my[6]] = gapsWhere[my[2]]
|
|
38
|
+
track[1, my[6]] = track[1, track[0, my[6]]]
|
|
39
|
+
track[1, track[0, my[6]]] = my[6]
|
|
40
|
+
track[0, track[1, my[6]]] = my[6]
|
|
41
|
+
track[3, my[6]] = my[2]
|
|
42
|
+
my[6] += 1
|
|
43
|
+
if my[2] > 0:
|
|
44
|
+
return
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import numba
|
|
2
|
+
|
|
3
|
+
@numba.jit((numba.int64[:, :, ::1], numba.int64[::1], numba.int64[::1], numba.int64[::1], numba.int64[::1], numba.int64[:, ::1]), parallel=True, boundscheck=False, error_model='numpy', fastmath=True, looplift=False, nogil=True, nopython=True)
|
|
4
|
+
def countParallel(connectionGraph, foldsSubTotals, gapsWherePARALLEL, myPARALLEL, the, trackPARALLEL):
|
|
5
|
+
for indexSherpa in numba.prange(the[2]):
|
|
6
|
+
gapsWhere = gapsWherePARALLEL.copy()
|
|
7
|
+
my = myPARALLEL.copy()
|
|
8
|
+
my[8] = indexSherpa
|
|
9
|
+
track = trackPARALLEL.copy()
|
|
10
|
+
while my[6] > 0:
|
|
11
|
+
if my[6] <= 1 or track[1, 0] == 1:
|
|
12
|
+
if my[6] > the[1]:
|
|
13
|
+
foldsSubTotals[my[8]] += the[1]
|
|
14
|
+
else:
|
|
15
|
+
my[1] = 0
|
|
16
|
+
my[3] = track[3, my[6] - 1]
|
|
17
|
+
my[0] = 1
|
|
18
|
+
while my[0] <= the[0]:
|
|
19
|
+
if connectionGraph[my[0], my[6], my[6]] == my[6]:
|
|
20
|
+
my[1] += 1
|
|
21
|
+
else:
|
|
22
|
+
my[7] = connectionGraph[my[0], my[6], my[6]]
|
|
23
|
+
while my[7] != my[6]:
|
|
24
|
+
if my[6] != the[2] or my[7] % the[2] == my[8]:
|
|
25
|
+
gapsWhere[my[3]] = my[7]
|
|
26
|
+
if track[2, my[7]] == 0:
|
|
27
|
+
my[3] += 1
|
|
28
|
+
track[2, my[7]] += 1
|
|
29
|
+
my[7] = connectionGraph[my[0], my[6], track[1, my[7]]]
|
|
30
|
+
my[0] += 1
|
|
31
|
+
my[5] = my[2]
|
|
32
|
+
while my[5] < my[3]:
|
|
33
|
+
gapsWhere[my[2]] = gapsWhere[my[5]]
|
|
34
|
+
if track[2, gapsWhere[my[5]]] == the[0] - my[1]:
|
|
35
|
+
my[2] += 1
|
|
36
|
+
track[2, gapsWhere[my[5]]] = 0
|
|
37
|
+
my[5] += 1
|
|
38
|
+
while my[6] > 0 and my[2] == track[3, my[6] - 1]:
|
|
39
|
+
my[6] -= 1
|
|
40
|
+
track[1, track[0, my[6]]] = track[1, my[6]]
|
|
41
|
+
track[0, track[1, my[6]]] = track[0, my[6]]
|
|
42
|
+
if my[6] > 0:
|
|
43
|
+
my[2] -= 1
|
|
44
|
+
track[0, my[6]] = gapsWhere[my[2]]
|
|
45
|
+
track[1, my[6]] = track[1, track[0, my[6]]]
|
|
46
|
+
track[1, track[0, my[6]]] = my[6]
|
|
47
|
+
track[0, track[1, my[6]]] = my[6]
|
|
48
|
+
track[3, my[6]] = my[2]
|
|
49
|
+
my[6] += 1
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import numba
|
|
2
|
+
|
|
3
|
+
@numba.jit((numba.int64[:, :, ::1], numba.int64[::1], numba.int64[::1], numba.int64[::1], numba.int64[::1], numba.int64[:, ::1]), parallel=False, boundscheck=False, error_model='numpy', fastmath=True, looplift=False, nogil=True, nopython=True)
|
|
4
|
+
def countSequential(connectionGraph, foldsSubTotals, gapsWhere, my, the, track):
|
|
5
|
+
while my[6] > 0:
|
|
6
|
+
if my[6] <= 1 or track[1, 0] == 1:
|
|
7
|
+
if my[6] > the[1]:
|
|
8
|
+
foldsSubTotals[my[8]] += the[1]
|
|
9
|
+
else:
|
|
10
|
+
my[1] = 0
|
|
11
|
+
my[3] = track[3, my[6] - 1]
|
|
12
|
+
my[0] = 1
|
|
13
|
+
while my[0] <= the[0]:
|
|
14
|
+
if connectionGraph[my[0], my[6], my[6]] == my[6]:
|
|
15
|
+
my[1] += 1
|
|
16
|
+
else:
|
|
17
|
+
my[7] = connectionGraph[my[0], my[6], my[6]]
|
|
18
|
+
while my[7] != my[6]:
|
|
19
|
+
gapsWhere[my[3]] = my[7]
|
|
20
|
+
if track[2, my[7]] == 0:
|
|
21
|
+
my[3] += 1
|
|
22
|
+
track[2, my[7]] += 1
|
|
23
|
+
my[7] = connectionGraph[my[0], my[6], track[1, my[7]]]
|
|
24
|
+
my[0] += 1
|
|
25
|
+
my[5] = my[2]
|
|
26
|
+
while my[5] < my[3]:
|
|
27
|
+
gapsWhere[my[2]] = gapsWhere[my[5]]
|
|
28
|
+
if track[2, gapsWhere[my[5]]] == the[0] - my[1]:
|
|
29
|
+
my[2] += 1
|
|
30
|
+
track[2, gapsWhere[my[5]]] = 0
|
|
31
|
+
my[5] += 1
|
|
32
|
+
while my[6] > 0 and my[2] == track[3, my[6] - 1]:
|
|
33
|
+
my[6] -= 1
|
|
34
|
+
track[1, track[0, my[6]]] = track[1, my[6]]
|
|
35
|
+
track[0, track[1, my[6]]] = track[0, my[6]]
|
|
36
|
+
if my[6] > 0:
|
|
37
|
+
my[2] -= 1
|
|
38
|
+
track[0, my[6]] = gapsWhere[my[2]]
|
|
39
|
+
track[1, my[6]] = track[1, track[0, my[6]]]
|
|
40
|
+
track[1, track[0, my[6]]] = my[6]
|
|
41
|
+
track[0, track[1, my[6]]] = my[6]
|
|
42
|
+
track[3, my[6]] = my[2]
|
|
43
|
+
my[6] += 1
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# useLovelace = True
|
|
2
|
+
useLovelace = False
|
|
3
|
+
|
|
4
|
+
if useLovelace:
|
|
5
|
+
from mapFolding.lovelace import countSequential
|
|
6
|
+
from mapFolding.lovelace import countParallel
|
|
7
|
+
from mapFolding.lovelace import countInitialize
|
|
8
|
+
|
|
9
|
+
else:
|
|
10
|
+
from mapFolding.countSequential import countSequential
|
|
11
|
+
from mapFolding.countParallel import countParallel
|
|
12
|
+
from mapFolding.countInitialize import countInitialize
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
from mapFolding import indexMy, indexThe, indexTrack
|
|
2
|
+
import ast
|
|
3
|
+
import pathlib
|
|
4
|
+
|
|
5
|
+
dictionaryEnumValues = {}
|
|
6
|
+
for enumIndex in [indexMy, indexThe, indexTrack]:
|
|
7
|
+
for memberName, memberValue in enumIndex._member_map_.items():
|
|
8
|
+
dictionaryEnumValues[f"{enumIndex.__name__}.{memberName}.value"] = memberValue.value
|
|
9
|
+
|
|
10
|
+
class RecursiveInliner(ast.NodeTransformer):
|
|
11
|
+
def __init__(self, dictionaryFunctions, dictionaryEnumValues):
|
|
12
|
+
self.dictionaryFunctions = dictionaryFunctions
|
|
13
|
+
self.dictionaryEnumValues = dictionaryEnumValues
|
|
14
|
+
self.processed = set() # Track processed functions to avoid infinite recursion
|
|
15
|
+
|
|
16
|
+
def inline_function_body(self, functionName):
|
|
17
|
+
if functionName in self.processed:
|
|
18
|
+
return None
|
|
19
|
+
|
|
20
|
+
self.processed.add(functionName)
|
|
21
|
+
inlineDefinition = self.dictionaryFunctions[functionName]
|
|
22
|
+
# Recursively process the function body
|
|
23
|
+
for node in ast.walk(inlineDefinition):
|
|
24
|
+
self.visit(node)
|
|
25
|
+
return inlineDefinition
|
|
26
|
+
|
|
27
|
+
def visit_Attribute(self, node):
|
|
28
|
+
# Substitute enum identifiers (e.g., indexMy.leaf1ndex.value)
|
|
29
|
+
if isinstance(node.value, ast.Attribute) and isinstance(node.value.value, ast.Name):
|
|
30
|
+
enumPath = f"{node.value.value.id}.{node.value.attr}.{node.attr}"
|
|
31
|
+
if enumPath in self.dictionaryEnumValues:
|
|
32
|
+
return ast.Constant(value=self.dictionaryEnumValues[enumPath])
|
|
33
|
+
return self.generic_visit(node)
|
|
34
|
+
|
|
35
|
+
def visit_Call(self, node):
|
|
36
|
+
callNode = self.generic_visit(node)
|
|
37
|
+
if isinstance(callNode, ast.Call) and isinstance(callNode.func, ast.Name) and callNode.func.id in self.dictionaryFunctions:
|
|
38
|
+
inlineDefinition = self.inline_function_body(callNode.func.id)
|
|
39
|
+
if inlineDefinition and inlineDefinition.body:
|
|
40
|
+
lastStmt = inlineDefinition.body[-1]
|
|
41
|
+
if isinstance(lastStmt, ast.Return) and lastStmt.value is not None:
|
|
42
|
+
return self.visit(lastStmt.value)
|
|
43
|
+
elif isinstance(lastStmt, ast.Expr) and lastStmt.value is not None:
|
|
44
|
+
return self.visit(lastStmt.value)
|
|
45
|
+
return None
|
|
46
|
+
return callNode
|
|
47
|
+
|
|
48
|
+
def visit_Expr(self, node):
|
|
49
|
+
if isinstance(node.value, ast.Call):
|
|
50
|
+
if isinstance(node.value.func, ast.Name) and node.value.func.id in self.dictionaryFunctions:
|
|
51
|
+
inlineDefinition = self.inline_function_body(node.value.func.id)
|
|
52
|
+
if inlineDefinition:
|
|
53
|
+
return [self.visit(stmt) for stmt in inlineDefinition.body]
|
|
54
|
+
return self.generic_visit(node)
|
|
55
|
+
|
|
56
|
+
def find_required_imports(node):
|
|
57
|
+
"""Find all modules that need to be imported based on AST analysis."""
|
|
58
|
+
requiredImports = set()
|
|
59
|
+
|
|
60
|
+
class ImportFinder(ast.NodeVisitor):
|
|
61
|
+
def visit_Name(self, node):
|
|
62
|
+
# Common modules we might need
|
|
63
|
+
if node.id in {'numba'}:
|
|
64
|
+
requiredImports.add(node.id)
|
|
65
|
+
self.generic_visit(node)
|
|
66
|
+
|
|
67
|
+
def visit_Decorator(self, node):
|
|
68
|
+
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
|
|
69
|
+
if node.func.id == 'jit':
|
|
70
|
+
requiredImports.add('numba')
|
|
71
|
+
self.generic_visit(node)
|
|
72
|
+
|
|
73
|
+
ImportFinder().visit(node)
|
|
74
|
+
return requiredImports
|
|
75
|
+
|
|
76
|
+
def generate_imports(requiredImports):
|
|
77
|
+
"""Generate import statements based on required modules."""
|
|
78
|
+
importStatements = []
|
|
79
|
+
|
|
80
|
+
# Map of module names to their import statements
|
|
81
|
+
importMapping = {
|
|
82
|
+
'numba': 'import numba',
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for moduleName in sorted(requiredImports):
|
|
86
|
+
if moduleName in importMapping:
|
|
87
|
+
importStatements.append(importMapping[moduleName])
|
|
88
|
+
|
|
89
|
+
return '\n'.join(importStatements)
|
|
90
|
+
|
|
91
|
+
def inline_functions(sourceCode, targetFunctionName, dictionaryEnumValues):
|
|
92
|
+
dictionaryParsed = ast.parse(sourceCode)
|
|
93
|
+
dictionaryFunctions = {
|
|
94
|
+
element.name: element
|
|
95
|
+
for element in dictionaryParsed.body
|
|
96
|
+
if isinstance(element, ast.FunctionDef)
|
|
97
|
+
}
|
|
98
|
+
nodeTarget = dictionaryFunctions[targetFunctionName]
|
|
99
|
+
nodeInliner = RecursiveInliner(dictionaryFunctions, dictionaryEnumValues)
|
|
100
|
+
nodeInlined = nodeInliner.visit(nodeTarget)
|
|
101
|
+
ast.fix_missing_locations(nodeInlined)
|
|
102
|
+
|
|
103
|
+
# Generate imports
|
|
104
|
+
requiredImports = find_required_imports(nodeInlined)
|
|
105
|
+
importStatements = generate_imports(requiredImports)
|
|
106
|
+
|
|
107
|
+
# Combine imports with inlined code
|
|
108
|
+
inlinedCode = importStatements + '\n\n' + ast.unparse(ast.Module(body=[nodeInlined], type_ignores=[]))
|
|
109
|
+
return inlinedCode
|
|
110
|
+
|
|
111
|
+
pathFilenameSource = pathlib.Path("/apps/mapFolding/mapFolding/lovelace.py")
|
|
112
|
+
codeSource = pathFilenameSource.read_text()
|
|
113
|
+
|
|
114
|
+
listCallables = [
|
|
115
|
+
'countSequential',
|
|
116
|
+
'countParallel',
|
|
117
|
+
'countInitialize',
|
|
118
|
+
]
|
|
119
|
+
listPathFilenamesDestination = []
|
|
120
|
+
for callableTarget in listCallables:
|
|
121
|
+
pathFilenameDestination = pathFilenameSource.with_stem(callableTarget)
|
|
122
|
+
codeInlined = inline_functions(codeSource, callableTarget, dictionaryEnumValues)
|
|
123
|
+
pathFilenameDestination.write_text(codeInlined)
|
|
124
|
+
listPathFilenamesDestination.append(pathFilenameDestination)
|