mapFolding 0.12.2__py3-none-any.whl → 0.12.3__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 +4 -2
- mapFolding/_theSSOT.py +32 -88
- mapFolding/{datatypes.py → _theTypes.py} +25 -3
- mapFolding/basecamp.py +39 -34
- mapFolding/beDRY.py +79 -54
- mapFolding/dataBaskets.py +117 -74
- mapFolding/filesystemToolkit.py +140 -91
- mapFolding/oeis.py +242 -144
- mapFolding/reference/flattened.py +1 -1
- mapFolding/someAssemblyRequired/RecipeJob.py +68 -53
- mapFolding/someAssemblyRequired/__init__.py +40 -15
- mapFolding/someAssemblyRequired/_toolIfThis.py +82 -54
- mapFolding/someAssemblyRequired/_toolkitContainers.py +19 -16
- mapFolding/someAssemblyRequired/getLLVMforNoReason.py +35 -26
- mapFolding/someAssemblyRequired/makeAllModules.py +348 -275
- mapFolding/someAssemblyRequired/makeJobTheorem2Numba.py +81 -57
- mapFolding/someAssemblyRequired/toolkitNumba.py +80 -50
- mapFolding/someAssemblyRequired/transformationTools.py +63 -40
- {mapfolding-0.12.2.dist-info → mapfolding-0.12.3.dist-info}/METADATA +7 -11
- {mapfolding-0.12.2.dist-info → mapfolding-0.12.3.dist-info}/RECORD +28 -28
- tests/test_computations.py +26 -8
- tests/test_oeis.py +8 -7
- tests/test_other.py +3 -3
- tests/test_tasks.py +2 -4
- {mapfolding-0.12.2.dist-info → mapfolding-0.12.3.dist-info}/WHEEL +0 -0
- {mapfolding-0.12.2.dist-info → mapfolding-0.12.3.dist-info}/entry_points.txt +0 -0
- {mapfolding-0.12.2.dist-info → mapfolding-0.12.3.dist-info}/licenses/LICENSE +0 -0
- {mapfolding-0.12.2.dist-info → mapfolding-0.12.3.dist-info}/top_level.txt +0 -0
mapFolding/oeis.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Mathematical validation and discovery through OEIS integration.
|
|
3
3
|
|
|
4
|
+
(AI generated docstring)
|
|
5
|
+
|
|
4
6
|
Complementing the unified computational interface, this module extends the map
|
|
5
7
|
folding ecosystem into the broader mathematical community through comprehensive
|
|
6
8
|
integration with the Online Encyclopedia of Integer Sequences (OEIS). This bridge
|
|
@@ -23,18 +25,17 @@ completes the journey from configuration foundation to mathematical discovery.
|
|
|
23
25
|
"""
|
|
24
26
|
|
|
25
27
|
from collections.abc import Callable
|
|
26
|
-
from datetime import datetime, timedelta
|
|
28
|
+
from datetime import datetime, timedelta, UTC
|
|
27
29
|
from functools import cache
|
|
30
|
+
from hunterMakesPy import writeStringToHere
|
|
28
31
|
from mapFolding import countFolds, packageSettings
|
|
29
32
|
from pathlib import Path
|
|
30
33
|
from typing import Any, Final, TypedDict
|
|
31
|
-
from urllib.
|
|
32
|
-
from Z0Z_tools import writeStringToHere
|
|
34
|
+
from urllib.request import urlopen
|
|
33
35
|
import argparse
|
|
34
36
|
import random
|
|
35
37
|
import sys
|
|
36
38
|
import time
|
|
37
|
-
import urllib.request
|
|
38
39
|
import warnings
|
|
39
40
|
|
|
40
41
|
cacheDays = 30
|
|
@@ -44,12 +45,15 @@ pathCache: Path = packageSettings.pathPackage / ".cache"
|
|
|
44
45
|
"""Local directory path for storing cached OEIS sequence data and metadata."""
|
|
45
46
|
|
|
46
47
|
class SettingsOEIS(TypedDict):
|
|
47
|
-
"""
|
|
48
|
-
Complete configuration settings for a single OEIS sequence implementation.
|
|
48
|
+
"""Complete configuration settings for a single OEIS sequence implementation.
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
(AI generated docstring)
|
|
51
|
+
|
|
52
|
+
This `TypedDict` defines the structure for storing all metadata, known values, and operational parameters
|
|
51
53
|
needed to work with an OEIS sequence within the map folding context.
|
|
54
|
+
|
|
52
55
|
"""
|
|
56
|
+
|
|
53
57
|
description: str
|
|
54
58
|
getMapShape: Callable[[int], tuple[int, ...]]
|
|
55
59
|
offset: int
|
|
@@ -60,12 +64,15 @@ class SettingsOEIS(TypedDict):
|
|
|
60
64
|
valueUnknown: int
|
|
61
65
|
|
|
62
66
|
class SettingsOEIShardcodedValues(TypedDict):
|
|
63
|
-
"""
|
|
64
|
-
Hardcoded configuration values for OEIS sequences defined within the module.
|
|
67
|
+
"""Hardcoded configuration values for OEIS sequences defined within the module.
|
|
65
68
|
|
|
66
|
-
|
|
69
|
+
(AI generated docstring)
|
|
70
|
+
|
|
71
|
+
This `TypedDict` contains the static configuration data that is embedded in the source code
|
|
67
72
|
rather than retrieved from external sources.
|
|
73
|
+
|
|
68
74
|
"""
|
|
75
|
+
|
|
69
76
|
getMapShape: Callable[[int], tuple[int, ...]]
|
|
70
77
|
valuesBenchmark: list[int]
|
|
71
78
|
valuesTestParallelization: list[int]
|
|
@@ -76,25 +83,25 @@ settingsOEIShardcodedValues: dict[str, SettingsOEIShardcodedValues] = {
|
|
|
76
83
|
'getMapShape': lambda n: tuple(sorted([1, n])),
|
|
77
84
|
'valuesBenchmark': [14],
|
|
78
85
|
'valuesTestParallelization': [*range(3, 7)],
|
|
79
|
-
'valuesTestValidation': [random.randint(2, 9)],
|
|
86
|
+
'valuesTestValidation': [random.randint(2, 9)], # noqa: S311
|
|
80
87
|
},
|
|
81
88
|
'A001415': {
|
|
82
89
|
'getMapShape': lambda n: tuple(sorted([2, n])),
|
|
83
90
|
'valuesBenchmark': [14],
|
|
84
91
|
'valuesTestParallelization': [*range(3, 7)],
|
|
85
|
-
'valuesTestValidation': [random.randint(2, 9)],
|
|
92
|
+
'valuesTestValidation': [random.randint(2, 9)], # noqa: S311
|
|
86
93
|
},
|
|
87
94
|
'A001416': {
|
|
88
95
|
'getMapShape': lambda n: tuple(sorted([3, n])),
|
|
89
96
|
'valuesBenchmark': [9],
|
|
90
97
|
'valuesTestParallelization': [*range(3, 5)],
|
|
91
|
-
'valuesTestValidation': [random.randint(2, 6)],
|
|
98
|
+
'valuesTestValidation': [random.randint(2, 6)], # noqa: S311
|
|
92
99
|
},
|
|
93
100
|
'A001417': {
|
|
94
101
|
'getMapShape': lambda n: tuple(2 for _dimension in range(n)),
|
|
95
102
|
'valuesBenchmark': [6],
|
|
96
103
|
'valuesTestParallelization': [*range(2, 4)],
|
|
97
|
-
'valuesTestValidation': [random.randint(2, 4)],
|
|
104
|
+
'valuesTestValidation': [random.randint(2, 4)], # noqa: S311
|
|
98
105
|
},
|
|
99
106
|
'A195646': {
|
|
100
107
|
'getMapShape': lambda n: tuple(3 for _dimension in range(n)),
|
|
@@ -106,7 +113,7 @@ settingsOEIShardcodedValues: dict[str, SettingsOEIShardcodedValues] = {
|
|
|
106
113
|
'getMapShape': lambda n: (n, n),
|
|
107
114
|
'valuesBenchmark': [5],
|
|
108
115
|
'valuesTestParallelization': [*range(2, 4)],
|
|
109
|
-
'valuesTestValidation': [random.randint(2, 4)],
|
|
116
|
+
'valuesTestValidation': [random.randint(2, 4)], # noqa: S311
|
|
110
117
|
},
|
|
111
118
|
}
|
|
112
119
|
"""
|
|
@@ -117,24 +124,32 @@ the static configuration needed to work with that sequence, including the mappin
|
|
|
117
124
|
sequence index to map shape and various test parameter sets.
|
|
118
125
|
"""
|
|
119
126
|
|
|
120
|
-
oeisIDsImplemented: Final[list[str]] = sorted([oeisID.upper().strip() for oeisID in settingsOEIShardcodedValues
|
|
127
|
+
oeisIDsImplemented: Final[list[str]] = sorted([oeisID.upper().strip() for oeisID in settingsOEIShardcodedValues])
|
|
121
128
|
"""Directly implemented OEIS IDs; standardized, e.g., 'A001415'."""
|
|
122
129
|
|
|
123
130
|
def validateOEISid(oeisIDcandidate: str) -> str:
|
|
124
|
-
"""
|
|
125
|
-
|
|
131
|
+
"""Validate an OEIS sequence ID against implemented sequences.
|
|
132
|
+
|
|
133
|
+
(AI generated docstring)
|
|
126
134
|
|
|
127
135
|
If the provided ID is recognized within the application's implemented OEIS sequences, the function returns the
|
|
128
136
|
verified ID in uppercase. Otherwise, a KeyError is raised indicating that the sequence is not directly supported.
|
|
129
137
|
|
|
130
|
-
Parameters
|
|
131
|
-
|
|
138
|
+
Parameters
|
|
139
|
+
----------
|
|
140
|
+
oeisIDcandidate : str
|
|
141
|
+
The OEIS sequence identifier to validate.
|
|
142
|
+
|
|
143
|
+
Returns
|
|
144
|
+
-------
|
|
145
|
+
str
|
|
146
|
+
The validated and possibly modified OEIS sequence ID, if recognized.
|
|
132
147
|
|
|
133
|
-
|
|
134
|
-
|
|
148
|
+
Raises
|
|
149
|
+
------
|
|
150
|
+
KeyError
|
|
151
|
+
If the provided sequence ID is not directly implemented.
|
|
135
152
|
|
|
136
|
-
Raises:
|
|
137
|
-
KeyError: If the provided sequence ID is not directly implemented.
|
|
138
153
|
"""
|
|
139
154
|
if oeisIDcandidate in oeisIDsImplemented:
|
|
140
155
|
return oeisIDcandidate
|
|
@@ -143,43 +158,59 @@ def validateOEISid(oeisIDcandidate: str) -> str:
|
|
|
143
158
|
if oeisIDcleaned in oeisIDsImplemented:
|
|
144
159
|
return oeisIDcleaned
|
|
145
160
|
else:
|
|
146
|
-
|
|
161
|
+
message = (
|
|
147
162
|
f"OEIS ID {oeisIDcandidate} is not directly implemented.\n"
|
|
148
163
|
f"Available sequences:\n{_formatOEISsequenceInfo()}"
|
|
149
164
|
)
|
|
165
|
+
raise KeyError(
|
|
166
|
+
message
|
|
167
|
+
)
|
|
150
168
|
|
|
151
169
|
def getFilenameOEISbFile(oeisID: str) -> str:
|
|
152
|
-
"""
|
|
153
|
-
|
|
170
|
+
"""Generate the filename for an OEIS b-file given a sequence ID.
|
|
171
|
+
|
|
172
|
+
(AI generated docstring)
|
|
154
173
|
|
|
155
174
|
OEIS b-files contain sequence values in a standardized format and follow the naming convention
|
|
156
175
|
'b{sequence_number}.txt', where the sequence number excludes the 'A' prefix.
|
|
157
176
|
|
|
158
|
-
Parameters
|
|
159
|
-
|
|
177
|
+
Parameters
|
|
178
|
+
----------
|
|
179
|
+
oeisID : str
|
|
180
|
+
The OEIS sequence identifier to convert to a b-file filename.
|
|
181
|
+
|
|
182
|
+
Returns
|
|
183
|
+
-------
|
|
184
|
+
str
|
|
185
|
+
The corresponding b-file filename for the given sequence ID.
|
|
160
186
|
|
|
161
|
-
Returns:
|
|
162
|
-
filename: The corresponding b-file filename for the given sequence ID.
|
|
163
187
|
"""
|
|
164
188
|
oeisID = validateOEISid(oeisID)
|
|
165
189
|
return f"b{oeisID[1:]}.txt"
|
|
166
190
|
|
|
167
|
-
def _parseBFileOEIS(OEISbFile: str
|
|
168
|
-
"""
|
|
169
|
-
|
|
191
|
+
def _parseBFileOEIS(OEISbFile: str) -> dict[int, int]:
|
|
192
|
+
"""Parse the content of an OEIS b-file into a sequence dictionary.
|
|
193
|
+
|
|
194
|
+
(AI generated docstring)
|
|
170
195
|
|
|
171
196
|
OEIS b-files contain sequence data in a standardized two-column format where each line represents
|
|
172
197
|
an index-value pair. Comment lines beginning with '#' are ignored during parsing.
|
|
173
198
|
|
|
174
|
-
Parameters
|
|
175
|
-
|
|
176
|
-
|
|
199
|
+
Parameters
|
|
200
|
+
----------
|
|
201
|
+
OEISbFile : str
|
|
202
|
+
A multiline string representing the content of an OEIS b-file.
|
|
203
|
+
|
|
204
|
+
Returns
|
|
205
|
+
-------
|
|
206
|
+
OEISsequence : dict[int, int]
|
|
207
|
+
A dictionary mapping sequence indices to their corresponding values.
|
|
177
208
|
|
|
178
|
-
|
|
179
|
-
|
|
209
|
+
Raises
|
|
210
|
+
------
|
|
211
|
+
ValueError
|
|
212
|
+
If the file content format is invalid or cannot be parsed.
|
|
180
213
|
|
|
181
|
-
Raises:
|
|
182
|
-
ValueError: If the file content format is invalid or cannot be parsed.
|
|
183
214
|
"""
|
|
184
215
|
bFileLines: list[str] = OEISbFile.strip().splitlines()
|
|
185
216
|
|
|
@@ -192,98 +223,120 @@ def _parseBFileOEIS(OEISbFile: str, oeisID: str) -> dict[int, int]:
|
|
|
192
223
|
return OEISsequence
|
|
193
224
|
|
|
194
225
|
def getOEISofficial(pathFilenameCache: Path, url: str) -> None | str:
|
|
195
|
-
"""
|
|
196
|
-
|
|
226
|
+
"""Retrieve OEIS sequence data from cache or online source with intelligent caching.
|
|
227
|
+
|
|
228
|
+
(AI generated docstring)
|
|
197
229
|
|
|
198
230
|
This function implements a caching strategy that prioritizes local cached data when it exists and
|
|
199
231
|
has not expired. Fresh data is retrieved from the OEIS website when the cache is stale or missing,
|
|
200
232
|
and the cache is updated for future use.
|
|
201
233
|
|
|
202
|
-
Parameters
|
|
203
|
-
|
|
204
|
-
|
|
234
|
+
Parameters
|
|
235
|
+
----------
|
|
236
|
+
pathFilenameCache : Path
|
|
237
|
+
Path to the local cache file for storing retrieved data.
|
|
238
|
+
url : str
|
|
239
|
+
URL to retrieve the OEIS sequence data from if cache is invalid or missing.
|
|
205
240
|
|
|
206
|
-
Returns
|
|
207
|
-
|
|
241
|
+
Returns
|
|
242
|
+
-------
|
|
243
|
+
oeisInformation : str | None
|
|
244
|
+
The retrieved OEIS sequence information as a string, or `None` if retrieval failed.
|
|
245
|
+
|
|
246
|
+
Notes
|
|
247
|
+
-----
|
|
248
|
+
Cache expiration is controlled by the module-level `cacheDays` variable. The function validates
|
|
249
|
+
URL schemes and issues warnings for failed retrievals.
|
|
208
250
|
|
|
209
|
-
Notes:
|
|
210
|
-
Cache expiration is controlled by the module-level `cacheDays` variable. The function validates
|
|
211
|
-
URL schemes and issues warnings for failed retrievals.
|
|
212
251
|
"""
|
|
213
252
|
tryCache: bool = False
|
|
214
253
|
if pathFilenameCache.exists():
|
|
215
|
-
fileAge: timedelta = datetime.now() - datetime.fromtimestamp(pathFilenameCache.stat().st_mtime)
|
|
254
|
+
fileAge: timedelta = datetime.now(tz=UTC) - datetime.fromtimestamp(pathFilenameCache.stat().st_mtime, tz=UTC)
|
|
216
255
|
tryCache = fileAge < timedelta(days=cacheDays)
|
|
217
256
|
|
|
218
257
|
oeisInformation: str | None = None
|
|
219
258
|
if tryCache:
|
|
220
259
|
try:
|
|
221
|
-
oeisInformation = pathFilenameCache.read_text()
|
|
260
|
+
oeisInformation = pathFilenameCache.read_text(encoding="utf-8")
|
|
222
261
|
except OSError:
|
|
223
262
|
tryCache = False
|
|
224
263
|
|
|
225
264
|
if not tryCache:
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
oeisInformation = str(oeisInformationRaw)
|
|
233
|
-
writeStringToHere(oeisInformation, pathFilenameCache)
|
|
265
|
+
if not url.startswith(("http:", "https:")):
|
|
266
|
+
raise ValueError("URL must start with 'http:' or 'https:'")
|
|
267
|
+
with urlopen(url) as response:
|
|
268
|
+
oeisInformationRaw = response.read().decode('utf-8')
|
|
269
|
+
oeisInformation = str(oeisInformationRaw)
|
|
270
|
+
writeStringToHere(oeisInformation, pathFilenameCache)
|
|
234
271
|
|
|
235
272
|
if not oeisInformation:
|
|
236
|
-
warnings.warn(f"Failed to retrieve OEIS sequence information for {pathFilenameCache.stem}.")
|
|
273
|
+
warnings.warn(f"Failed to retrieve OEIS sequence information for {pathFilenameCache.stem}.", stacklevel=2)
|
|
237
274
|
|
|
238
275
|
return oeisInformation
|
|
239
276
|
|
|
240
277
|
def getOEISidValues(oeisID: str) -> dict[int, int]:
|
|
241
|
-
"""
|
|
242
|
-
|
|
278
|
+
"""Retrieve known sequence values for a specified OEIS sequence.
|
|
279
|
+
|
|
280
|
+
(AI generated docstring)
|
|
243
281
|
|
|
244
282
|
This function fetches the complete set of known values for an OEIS sequence by accessing cached
|
|
245
283
|
data when available or retrieving fresh data from the OEIS website. The data is parsed from the
|
|
246
284
|
standard OEIS b-file format.
|
|
247
285
|
|
|
248
|
-
Parameters
|
|
249
|
-
|
|
286
|
+
Parameters
|
|
287
|
+
----------
|
|
288
|
+
oeisID : str
|
|
289
|
+
The identifier of the OEIS sequence to retrieve.
|
|
250
290
|
|
|
251
|
-
Returns
|
|
252
|
-
|
|
291
|
+
Returns
|
|
292
|
+
-------
|
|
293
|
+
OEISsequence : dict[int, int]
|
|
294
|
+
A dictionary mapping sequence indices to their corresponding values, or a fallback
|
|
253
295
|
dictionary containing {-1: -1} if retrieval fails.
|
|
254
296
|
|
|
255
|
-
Raises
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
297
|
+
Raises
|
|
298
|
+
------
|
|
299
|
+
ValueError
|
|
300
|
+
If the cached or downloaded file format is invalid.
|
|
301
|
+
IOError
|
|
302
|
+
If there is an error reading from or writing to the local cache.
|
|
259
303
|
|
|
304
|
+
"""
|
|
260
305
|
pathFilenameCache: Path = pathCache / getFilenameOEISbFile(oeisID)
|
|
261
306
|
url: str = f"https://oeis.org/{oeisID}/{getFilenameOEISbFile(oeisID)}"
|
|
262
307
|
|
|
263
308
|
oeisInformation: None | str = getOEISofficial(pathFilenameCache, url)
|
|
264
309
|
|
|
265
310
|
if oeisInformation:
|
|
266
|
-
return _parseBFileOEIS(oeisInformation
|
|
311
|
+
return _parseBFileOEIS(oeisInformation)
|
|
267
312
|
return {-1: -1}
|
|
268
313
|
|
|
269
314
|
def getOEISidInformation(oeisID: str) -> tuple[str, int]:
|
|
270
|
-
"""
|
|
271
|
-
|
|
315
|
+
"""Retrieve the description and offset metadata for an OEIS sequence.
|
|
316
|
+
|
|
317
|
+
(AI generated docstring)
|
|
272
318
|
|
|
273
319
|
This function extracts the mathematical description and starting index offset from OEIS sequence
|
|
274
320
|
metadata using the machine-readable text format. It employs the same caching mechanism as other
|
|
275
321
|
retrieval functions to minimize network requests.
|
|
276
322
|
|
|
277
|
-
Parameters
|
|
278
|
-
|
|
323
|
+
Parameters
|
|
324
|
+
----------
|
|
325
|
+
oeisID : str
|
|
326
|
+
The OEIS sequence identifier to retrieve metadata for.
|
|
327
|
+
|
|
328
|
+
Returns
|
|
329
|
+
-------
|
|
330
|
+
description : str
|
|
331
|
+
A human-readable string describing the sequence's mathematical meaning.
|
|
332
|
+
offset : int
|
|
333
|
+
The starting index of the sequence, typically 0 or 1 depending on mathematical context.
|
|
279
334
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
335
|
+
Notes
|
|
336
|
+
-----
|
|
337
|
+
Descriptions are parsed from OEIS %N entries and offsets from %O entries. If metadata cannot
|
|
338
|
+
be retrieved, warning messages are issued and fallback values are returned.
|
|
283
339
|
|
|
284
|
-
Notes:
|
|
285
|
-
Descriptions are parsed from OEIS %N entries and offsets from %O entries. If metadata cannot
|
|
286
|
-
be retrieved, warning messages are issued and fallback values are returned.
|
|
287
340
|
"""
|
|
288
341
|
oeisID = validateOEISid(oeisID)
|
|
289
342
|
pathFilenameCache: Path = pathCache / f"{oeisID}.txt"
|
|
@@ -296,8 +349,8 @@ def getOEISidInformation(oeisID: str) -> tuple[str, int]:
|
|
|
296
349
|
listDescriptionDeconstructed: list[str] = []
|
|
297
350
|
offset = None
|
|
298
351
|
for lineOEIS in oeisInformation.splitlines():
|
|
299
|
-
lineOEIS = lineOEIS.strip()
|
|
300
|
-
if not lineOEIS or len(lineOEIS.split()) < 3:
|
|
352
|
+
lineOEIS = lineOEIS.strip() # noqa: PLW2901
|
|
353
|
+
if not lineOEIS or len(lineOEIS.split()) < 3: # noqa: PLR2004
|
|
301
354
|
continue
|
|
302
355
|
fieldCode, sequenceID, fieldData = lineOEIS.split(maxsplit=2)
|
|
303
356
|
if fieldCode == '%N' and sequenceID == oeisID:
|
|
@@ -306,17 +359,18 @@ def getOEISidInformation(oeisID: str) -> tuple[str, int]:
|
|
|
306
359
|
offsetAsStr: str = fieldData.split(',')[0]
|
|
307
360
|
offset = int(offsetAsStr)
|
|
308
361
|
if not listDescriptionDeconstructed:
|
|
309
|
-
warnings.warn(f"No description found for {oeisID}")
|
|
362
|
+
warnings.warn(f"No description found for {oeisID}", stacklevel=2)
|
|
310
363
|
listDescriptionDeconstructed.append("No description found")
|
|
311
364
|
if offset is None:
|
|
312
|
-
warnings.warn(f"No offset found for {oeisID}")
|
|
365
|
+
warnings.warn(f"No offset found for {oeisID}", stacklevel=2)
|
|
313
366
|
offset = -1
|
|
314
367
|
description: str = ' '.join(listDescriptionDeconstructed)
|
|
315
368
|
return description, offset
|
|
316
369
|
|
|
317
370
|
def makeSettingsOEIS() -> dict[str, SettingsOEIS]:
|
|
318
|
-
"""
|
|
319
|
-
|
|
371
|
+
"""Construct the comprehensive settings dictionary for all implemented OEIS sequences.
|
|
372
|
+
|
|
373
|
+
(AI generated docstring)
|
|
320
374
|
|
|
321
375
|
This function builds the complete configuration dictionary by merging hardcoded settings with
|
|
322
376
|
dynamically retrieved data from OEIS. For each implemented sequence, it combines:
|
|
@@ -329,9 +383,12 @@ def makeSettingsOEIS() -> dict[str, SettingsOEIS]:
|
|
|
329
383
|
operations throughout the package, enabling consistent access to sequence definitions, known values,
|
|
330
384
|
and operational parameters.
|
|
331
385
|
|
|
332
|
-
Returns
|
|
333
|
-
|
|
386
|
+
Returns
|
|
387
|
+
-------
|
|
388
|
+
settingsTarget : dict[str, SettingsOEIS]
|
|
389
|
+
A comprehensive dictionary mapping OEIS sequence IDs to their complete settings
|
|
334
390
|
objects, containing all metadata and known values needed for computation and validation.
|
|
391
|
+
|
|
335
392
|
"""
|
|
336
393
|
settingsTarget: dict[str, SettingsOEIS] = {}
|
|
337
394
|
for oeisID in oeisIDsImplemented:
|
|
@@ -361,17 +418,21 @@ hardcoded configurations with dynamically retrieved OEIS data during module init
|
|
|
361
418
|
|
|
362
419
|
@cache
|
|
363
420
|
def makeDictionaryFoldsTotalKnown() -> dict[tuple[int, ...], int]:
|
|
364
|
-
"""
|
|
365
|
-
|
|
421
|
+
"""Create a cached lookup dictionary mapping map shapes to their known folding totals.
|
|
422
|
+
|
|
423
|
+
(AI generated docstring)
|
|
366
424
|
|
|
367
425
|
This function processes all known sequence values from implemented OEIS sequences and creates
|
|
368
426
|
a unified dictionary that maps map dimension tuples to their corresponding folding totals. The
|
|
369
427
|
resulting dictionary enables rapid lookup of known values without requiring knowledge of which
|
|
370
428
|
specific OEIS sequence contains the data.
|
|
371
429
|
|
|
372
|
-
Returns
|
|
373
|
-
|
|
374
|
-
|
|
430
|
+
Returns
|
|
431
|
+
-------
|
|
432
|
+
dictionaryMapDimensionsToFoldsTotalKnown : dict[tuple[int, ...], int]
|
|
433
|
+
A dictionary where keys are tuples representing map shapes and values are the total number
|
|
434
|
+
of distinct folding patterns for those shapes.
|
|
435
|
+
|
|
375
436
|
"""
|
|
376
437
|
dictionaryMapDimensionsToFoldsTotalKnown: dict[tuple[int, ...], int] = {}
|
|
377
438
|
|
|
@@ -385,36 +446,47 @@ def makeDictionaryFoldsTotalKnown() -> dict[tuple[int, ...], int]:
|
|
|
385
446
|
return dictionaryMapDimensionsToFoldsTotalKnown
|
|
386
447
|
|
|
387
448
|
def getFoldsTotalKnown(mapShape: tuple[int, ...]) -> int:
|
|
388
|
-
"""
|
|
389
|
-
|
|
449
|
+
"""Retrieve the known total number of distinct folding patterns for a given map shape.
|
|
450
|
+
|
|
451
|
+
(AI generated docstring)
|
|
390
452
|
|
|
391
453
|
This function provides rapid access to precalculated folding totals from OEIS sequences without
|
|
392
454
|
requiring computation. It serves as a validation reference for algorithm results and enables
|
|
393
455
|
quick lookup of known values across all implemented sequences.
|
|
394
456
|
|
|
395
|
-
Parameters
|
|
396
|
-
|
|
457
|
+
Parameters
|
|
458
|
+
----------
|
|
459
|
+
mapShape : tuple[int, ...]
|
|
460
|
+
A tuple of integers representing the dimensions of the map.
|
|
397
461
|
|
|
398
|
-
Returns
|
|
399
|
-
|
|
462
|
+
Returns
|
|
463
|
+
-------
|
|
464
|
+
foldingsTotal : int
|
|
465
|
+
The known total number of distinct folding patterns for the given map shape,
|
|
400
466
|
or -1 if the map shape does not match any known values in the OEIS sequences.
|
|
401
467
|
|
|
402
|
-
Notes
|
|
403
|
-
|
|
404
|
-
|
|
468
|
+
Notes
|
|
469
|
+
-----
|
|
470
|
+
The function uses a cached dictionary for efficient retrieval without repeatedly processing
|
|
471
|
+
OEIS data. Map shapes are matched exactly as provided without internal sorting or normalization.
|
|
472
|
+
|
|
405
473
|
"""
|
|
406
474
|
lookupFoldsTotal = makeDictionaryFoldsTotalKnown()
|
|
407
475
|
return lookupFoldsTotal.get(tuple(mapShape), -1)
|
|
408
476
|
|
|
409
477
|
def _formatHelpText() -> str:
|
|
410
|
-
"""
|
|
411
|
-
|
|
478
|
+
"""Format comprehensive help text for both command-line and interactive use.
|
|
479
|
+
|
|
480
|
+
(AI generated docstring)
|
|
412
481
|
|
|
413
482
|
This function generates standardized help documentation that includes all available OEIS sequences
|
|
414
483
|
with their descriptions and provides usage examples for both command-line and programmatic interfaces.
|
|
415
484
|
|
|
416
|
-
Returns
|
|
417
|
-
|
|
485
|
+
Returns
|
|
486
|
+
-------
|
|
487
|
+
helpText : str
|
|
488
|
+
A formatted string containing complete usage information and examples.
|
|
489
|
+
|
|
418
490
|
"""
|
|
419
491
|
exampleOEISid: str = oeisIDsImplemented[0]
|
|
420
492
|
exampleN: int = settingsOEIS[exampleOEISid]['valuesTestValidation'][-1]
|
|
@@ -431,14 +503,18 @@ def _formatHelpText() -> str:
|
|
|
431
503
|
)
|
|
432
504
|
|
|
433
505
|
def _formatOEISsequenceInfo() -> str:
|
|
434
|
-
"""
|
|
435
|
-
|
|
506
|
+
"""Format information about available OEIS sequences for display in help messages and error output.
|
|
507
|
+
|
|
508
|
+
(AI generated docstring)
|
|
436
509
|
|
|
437
510
|
This function creates a standardized listing of all implemented OEIS sequences with their mathematical
|
|
438
511
|
descriptions, suitable for inclusion in help text and error messages.
|
|
439
512
|
|
|
440
|
-
Returns
|
|
441
|
-
|
|
513
|
+
Returns
|
|
514
|
+
-------
|
|
515
|
+
sequenceInfo : str
|
|
516
|
+
A formatted string listing each OEIS sequence ID with its description.
|
|
517
|
+
|
|
442
518
|
"""
|
|
443
519
|
return "\n".join(
|
|
444
520
|
f" {oeisID}: {settingsOEIS[oeisID]['description']}"
|
|
@@ -446,46 +522,60 @@ def _formatOEISsequenceInfo() -> str:
|
|
|
446
522
|
)
|
|
447
523
|
|
|
448
524
|
def oeisIDfor_n(oeisID: str, n: int | Any) -> int:
|
|
449
|
-
"""
|
|
450
|
-
|
|
525
|
+
"""Calculate the value a(n) for a specified OEIS sequence and index.
|
|
526
|
+
|
|
527
|
+
(AI generated docstring)
|
|
451
528
|
|
|
452
529
|
This function serves as the primary interface for computing OEIS sequence values within the map folding
|
|
453
530
|
context. For small values or values within the known range, it returns cached OEIS data. For larger
|
|
454
531
|
values, it computes the result using the map folding algorithm with the appropriate map shape derived
|
|
455
532
|
from the sequence definition.
|
|
456
533
|
|
|
457
|
-
Parameters
|
|
458
|
-
|
|
459
|
-
|
|
534
|
+
Parameters
|
|
535
|
+
----------
|
|
536
|
+
oeisID : str
|
|
537
|
+
The identifier of the OEIS sequence to evaluate.
|
|
538
|
+
n : int | Any
|
|
539
|
+
A non-negative integer index for which to calculate the sequence value.
|
|
460
540
|
|
|
461
|
-
Returns
|
|
462
|
-
|
|
541
|
+
Returns
|
|
542
|
+
-------
|
|
543
|
+
int
|
|
544
|
+
The value a(n) of the specified OEIS sequence.
|
|
545
|
+
|
|
546
|
+
Raises
|
|
547
|
+
------
|
|
548
|
+
ValueError
|
|
549
|
+
If n is not a non-negative integer.
|
|
550
|
+
KeyError
|
|
551
|
+
If the OEIS sequence ID is not directly implemented.
|
|
552
|
+
ArithmeticError
|
|
553
|
+
If n is below the sequence's defined offset.
|
|
463
554
|
|
|
464
|
-
Raises:
|
|
465
|
-
ValueError: If n is not a non-negative integer.
|
|
466
|
-
KeyError: If the OEIS sequence ID is not directly implemented.
|
|
467
|
-
ArithmeticError: If n is below the sequence's defined offset.
|
|
468
555
|
"""
|
|
469
556
|
oeisID = validateOEISid(oeisID)
|
|
470
557
|
|
|
471
558
|
if not isinstance(n, int) or n < 0:
|
|
472
|
-
|
|
559
|
+
message = f"I received `{n = }` in the form of `{type(n) = }`, but it must be non-negative integer in the form of `{int}`."
|
|
560
|
+
raise ValueError(message)
|
|
473
561
|
|
|
474
562
|
mapShape: tuple[int, ...] = settingsOEIS[oeisID]['getMapShape'](n)
|
|
475
563
|
|
|
476
|
-
if n <= 1 or len(mapShape) < 2:
|
|
564
|
+
if n <= 1 or len(mapShape) < 2: # noqa: PLR2004
|
|
477
565
|
offset: int = settingsOEIS[oeisID]['offset']
|
|
478
566
|
if n < offset:
|
|
479
|
-
|
|
567
|
+
message = f"OEIS sequence {oeisID} is not defined at {n = }."
|
|
568
|
+
raise ArithmeticError(message)
|
|
480
569
|
foldsTotal: int = settingsOEIS[oeisID]['valuesKnown'][n]
|
|
481
570
|
return foldsTotal
|
|
482
571
|
return countFolds(mapShape)
|
|
483
572
|
|
|
484
573
|
def OEIS_for_n() -> None:
|
|
485
|
-
"""
|
|
486
|
-
|
|
574
|
+
"""Command-line interface for calculating OEIS sequence values.
|
|
575
|
+
|
|
576
|
+
(AI generated docstring)
|
|
487
577
|
|
|
488
|
-
This function provides a command-line interface to the oeisIDfor_n function, enabling users to
|
|
578
|
+
This function provides a command-line interface to the `oeisIDfor_n` function, enabling users to
|
|
489
579
|
calculate specific values of implemented OEIS sequences from the terminal. It includes argument
|
|
490
580
|
parsing, error handling, and performance timing to provide a complete user experience.
|
|
491
581
|
|
|
@@ -493,11 +583,15 @@ def OEIS_for_n() -> None:
|
|
|
493
583
|
then outputs the calculated sequence value along with execution time. Error messages are directed
|
|
494
584
|
to stderr with appropriate exit codes for shell scripting integration.
|
|
495
585
|
|
|
496
|
-
Usage
|
|
497
|
-
|
|
586
|
+
Usage
|
|
587
|
+
-----
|
|
588
|
+
python -m mapFolding.oeis OEIS_for_n A001415 10
|
|
589
|
+
|
|
590
|
+
Raises
|
|
591
|
+
------
|
|
592
|
+
SystemExit
|
|
593
|
+
With code 1 if invalid arguments are provided or computation fails.
|
|
498
594
|
|
|
499
|
-
Raises:
|
|
500
|
-
SystemExit: With code 1 if invalid arguments are provided or computation fails.
|
|
501
595
|
"""
|
|
502
596
|
parserCLI = argparse.ArgumentParser(
|
|
503
597
|
description="Calculate a(n) for an OEIS sequence.",
|
|
@@ -512,17 +606,18 @@ def OEIS_for_n() -> None:
|
|
|
512
606
|
timeStart: float = time.perf_counter()
|
|
513
607
|
|
|
514
608
|
try:
|
|
515
|
-
print(oeisIDfor_n(argumentsCLI.oeisID, argumentsCLI.n), "distinct folding patterns.")
|
|
609
|
+
print(oeisIDfor_n(argumentsCLI.oeisID, argumentsCLI.n), "distinct folding patterns.") # noqa: T201
|
|
516
610
|
except (KeyError, ValueError, ArithmeticError) as ERRORmessage:
|
|
517
|
-
print(f"Error: {ERRORmessage}", file=sys.stderr)
|
|
611
|
+
print(f"Error: {ERRORmessage}", file=sys.stderr) # noqa: T201
|
|
518
612
|
sys.exit(1)
|
|
519
613
|
|
|
520
614
|
timeElapsed: float = time.perf_counter() - timeStart
|
|
521
|
-
print(f"Time elapsed: {timeElapsed:.3f} seconds")
|
|
615
|
+
print(f"Time elapsed: {timeElapsed:.3f} seconds") # noqa: T201
|
|
522
616
|
|
|
523
617
|
def clearOEIScache() -> None:
|
|
524
|
-
"""
|
|
525
|
-
|
|
618
|
+
"""Delete all cached OEIS sequence files from the local cache directory.
|
|
619
|
+
|
|
620
|
+
(AI generated docstring)
|
|
526
621
|
|
|
527
622
|
This function removes all cached OEIS data files, including both sequence value files (b-files)
|
|
528
623
|
and metadata files, forcing fresh retrieval from the OEIS website on the next access. This is
|
|
@@ -530,18 +625,20 @@ def clearOEIScache() -> None:
|
|
|
530
625
|
|
|
531
626
|
The function safely handles missing files and provides user feedback about the cache clearing
|
|
532
627
|
operation. If the cache directory does not exist, an informative message is displayed.
|
|
628
|
+
|
|
533
629
|
"""
|
|
534
630
|
if not pathCache.exists():
|
|
535
|
-
print(f"Cache directory, {pathCache}, not found - nothing to clear.")
|
|
631
|
+
print(f"Cache directory, {pathCache}, not found - nothing to clear.") # noqa: T201
|
|
536
632
|
return
|
|
537
633
|
for oeisID in settingsOEIS:
|
|
538
634
|
( pathCache / f"{oeisID}.txt" ).unlink(missing_ok=True)
|
|
539
635
|
( pathCache / getFilenameOEISbFile(oeisID) ).unlink(missing_ok=True)
|
|
540
|
-
print(f"Cache cleared from {pathCache}")
|
|
636
|
+
print(f"Cache cleared from {pathCache}") # noqa: T201
|
|
541
637
|
|
|
542
638
|
def getOEISids() -> None:
|
|
543
|
-
"""
|
|
544
|
-
|
|
639
|
+
"""Display comprehensive information about all implemented OEIS sequences.
|
|
640
|
+
|
|
641
|
+
(AI generated docstring)
|
|
545
642
|
|
|
546
643
|
This function serves as the primary help interface for the module, displaying detailed information
|
|
547
644
|
about all directly implemented OEIS sequences along with usage examples for both command-line and
|
|
@@ -550,8 +647,9 @@ def getOEISids() -> None:
|
|
|
550
647
|
|
|
551
648
|
The output includes sequence identifiers, mathematical descriptions, and practical usage examples
|
|
552
649
|
to help users understand how to access and utilize the OEIS interface functionality.
|
|
650
|
+
|
|
553
651
|
"""
|
|
554
|
-
print(_formatHelpText())
|
|
652
|
+
print(_formatHelpText()) # noqa: T201
|
|
555
653
|
|
|
556
654
|
if __name__ == "__main__":
|
|
557
655
|
getOEISids()
|