hunterMakesPy 0.1.2__py3-none-any.whl → 0.2.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.
- hunterMakesPy/__init__.py +7 -2
- hunterMakesPy/dataStructures.py +128 -125
- hunterMakesPy/filesystemToolkit.py +17 -9
- hunterMakesPy/parseParameters.py +29 -29
- hunterMakesPy/pytestForYourUse.py +5 -322
- hunterMakesPy/tests/__init__.py +5 -0
- {tests → hunterMakesPy/tests}/conftest.py +32 -2
- {tests → hunterMakesPy/tests}/test_coping.py +1 -1
- {tests → hunterMakesPy/tests}/test_dataStructures.py +124 -118
- hunterMakesPy/tests/test_filesystemToolkit.py +263 -0
- hunterMakesPy/tests/test_parseParameters.py +339 -0
- {huntermakespy-0.1.2.dist-info → huntermakespy-0.2.1.dist-info}/METADATA +24 -27
- huntermakespy-0.2.1.dist-info/RECORD +20 -0
- {huntermakespy-0.1.2.dist-info → huntermakespy-0.2.1.dist-info}/top_level.txt +0 -1
- huntermakespy-0.1.2.dist-info/RECORD +0 -20
- tests/__init__.py +0 -0
- tests/test_filesystemToolkit.py +0 -43
- tests/test_parseParameters.py +0 -21
- {huntermakespy-0.1.2.dist-info → huntermakespy-0.2.1.dist-info}/WHEEL +0 -0
- {huntermakespy-0.1.2.dist-info → huntermakespy-0.2.1.dist-info}/licenses/LICENSE +0 -0
hunterMakesPy/__init__.py
CHANGED
|
@@ -7,6 +7,7 @@ This package provides:
|
|
|
7
7
|
- Utilities for string extraction from nested data structures and merging dictionaries of lists.
|
|
8
8
|
|
|
9
9
|
"""
|
|
10
|
+
# pyright: reportUnusedImport=false
|
|
10
11
|
from hunterMakesPy.theTypes import identifierDotAttribute as identifierDotAttribute
|
|
11
12
|
|
|
12
13
|
from hunterMakesPy.coping import PackageSettings as PackageSettings, raiseIfNone as raiseIfNone
|
|
@@ -18,7 +19,11 @@ from hunterMakesPy.filesystemToolkit import (importLogicalPath2Identifier as imp
|
|
|
18
19
|
importPathFilename2Identifier as importPathFilename2Identifier, makeDirsSafely as makeDirsSafely,
|
|
19
20
|
writeStringToHere as writeStringToHere)
|
|
20
21
|
|
|
21
|
-
from hunterMakesPy.dataStructures import
|
|
22
|
-
|
|
22
|
+
from hunterMakesPy.dataStructures import stringItUp as stringItUp, updateExtendPolishDictionaryLists as updateExtendPolishDictionaryLists
|
|
23
|
+
|
|
24
|
+
import sys
|
|
25
|
+
|
|
26
|
+
if sys.version_info < (3, 14):
|
|
27
|
+
from hunterMakesPy.dataStructures import autoDecodingRLE as autoDecodingRLE
|
|
23
28
|
|
|
24
29
|
from hunterMakesPy._theSSOT import settingsPackage
|
hunterMakesPy/dataStructures.py
CHANGED
|
@@ -5,156 +5,159 @@ from numpy import integer
|
|
|
5
5
|
from numpy.typing import NDArray
|
|
6
6
|
from typing import Any
|
|
7
7
|
import more_itertools
|
|
8
|
-
import python_minifier
|
|
9
8
|
import re as regex
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
if sys.version_info < (3, 14):
|
|
12
|
+
import python_minifier
|
|
13
|
+
|
|
14
|
+
def autoDecodingRLE(arrayTarget: NDArray[integer[Any]], *, assumeAddSpaces: bool = False) -> str: # noqa: C901, PLR0915
|
|
15
|
+
"""Transform a NumPy array into a compact, self-decoding run-length encoded string representation.
|
|
16
|
+
|
|
17
|
+
This function converts a NumPy array into a string that, when evaluated as Python code,
|
|
18
|
+
recreates the original array structure. The function employs two compression strategies:
|
|
19
|
+
1. Python's `range` syntax for consecutive integer sequences
|
|
20
|
+
2. Multiplication syntax for repeated elements
|
|
21
|
+
|
|
22
|
+
The resulting string representation is designed to be both human-readable and space-efficient,
|
|
23
|
+
especially for large cartesian mappings with repetitive patterns. When this string is used
|
|
24
|
+
as a data source, Python will automatically decode it into Python `list`, which if used as an
|
|
25
|
+
argument to `numpy.array()`, will recreate the original array structure.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
arrayTarget : NDArray[integer[Any]]
|
|
30
|
+
(array2target) The NumPy array to be encoded.
|
|
31
|
+
assumeAddSpaces : bool = False
|
|
32
|
+
(assume2add2spaces) Affects internal length comparison during compression decisions.
|
|
33
|
+
This parameter doesn't directly change output format but influences whether
|
|
34
|
+
`range` or multiplication syntax is preferred in certain cases. The parameter
|
|
35
|
+
exists because the Abstract Syntax Tree (AST) inserts spaces in its string
|
|
36
|
+
representation.
|
|
37
|
+
|
|
38
|
+
Returns
|
|
39
|
+
-------
|
|
40
|
+
rleString : str
|
|
41
|
+
(rle2string) A string representation of the array using run-length encoding that,
|
|
42
|
+
when evaluated as Python code, reproduces the original array structure.
|
|
43
|
+
|
|
44
|
+
Notes
|
|
45
|
+
-----
|
|
46
|
+
The "autoDecoding" feature means that the string representation evaluates directly
|
|
47
|
+
to the desired data structure without explicit decompression steps.
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
def sliceNDArrayToNestedLists(arraySlice: NDArray[integer[Any]]) -> Any:
|
|
51
|
+
def getLengthOption(optionAsStr: str) -> int:
|
|
52
|
+
"""`assumeAddSpaces` characters: `,` 1; `]*` 2."""
|
|
53
|
+
return assumeAddSpaces * (optionAsStr.count(',') + optionAsStr.count(']*') * 2) + len(optionAsStr)
|
|
54
|
+
|
|
55
|
+
if arraySlice.ndim > 1:
|
|
56
|
+
axisOfOperation = 0
|
|
57
|
+
return [sliceNDArrayToNestedLists(arraySlice[index]) for index in range(arraySlice.shape[axisOfOperation])]
|
|
58
|
+
if arraySlice.ndim == 1:
|
|
59
|
+
arraySliceAsList: list[int | range] = []
|
|
60
|
+
cache_consecutiveGroup_addMe: dict[Iterator[Any], list[int] | list[range]] = {}
|
|
61
|
+
for consecutiveGroup in more_itertools.consecutive_groups(arraySlice.tolist()):
|
|
62
|
+
if consecutiveGroup in cache_consecutiveGroup_addMe:
|
|
63
|
+
addMe = cache_consecutiveGroup_addMe[consecutiveGroup]
|
|
64
|
+
else:
|
|
65
|
+
ImaSerious: list[int] = list(consecutiveGroup)
|
|
66
|
+
ImaRange = [range(ImaSerious[0], ImaSerious[-1] + 1)]
|
|
67
|
+
ImaRangeAsStr = python_minifier.minify(str(ImaRange)).replace('range(0,', 'range(').replace('range', '*range')
|
|
10
68
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
recreates the original array structure. The function employs two compression strategies:
|
|
16
|
-
1. Python's `range` syntax for consecutive integer sequences
|
|
17
|
-
2. Multiplication syntax for repeated elements
|
|
18
|
-
|
|
19
|
-
The resulting string representation is designed to be both human-readable and space-efficient,
|
|
20
|
-
especially for large cartesian mappings with repetitive patterns. When this string is used
|
|
21
|
-
as a data source, Python will automatically decode it into Python `list`, which if used as an
|
|
22
|
-
argument to `numpy.array()`, will recreate the original array structure.
|
|
23
|
-
|
|
24
|
-
Parameters
|
|
25
|
-
----------
|
|
26
|
-
arrayTarget : NDArray[integer[Any]]
|
|
27
|
-
(array2target) The NumPy array to be encoded.
|
|
28
|
-
assumeAddSpaces : bool = False
|
|
29
|
-
(assume2add2spaces) Affects internal length comparison during compression decisions.
|
|
30
|
-
This parameter doesn't directly change output format but influences whether
|
|
31
|
-
`range` or multiplication syntax is preferred in certain cases. The parameter
|
|
32
|
-
exists because the Abstract Syntax Tree (AST) inserts spaces in its string
|
|
33
|
-
representation.
|
|
34
|
-
|
|
35
|
-
Returns
|
|
36
|
-
-------
|
|
37
|
-
rleString : str
|
|
38
|
-
(rle2string) A string representation of the array using run-length encoding that,
|
|
39
|
-
when evaluated as Python code, reproduces the original array structure.
|
|
69
|
+
option1 = ImaRange
|
|
70
|
+
option1AsStr = ImaRangeAsStr
|
|
71
|
+
option2 = ImaSerious
|
|
72
|
+
option2AsStr = None
|
|
40
73
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
to the desired data structure without explicit decompression steps.
|
|
74
|
+
# alpha, potential function
|
|
75
|
+
option1AsStr = option1AsStr or python_minifier.minify(str(option1))
|
|
76
|
+
lengthOption1 = getLengthOption(option1AsStr)
|
|
45
77
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def getLengthOption(optionAsStr: str) -> int:
|
|
49
|
-
"""`assumeAddSpaces` characters: `,` 1; `]*` 2."""
|
|
50
|
-
return assumeAddSpaces * (optionAsStr.count(',') + optionAsStr.count(']*') * 2) + len(optionAsStr)
|
|
51
|
-
|
|
52
|
-
if arraySlice.ndim > 1:
|
|
53
|
-
axisOfOperation = 0
|
|
54
|
-
return [sliceNDArrayToNestedLists(arraySlice[index]) for index in range(arraySlice.shape[axisOfOperation])]
|
|
55
|
-
if arraySlice.ndim == 1:
|
|
56
|
-
arraySliceAsList: list[int | range] = []
|
|
57
|
-
cache_consecutiveGroup_addMe: dict[Iterator[Any], list[int] | list[range]] = {}
|
|
58
|
-
for consecutiveGroup in more_itertools.consecutive_groups(arraySlice.tolist()):
|
|
59
|
-
if consecutiveGroup in cache_consecutiveGroup_addMe:
|
|
60
|
-
addMe = cache_consecutiveGroup_addMe[consecutiveGroup]
|
|
61
|
-
else:
|
|
62
|
-
ImaSerious: list[int] = list(consecutiveGroup)
|
|
63
|
-
ImaRange = [range(ImaSerious[0], ImaSerious[-1] + 1)]
|
|
64
|
-
ImaRangeAsStr = python_minifier.minify(str(ImaRange)).replace('range(0,', 'range(').replace('range', '*range')
|
|
78
|
+
option2AsStr = option2AsStr or python_minifier.minify(str(option2))
|
|
79
|
+
lengthOption2 = getLengthOption(option2AsStr)
|
|
65
80
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
81
|
+
if lengthOption1 < lengthOption2:
|
|
82
|
+
addMe = option1
|
|
83
|
+
else:
|
|
84
|
+
addMe = option2
|
|
70
85
|
|
|
71
|
-
|
|
72
|
-
option1AsStr = option1AsStr or python_minifier.minify(str(option1))
|
|
73
|
-
lengthOption1 = getLengthOption(option1AsStr)
|
|
86
|
+
cache_consecutiveGroup_addMe[consecutiveGroup] = addMe
|
|
74
87
|
|
|
75
|
-
|
|
76
|
-
lengthOption2 = getLengthOption(option2AsStr)
|
|
88
|
+
arraySliceAsList += addMe
|
|
77
89
|
|
|
78
|
-
|
|
79
|
-
|
|
90
|
+
listRangeAndTuple: list[int | range | tuple[int | range, int]] = []
|
|
91
|
+
cache_malkovichGrouped_addMe: dict[tuple[int | range, int], list[tuple[int | range, int]] | list[int | range]] = {}
|
|
92
|
+
for malkovichGrouped in more_itertools.run_length.encode(arraySliceAsList):
|
|
93
|
+
if malkovichGrouped in cache_malkovichGrouped_addMe:
|
|
94
|
+
addMe = cache_malkovichGrouped_addMe[malkovichGrouped]
|
|
80
95
|
else:
|
|
81
|
-
|
|
96
|
+
lengthMalkovich = malkovichGrouped[-1]
|
|
97
|
+
malkovichAsList = list(more_itertools.run_length.decode([malkovichGrouped]))
|
|
98
|
+
malkovichMalkovich = f"[{malkovichGrouped[0]}]*{lengthMalkovich}"
|
|
82
99
|
|
|
83
|
-
|
|
100
|
+
option1 = [malkovichGrouped]
|
|
101
|
+
option1AsStr = malkovichMalkovich
|
|
102
|
+
option2 = malkovichAsList
|
|
103
|
+
option2AsStr = None
|
|
84
104
|
|
|
85
|
-
|
|
105
|
+
# beta, potential function
|
|
106
|
+
option1AsStr = option1AsStr or python_minifier.minify(str(option1))
|
|
107
|
+
lengthOption1 = getLengthOption(option1AsStr)
|
|
86
108
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
for malkovichGrouped in more_itertools.run_length.encode(arraySliceAsList):
|
|
90
|
-
if malkovichGrouped in cache_malkovichGrouped_addMe:
|
|
91
|
-
addMe = cache_malkovichGrouped_addMe[malkovichGrouped]
|
|
92
|
-
else:
|
|
93
|
-
lengthMalkovich = malkovichGrouped[-1]
|
|
94
|
-
malkovichAsList = list(more_itertools.run_length.decode([malkovichGrouped]))
|
|
95
|
-
malkovichMalkovich = f"[{malkovichGrouped[0]}]*{lengthMalkovich}"
|
|
109
|
+
option2AsStr = option2AsStr or python_minifier.minify(str(option2))
|
|
110
|
+
lengthOption2 = getLengthOption(option2AsStr)
|
|
96
111
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
# beta, potential function
|
|
103
|
-
option1AsStr = option1AsStr or python_minifier.minify(str(option1))
|
|
104
|
-
lengthOption1 = getLengthOption(option1AsStr)
|
|
105
|
-
|
|
106
|
-
option2AsStr = option2AsStr or python_minifier.minify(str(option2))
|
|
107
|
-
lengthOption2 = getLengthOption(option2AsStr)
|
|
108
|
-
|
|
109
|
-
if lengthOption1 < lengthOption2:
|
|
110
|
-
addMe = option1
|
|
111
|
-
else:
|
|
112
|
-
addMe = option2
|
|
112
|
+
if lengthOption1 < lengthOption2:
|
|
113
|
+
addMe = option1
|
|
114
|
+
else:
|
|
115
|
+
addMe = option2
|
|
113
116
|
|
|
114
|
-
|
|
117
|
+
cache_malkovichGrouped_addMe[malkovichGrouped] = addMe
|
|
115
118
|
|
|
116
|
-
|
|
119
|
+
listRangeAndTuple += addMe
|
|
117
120
|
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
return listRangeAndTuple
|
|
122
|
+
return arraySlice
|
|
120
123
|
|
|
121
|
-
|
|
124
|
+
arrayAsNestedLists = sliceNDArrayToNestedLists(arrayTarget)
|
|
122
125
|
|
|
123
|
-
|
|
126
|
+
arrayAsStr = python_minifier.minify(str(arrayAsNestedLists))
|
|
124
127
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
128
|
+
patternRegex = regex.compile(
|
|
129
|
+
"(?<!rang)(?:"
|
|
130
|
+
# Pattern 1: Comma ahead, bracket behind # noqa: ERA001
|
|
131
|
+
"(?P<joinAhead>,)\\((?P<malkovich>\\d+),(?P<multiply>\\d+)\\)(?P<bracketBehind>])|"
|
|
132
|
+
# Pattern 2: Bracket or start ahead, comma behind # noqa: ERA001
|
|
133
|
+
"(?P<bracketOrStartAhead>\\[|^.)\\((?P<malkovichMalkovich>\\d+),(?P<multiplyIDK>\\d+)\\)(?P<joinBehind>,)|"
|
|
134
|
+
# Pattern 3: Bracket ahead, bracket behind # noqa: ERA001
|
|
135
|
+
"(?P<bracketAhead>\\[)\\((?P<malkovichMalkovichMalkovich>\\d+),(?P<multiply_whatever>\\d+)\\)(?P<bracketBehindBracketBehind>])|"
|
|
136
|
+
# Pattern 4: Comma ahead, comma behind # noqa: ERA001
|
|
137
|
+
"(?P<joinAheadJoinAhead>,)\\((?P<malkovichMalkovichMalkovichMalkovich>\\d+),(?P<multiplyOrSomething>\\d+)\\)(?P<joinBehindJoinBehind>,)"
|
|
138
|
+
")"
|
|
139
|
+
)
|
|
137
140
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
141
|
+
def replacementByContext(match: regex.Match[str]) -> str:
|
|
142
|
+
"""Generate replacement string based on context patterns."""
|
|
143
|
+
elephino = match.groupdict()
|
|
144
|
+
joinAhead = elephino.get('joinAhead') or elephino.get('joinAheadJoinAhead')
|
|
145
|
+
malkovich = elephino.get('malkovich') or elephino.get('malkovichMalkovich') or elephino.get('malkovichMalkovichMalkovich') or elephino.get('malkovichMalkovichMalkovichMalkovich')
|
|
146
|
+
multiply = elephino.get('multiply') or elephino.get('multiplyIDK') or elephino.get('multiply_whatever') or elephino.get('multiplyOrSomething')
|
|
147
|
+
joinBehind = elephino.get('joinBehind') or elephino.get('joinBehindJoinBehind')
|
|
145
148
|
|
|
146
|
-
|
|
149
|
+
replaceAhead = "]+[" if joinAhead == "," else "["
|
|
147
150
|
|
|
148
|
-
|
|
151
|
+
replaceBehind = "+[" if joinBehind == "," else ""
|
|
149
152
|
|
|
150
|
-
|
|
153
|
+
return f"{replaceAhead}{malkovich}]*{multiply}{replaceBehind}"
|
|
151
154
|
|
|
152
|
-
|
|
153
|
-
|
|
155
|
+
arrayAsStr = patternRegex.sub(replacementByContext, arrayAsStr)
|
|
156
|
+
arrayAsStr = patternRegex.sub(replacementByContext, arrayAsStr)
|
|
154
157
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
+
# Replace `range(0,stop)` syntax with `range(stop)` syntax. # noqa: ERA001
|
|
159
|
+
# Add unpack operator `*` for automatic decoding when evaluated.
|
|
160
|
+
return arrayAsStr.replace('range(0,', 'range(').replace('range', '*range')
|
|
158
161
|
|
|
159
162
|
def stringItUp(*scrapPile: Any) -> list[str]: # noqa: C901
|
|
160
163
|
"""Convert, if possible, every element in the input data structure to a string.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""File system and module import utilities.
|
|
2
2
|
|
|
3
|
-
This module provides basic file I/O utilities such as importing callables from modules,
|
|
3
|
+
This module provides basic file I/O utilities such as importing callables from modules, safely creating directories, and writing to files or streams (pipes).
|
|
4
4
|
|
|
5
5
|
"""
|
|
6
6
|
|
|
@@ -97,17 +97,25 @@ def makeDirsSafely(pathFilename: Any) -> None:
|
|
|
97
97
|
with contextlib.suppress(OSError):
|
|
98
98
|
Path(pathFilename).parent.mkdir(parents=True, exist_ok=True)
|
|
99
99
|
|
|
100
|
-
def writeStringToHere(this: str, pathFilename: PathLike[Any] | PurePath) -> None:
|
|
101
|
-
"""Write a string to a file
|
|
100
|
+
def writeStringToHere(this: str, pathFilename: PathLike[Any] | PurePath | io.TextIOBase) -> None:
|
|
101
|
+
"""Write a string to a file or text stream.
|
|
102
|
+
|
|
103
|
+
This function writes a string to either a file path or an open text stream. For file paths, it creates parent directories as
|
|
104
|
+
needed and writes with UTF-8 encoding. For text streams, it writes directly to the stream and flushes the buffer.
|
|
102
105
|
|
|
103
106
|
Parameters
|
|
104
107
|
----------
|
|
105
108
|
this : str
|
|
106
|
-
The string content to write
|
|
107
|
-
pathFilename : PathLike[Any] | PurePath
|
|
108
|
-
The
|
|
109
|
+
The string content to write.
|
|
110
|
+
pathFilename : PathLike[Any] | PurePath | io.TextIOBase
|
|
111
|
+
The target destination: either a file path or an open text stream.
|
|
109
112
|
|
|
110
113
|
"""
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
if isinstance(pathFilename, io.TextIOBase):
|
|
115
|
+
pathFilename.write(str(this))
|
|
116
|
+
pathFilename.flush()
|
|
117
|
+
else:
|
|
118
|
+
pathFilename = Path(pathFilename)
|
|
119
|
+
makeDirsSafely(pathFilename)
|
|
120
|
+
pathFilename.write_text(str(this), encoding='utf-8')
|
|
121
|
+
|
hunterMakesPy/parseParameters.py
CHANGED
|
@@ -9,18 +9,16 @@ import multiprocessing
|
|
|
9
9
|
class ErrorMessageContext:
|
|
10
10
|
"""Context information for constructing error messages.
|
|
11
11
|
|
|
12
|
-
(AI generated docstring)
|
|
13
|
-
|
|
14
12
|
Parameters
|
|
15
13
|
----------
|
|
16
14
|
parameterValue : Any = None
|
|
17
|
-
|
|
15
|
+
The value that caused the error.
|
|
18
16
|
parameterValueType : str | None = None
|
|
19
|
-
|
|
17
|
+
The name of the type of the parameter value.
|
|
20
18
|
containerType : str | None = None
|
|
21
|
-
|
|
19
|
+
The name of the type of the container holding the parameter.
|
|
22
20
|
isElement : bool = False
|
|
23
|
-
|
|
21
|
+
Whether the parameter is an element within a container.
|
|
24
22
|
|
|
25
23
|
"""
|
|
26
24
|
|
|
@@ -32,7 +30,8 @@ class ErrorMessageContext:
|
|
|
32
30
|
def _constructErrorMessage(context: ErrorMessageContext, parameterName: str, parameterType: type[Any] | None) -> str:
|
|
33
31
|
"""Construct error message from available context using template.
|
|
34
32
|
|
|
35
|
-
I received ["value" | a value | `None`] [of type `type` | `None`] [as an element in | `None`] [a `containerType` type |
|
|
33
|
+
I received ["value" | a value | `None`] [of type `type` | `None`] [as an element in | `None`] [a `containerType` type |
|
|
34
|
+
`None`] but `parameterName` must have integers [in type(s) `parameterType` | `None`].
|
|
36
35
|
|
|
37
36
|
Hypothetically, this is a prototype that can be generalized to other functions. In this package and a few of my other
|
|
38
37
|
packages, I have developed standardized error messages, but those are quite different from this. I will certainly continue to
|
|
@@ -41,16 +40,16 @@ def _constructErrorMessage(context: ErrorMessageContext, parameterName: str, par
|
|
|
41
40
|
Parameters
|
|
42
41
|
----------
|
|
43
42
|
context : ErrorMessageContext
|
|
44
|
-
|
|
43
|
+
The error context containing parameter value, type, and container information.
|
|
45
44
|
parameterName : str
|
|
46
|
-
|
|
45
|
+
The name of the parameter that caused the error.
|
|
47
46
|
parameterType : type[Any] | None
|
|
48
|
-
|
|
47
|
+
The expected type of the parameter, used in error messages.
|
|
49
48
|
|
|
50
49
|
Returns
|
|
51
50
|
-------
|
|
52
51
|
errorMessage : str
|
|
53
|
-
|
|
52
|
+
The constructed error message string.
|
|
54
53
|
|
|
55
54
|
"""
|
|
56
55
|
messageParts = ["I received "]
|
|
@@ -79,7 +78,8 @@ def _constructErrorMessage(context: ErrorMessageContext, parameterName: str, par
|
|
|
79
78
|
def defineConcurrencyLimit(*, limit: bool | float | int | None, cpuTotal: int = multiprocessing.cpu_count()) -> int: # noqa: C901, PYI041
|
|
80
79
|
"""Determine the concurrency limit based on the provided parameter.
|
|
81
80
|
|
|
82
|
-
|
|
81
|
+
Tests for this function can be run with:
|
|
82
|
+
`from hunterMakesPy.tests.test_parseParameters import PytestFor_defineConcurrencyLimit`
|
|
83
83
|
|
|
84
84
|
Parameters
|
|
85
85
|
----------
|
|
@@ -92,12 +92,12 @@ def defineConcurrencyLimit(*, limit: bool | float | int | None, cpuTotal: int =
|
|
|
92
92
|
Returns
|
|
93
93
|
-------
|
|
94
94
|
concurrencyLimit : int
|
|
95
|
-
|
|
95
|
+
The calculated concurrency limit, ensuring it is at least 1.
|
|
96
96
|
|
|
97
97
|
Notes
|
|
98
98
|
-----
|
|
99
|
-
If you want to be extra nice to your users, consider using `hunterMakesPy.oopsieKwargsie()` to handle
|
|
100
|
-
|
|
99
|
+
If you want to be extra nice to your users, consider using `hunterMakesPy.oopsieKwargsie()` to handle malformed inputs. For
|
|
100
|
+
example:
|
|
101
101
|
|
|
102
102
|
```
|
|
103
103
|
if not (CPUlimit is None or isinstance(CPUlimit, (bool, int, float))):
|
|
@@ -163,23 +163,25 @@ def defineConcurrencyLimit(*, limit: bool | float | int | None, cpuTotal: int =
|
|
|
163
163
|
def intInnit(listInt_Allegedly: Iterable[Any], parameterName: str | None = None, parameterType: type[Any] | None = None) -> list[int]: # noqa: C901, PLR0912, PLR0915
|
|
164
164
|
"""Validate and convert input values to a `list` of integers.
|
|
165
165
|
|
|
166
|
-
Accepts various numeric types and attempts to convert them into integers while providing descriptive error messages.
|
|
166
|
+
Accepts various numeric types and attempts to convert them into integers while providing descriptive error messages. This
|
|
167
|
+
package includes Pytest tests that can be imported and run: `from hunterMakesPy.tests.test_parseParameters import
|
|
168
|
+
PytestFor_intInnit`.
|
|
167
169
|
|
|
168
170
|
Parameters
|
|
169
171
|
----------
|
|
170
172
|
listInt_Allegedly : Iterable[Any]
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
Rejects boolean values and non-integer numeric values.
|
|
173
|
+
The input sequence that should contain integer-compatible values. Accepts integers, strings, floats, complex numbers, and
|
|
174
|
+
binary data. Rejects boolean values and non-integer numeric values.
|
|
174
175
|
parameterName : str | None = None
|
|
175
|
-
|
|
176
|
+
Name of the parameter from your function for which this function is validating the input validated. If there is an error
|
|
177
|
+
message, it provides context to your user. Defaults to 'the parameter'.
|
|
176
178
|
parameterType : type[Any] | None = None
|
|
177
|
-
|
|
179
|
+
Expected type(s) of the parameter, used in error messages.
|
|
178
180
|
|
|
179
181
|
Returns
|
|
180
182
|
-------
|
|
181
183
|
listValidated : list[int]
|
|
182
|
-
|
|
184
|
+
A `list` containing validated integers.
|
|
183
185
|
|
|
184
186
|
Raises
|
|
185
187
|
------
|
|
@@ -192,9 +194,6 @@ def intInnit(listInt_Allegedly: Iterable[Any], parameterName: str | None = None,
|
|
|
192
194
|
|
|
193
195
|
Notes
|
|
194
196
|
-----
|
|
195
|
-
This package includes Pytest tests that can be imported and run:
|
|
196
|
-
`from hunterMakesPy.pytest_parseParameters import makeTestSuiteIntInnit`
|
|
197
|
-
|
|
198
197
|
The function performs strict validation and follows fail-early principles to catch potential issues before they become catastrophic.
|
|
199
198
|
|
|
200
199
|
"""
|
|
@@ -284,19 +283,20 @@ def intInnit(listInt_Allegedly: Iterable[Any], parameterName: str | None = None,
|
|
|
284
283
|
def oopsieKwargsie(huh: Any) -> bool | None | str:
|
|
285
284
|
"""Interpret a `str` as `True`, `False`, or `None` to avoid an `Exception`.
|
|
286
285
|
|
|
287
|
-
If a calling function passes a `str` to a parameter that shouldn't receive a `str`, `oopsieKwargsie()` might help you avoid an
|
|
288
|
-
`
|
|
286
|
+
If a calling function passes a `str` to a parameter that shouldn't receive a `str`, `oopsieKwargsie()` might help you avoid an
|
|
287
|
+
`Exception`. It tries to interpret the string as `True`, `False`, or `None`.
|
|
288
|
+
|
|
289
|
+
Tests for this function can be run with: `from hunterMakesPy.tests.test_parseParameters import PytestFor_oopsieKwargsie`.
|
|
289
290
|
|
|
290
291
|
Parameters
|
|
291
292
|
----------
|
|
292
293
|
huh : Any
|
|
293
294
|
(huh) The input string to be parsed.
|
|
294
295
|
|
|
295
|
-
|
|
296
296
|
Returns
|
|
297
297
|
-------
|
|
298
298
|
interpretedValue : bool | None | str
|
|
299
|
-
|
|
299
|
+
The reserved keyword `True`, `False`, or `None` or the original string, `huh`.
|
|
300
300
|
|
|
301
301
|
"""
|
|
302
302
|
if not isinstance(huh, str):
|