none-shall-parse 0.2.2__tar.gz → 0.3.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: none-shall-parse
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Trinity Shared Python utilities.
5
5
  Author: Andries Niemandt, Jan Badenhorst
6
6
  Author-email: Andries Niemandt <andries.niemandt@trintel.co.za>, Jan Badenhorst <jan@trintel.co.za>
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "none-shall-parse"
7
- version = "0.2.2"
7
+ version = "0.3.0"
8
8
  description = "Trinity Shared Python utilities."
9
9
  readme = "README.md"
10
10
  authors = [
@@ -0,0 +1,212 @@
1
+ from typing import Union
2
+
3
+ LUHN_DOUBLES = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
4
+
5
+
6
+ def get_luhn_digit(n: Union[str, int]) -> int:
7
+ """
8
+ Calculates the Luhn checksum digit for a given number.
9
+
10
+ The function processes a number using the Luhn algorithm. It computes
11
+ the Luhn checksum digit based on the provided digits, ensuring that the
12
+ resulting number adheres to the Luhn standard. The method is useful for
13
+ validating numerical identifiers like credit card numbers or IMEIs.
14
+
15
+ Parameters:
16
+ n (str | int): A number represented as a string or integer that
17
+ requires Luhn checksum digit calculation.
18
+
19
+ Returns:
20
+ int: The Luhn checksum digit as an integer.
21
+ """
22
+ chars = [int(ch) for ch in str(n)]
23
+ firsts = [ch for ch in chars[0::2]]
24
+ doubles = [LUHN_DOUBLES[ch] for ch in chars[1::2]]
25
+ check = 10 - divmod(sum((sum(firsts), sum(doubles))), 10)[1]
26
+ return divmod(check, 10)[1]
27
+
28
+
29
+ def is_valid_luhn(n: Union[str, int]) -> bool:
30
+ """
31
+ Determines if a given number, represented as a string or integer, adheres
32
+ to the Luhn algorithm.
33
+
34
+ The Luhn algorithm, also known as the mod 10 algorithm, is a simple checksum
35
+ formula used to validate identification numbers such as credit card numbers.
36
+
37
+ Parameters:
38
+ n : Union[str, int]
39
+ The input number to be validated. It can be provided as a string or an integer.
40
+
41
+ Returns:
42
+ bool
43
+ Returns True if the input number satisfies the Luhn algorithm; otherwise, False.
44
+ """
45
+ n = "".join(
46
+ [
47
+ e
48
+ for e in n
49
+ if e
50
+ in [
51
+ 0,
52
+ 1,
53
+ 2,
54
+ 3,
55
+ 4,
56
+ 5,
57
+ 6,
58
+ 7,
59
+ 8,
60
+ 9,
61
+ "0",
62
+ "1",
63
+ "2",
64
+ "3",
65
+ "4",
66
+ "5",
67
+ "6",
68
+ "7",
69
+ "8",
70
+ "9",
71
+ ]
72
+ ]
73
+ )
74
+ chars = [int(ch) for ch in str(n)][::-1] # Reversed Digits
75
+ firsts = [ch for ch in chars[0::2]]
76
+ doubles = [LUHN_DOUBLES[ch] for ch in chars[1::2]]
77
+ final = sum((sum(firsts), sum(doubles)))
78
+ return divmod(final, 10)[1] == 0
79
+
80
+
81
+ def is_valid_imei(n: Union[str, int]) -> bool:
82
+ """
83
+ Determines whether the given number is a valid IMEI (International Mobile
84
+ Equipment Identity) number.
85
+
86
+ An IMEI number is a 15-digit unique identifier for a mobile device. This function
87
+ first checks that the length of the input is 15 characters and then validates
88
+ it using the Luhn algorithm.
89
+
90
+ Parameters:
91
+ n: Union[str, int]
92
+ The number to be checked, represented as a string or integer.
93
+
94
+ Returns:
95
+ bool
96
+ True if the given number is a valid IMEI, otherwise False.
97
+ """
98
+ return len(str(n)) == 15 and is_valid_luhn(n)
99
+
100
+
101
+ def normalize_imei(c: Union[str, int]) -> str:
102
+ """
103
+ Normalizes the given IMEI number by extracting the first 14 digits and appending
104
+ the calculated Luhn check digit to make it a valid IMEI.
105
+
106
+ The IMEI (International Mobile Equipment Identity) is a unique identifier
107
+ typically consisting of 15 digits. This function ensures that the provided
108
+ IMEI-like input is converted into a valid IMEI format by calculating and appending
109
+ the appropriate check digit.
110
+
111
+ Parameters:
112
+ c: Union[str, int]
113
+ The input IMEI or a value resembling an IMEI. It can be provided as a string
114
+ or an integer.
115
+
116
+ Returns:
117
+ str
118
+ A 15-digit valid IMEI as a string.
119
+
120
+ Raises:
121
+ Exception
122
+ Raises any exceptions occurring internally within the `get_luhn_digit` function
123
+ if the calculation of the check digit fails.
124
+
125
+ Notes:
126
+ This function assumes the presence of the `get_luhn_digit` function for Luhn
127
+ digit calculation.
128
+ """
129
+ t = str(c)[:14]
130
+ check_digit = get_luhn_digit(t)
131
+ return "%s%s" % (t, check_digit)
132
+
133
+
134
+ def get_tac_from_imei(n: Union[str, int]) -> tuple[bool, str]:
135
+ """
136
+ Determines the validity of an IMEI number and extracts its TAC if valid.
137
+
138
+ This function checks whether a provided IMEI (International Mobile Equipment
139
+ Identity) number is valid based on IMEI validation rules. If the given IMEI
140
+ is valid, the function also extracts and returns the TAC (Type Allocation
141
+ Code), which corresponds to the first 8 digits of the IMEI.
142
+
143
+ Parameters:
144
+ n (str): The IMEI number to be validated and processed.
145
+
146
+ Returns:
147
+ tuple: A tuple containing a boolean indicating whether the IMEI is valid
148
+ and a string representing the TAC if valid or a placeholder if invalid.
149
+ """
150
+ tac = "Not a Valid IMEI"
151
+ is_valid = is_valid_imei(n)
152
+ if not is_valid:
153
+ return False, tac
154
+ else:
155
+ tac = str(n)[:8]
156
+ return True, tac
157
+
158
+
159
+ def decrement_imei(n: Union[str, int]) -> tuple[bool, str]:
160
+ """
161
+ Decrements the given IMEI number by one and normalizes it.
162
+
163
+ This function validates the provided IMEI number. If it is a valid IMEI, the
164
+ function decrements the first 14 digits by one and computes the new IMEI
165
+ checksum to generate a normalized IMEI. If the provided IMEI is not valid,
166
+ it returns a failure status and an error message.
167
+
168
+ Parameters:
169
+ n: int
170
+ The IMEI number to be validated and decremented.
171
+
172
+ Returns:
173
+ tuple[bool, str]
174
+ A tuple where the first element is a boolean indicating whether the
175
+ operation was successful, and the second element is the resulting IMEI
176
+ or an error message if the input was invalid.
177
+ """
178
+ result = "Not a Valid IMEI"
179
+ is_valid = is_valid_imei(n)
180
+ if not is_valid:
181
+ return False, result
182
+ else:
183
+ result = normalize_imei(int(str(n)[:14]) - 1)
184
+ return True, result
185
+
186
+
187
+ def increment_imei(n: Union[str, int]) -> tuple[bool, str]:
188
+ """
189
+ Determines if a given IMEI number is valid and increments it by 1 if valid.
190
+
191
+ This function first checks if the provided IMEI number is valid using the
192
+ is_valid_imei function. If the input is a valid IMEI, it increments the IMEI
193
+ value by 1 while retaining only the first 14 digits. If the input is not valid,
194
+ it returns a predefined invalid result.
195
+
196
+ Parameters:
197
+ n: int
198
+ IMEI number to be validated and potentially incremented.
199
+
200
+ Returns:
201
+ tuple[bool, str]
202
+ A tuple where the first element is a boolean indicating whether the operation
203
+ was successful, and the second element is a string containing the incremented
204
+ IMEI number if valid or an error message if not valid.
205
+ """
206
+ result = "Not a Valid IMEI"
207
+ is_valid = is_valid_imei(n)
208
+ if not is_valid:
209
+ return False, result
210
+ else:
211
+ result = normalize_imei(int(str(n)[:14]) + 1)
212
+ return True, result
@@ -1,8 +1,6 @@
1
1
  import collections
2
- from typing import Iterable, Generator, Any, TypeVar, List
3
-
4
- T = TypeVar('T')
5
-
2
+ from typing import Iterable, Generator, Any, List
3
+ from .types import T
6
4
 
7
5
  def flatten(some_list: Iterable) -> Generator[Any, None, None]:
8
6
  """
@@ -1,9 +1,8 @@
1
- from typing import Any, Sequence, Tuple, Callable
1
+ from typing import Callable, Any
2
2
  from typing import Union
3
3
 
4
- from .strings import slugify
5
-
6
- ChoicesType = Sequence[Tuple[Union[int, str], str]]
4
+ from src.none_shall_parse.strings import slugify
5
+ from src.none_shall_parse.types import ChoicesType, StringLike
7
6
 
8
7
  _true_set = {'yes', 'true', 't', 'y', '1'}
9
8
  _false_set = {'no', 'false', 'f', 'n', '0'}
@@ -125,7 +124,7 @@ def int_or_none(s: int | float | str | None) -> int | None:
125
124
 
126
125
  def choices_code_to_string(
127
126
  choices: ChoicesType, default: str | None = None,
128
- to_slug: bool = False) -> Callable[[Union[int, str]], str | None]:
127
+ to_slug: bool = False) -> Callable[[Union[int, StringLike]], StringLike | None]:
129
128
  """
130
129
  Converts a code to a corresponding string representation based on provided choices.
131
130
  The function allows optional fallback to a default value and can slugify the resulting string
@@ -0,0 +1,247 @@
1
+ import base64
2
+ import hashlib
3
+ import itertools
4
+ import random
5
+ import re
6
+ import secrets
7
+ import string
8
+ import unicodedata
9
+ from typing import Any
10
+
11
+ _control_chars = "".join(
12
+ map(chr, itertools.chain(range(0x00, 0x20), range(0x7F, 0xA0))))
13
+ _re_control_char = re.compile("[%s]" % re.escape(_control_chars))
14
+ _re_combine_whitespace = re.compile(r"\s+")
15
+
16
+
17
+ def slugify(value: object, allow_unicode: bool = False) -> str:
18
+ """
19
+ Maps directly to Django's slugify function.
20
+ Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
21
+ dashes to single dashes. Remove characters that aren't alphanumerics,
22
+ underscores, or hyphens. Convert to lowercase. Also strip leading and
23
+ trailing whitespace, dashes, and underscores.
24
+ """
25
+ value = str(value)
26
+ if allow_unicode:
27
+ value = unicodedata.normalize("NFKC", value)
28
+ else:
29
+ value = (
30
+ unicodedata.normalize("NFKD", value)
31
+ .encode("ascii", "ignore")
32
+ .decode("ascii")
33
+ )
34
+ value = re.sub(r"[^\w\s-]", "", value.lower())
35
+ return re.sub(r"[-\s]+", "-", value).strip("-_")
36
+
37
+
38
+ def random_16():
39
+ return "".join(random.choices(string.ascii_letters + string.digits, k=16))
40
+
41
+
42
+ def to_human_string(s: Any) -> tuple[Any | str, bool]:
43
+ """
44
+ Cleans up a string by removing extra whitespace and control characters.
45
+
46
+ Removes unnecessary whitespace and control characters from the input string.
47
+ This function is designed to validate and clean user-provided string input
48
+ while preserving input that does not require modification. It returns the
49
+ cleaned string along with a boolean indicating whether changes were made
50
+ to the original string.
51
+
52
+ Parameters:
53
+ s : any
54
+ The input value to clean and validate. If it is not a string, it is
55
+ returned unchanged with a modification flag of False.
56
+
57
+ Returns:
58
+ tuple
59
+ A tuple where the first element is the cleaned string (or the original
60
+ input if it is not a string), and the second element is a boolean
61
+ indicating whether the string was modified.
62
+ """
63
+ if not isinstance(s, str):
64
+ return s, False
65
+
66
+ c = _re_combine_whitespace.sub(" ", s).strip()
67
+ clean_string = _re_control_char.sub("", c)
68
+ if clean_string == s:
69
+ return s, False
70
+ return clean_string, True
71
+
72
+
73
+ def is_quoted_string(s: str, strip: bool = False) -> tuple[bool, str]:
74
+ """
75
+ Checks if a given string is enclosed in quotes and optionally strips the quotes.
76
+
77
+ The function determines whether a given string starts and ends with matching quotes,
78
+ either single quotes (') or double quotes ("). If the string is quoted and
79
+ the `strip` parameter is set to True, it removes the enclosing quotes and returns
80
+ the unquoted string.
81
+
82
+ Parameters:
83
+ s : str
84
+ The input string to check and possibly process.
85
+ strip : bool, optional
86
+ Indicates whether to remove the enclosing quotes if the string is quoted.
87
+ Defaults to False.
88
+
89
+ Returns:
90
+ tuple[bool, str]
91
+ A tuple where the first element is a boolean indicating whether the string
92
+ is quoted, and the second element is the original string or the stripped
93
+ version if `strip` is True.
94
+ """
95
+ is_quoted = False
96
+ result = s
97
+ if not isinstance(s, str):
98
+ return is_quoted, result
99
+
100
+ if s[0] == s[-1]:
101
+ if s[0] in ['"', "'"]:
102
+ is_quoted = True
103
+ if strip:
104
+ if s[0] == "'":
105
+ result = s.strip("'")
106
+ elif s[0] == '"':
107
+ result = s.strip('"')
108
+ return is_quoted, result
109
+
110
+
111
+ def is_numeric_string(s: str, convert: bool = False) -> tuple[bool, str | int | float]:
112
+ """
113
+ Checks if the given string represents a numeric value and optionally converts it.
114
+
115
+ This function determines if the provided string represents a numeric value.
116
+ If the input is numeric and the `convert` flag is set to True, it returns
117
+ the numeric value converted to either an integer (if the float represents
118
+ an integer) or a float. If the input is not numeric, it returns the original
119
+ input string.
120
+
121
+ Parameters
122
+ ----------
123
+ s : str
124
+ The input string to check.
125
+ convert : bool, optional
126
+ A flag indicating whether to convert the numeric string to a numeric
127
+ type (default is False).
128
+
129
+ Returns
130
+ -------
131
+ tuple
132
+ A tuple containing a boolean and the result:
133
+ - A boolean indicating whether the input string is numeric or not.
134
+ - The numeric value if `convert` is True and the string is numeric;
135
+ otherwise, the original string.
136
+ """
137
+ is_numeric = False
138
+ result = s
139
+ f = None
140
+ if not isinstance(s, str):
141
+ return is_numeric, result
142
+ try:
143
+ f = float(s)
144
+ is_numeric = True
145
+ except ValueError:
146
+ is_numeric = False
147
+
148
+ if is_numeric and convert:
149
+ result = int(f) if f.is_integer() else f
150
+
151
+ return is_numeric, result
152
+
153
+
154
+ def custom_slug(s: str) -> str:
155
+ # Remove all non-word characters (everything except numbers and letters)
156
+ s = re.sub(r"[^\w\s]", "", s)
157
+
158
+ # Replace all runs of whitespace with a single dash
159
+ s = re.sub(r"\s+", "_", s)
160
+
161
+ return s
162
+
163
+
164
+ def b64_encode(s: str | bytes) -> str:
165
+ """
166
+ Encodes a string or bytes into its Base64 representation.
167
+
168
+ This function takes an input, either a string or bytes, and encodes it
169
+ into its Base64 representation. If the input is a string, it is first
170
+ encoded into bytes using UTF-8 encoding. The resulting Base64 encoded
171
+ value is returned as a string.
172
+
173
+ Args:
174
+ s: The input to encode, which can be either a string or bytes.
175
+
176
+ Returns:
177
+ The Base64 encoded representation of the input as a string.
178
+ """
179
+ if isinstance(s, str):
180
+ s = s.encode("utf-8")
181
+ return base64.b64encode(s).decode("utf-8")
182
+
183
+
184
+ def b64_decode(s: str) -> bytes:
185
+ """
186
+ Decodes a Base64 encoded string to its original binary format.
187
+
188
+ This function takes a Base64 encoded string and decodes it to its
189
+ original bytes form. Base64 encoding may omit padding characters, so
190
+ the function ensures the input is properly padded before decoding.
191
+
192
+ Parameters:
193
+ s: str
194
+ A Base64 encoded string that needs to be decoded.
195
+
196
+ Returns:
197
+ bytes
198
+ The decoded binary data.
199
+
200
+ Raises:
201
+ ValueError
202
+ If the input string contains invalid Base64 characters.
203
+ """
204
+ pad = "=" * (-len(s) % 4)
205
+ return base64.b64decode(s + pad)
206
+
207
+
208
+ def calc_hash(*args: Any) -> str:
209
+ """
210
+ Calculate and return a SHA-1 hash for the given arguments.
211
+
212
+ This function joins the provided arguments into a single string, encodes it
213
+ using UTF-16, and calculates the SHA-1 hash of the resulting bytes.
214
+
215
+ Args:
216
+ *args: A variable number of arguments to include in the hash.
217
+
218
+ Returns:
219
+ str: The computed SHA-1 hash as a hexadecimal string.
220
+ """
221
+ s = '_'.join(map(str, args))
222
+ return hashlib.sha1(s.encode("utf-16")).hexdigest()
223
+
224
+
225
+ def generate_random_password(n: int = 10) -> str:
226
+ """
227
+ Generates a random password meeting specific criteria for complexity. The
228
+ function ensures the password contains at least one lowercase letter, one
229
+ uppercase letter, and at least three numeric digits. The length of the
230
+ password can be customized using the 'n' parameter.
231
+
232
+ Parameters:
233
+ n (int): Length of the password to be generated. Default is 10.
234
+
235
+ Returns:
236
+ str: A randomly generated password that meets the specified criteria.
237
+ """
238
+ alphabet = string.ascii_letters + string.digits
239
+ while True:
240
+ password = "".join(secrets.choice(alphabet) for i in range(n))
241
+ if (
242
+ any(c.islower() for c in password)
243
+ and any(c.isupper() for c in password)
244
+ and sum(c.isdigit() for c in password) >= 3
245
+ ):
246
+ break
247
+ return password
@@ -0,0 +1,31 @@
1
+ from typing import Protocol, Sequence, Tuple, Union, TypeVar
2
+
3
+
4
+ class StringLike(Protocol):
5
+ """
6
+ Protocol that defines the expected behavior for string-like objects.
7
+
8
+ This protocol specifies the methods and properties that an object
9
+ must implement to be considered string-like. It defines basic
10
+ string operations such as getting the string representation and
11
+ length, string concatenation, containment checks, and several
12
+ commonly used string manipulation methods. Objects adhering to
13
+ this protocol can mimic the behavior of standard Python strings.
14
+ """
15
+ def __str__(self) -> str: ...
16
+ def __len__(self) -> int: ...
17
+ def __add__(self, other: str) -> str: ...
18
+ def __contains__(self, item: str) -> bool: ...
19
+
20
+ # Most commonly used string methods
21
+ def upper(self) -> str: ...
22
+ def lower(self) -> str: ...
23
+ def strip(self) -> str: ...
24
+ def startswith(self, prefix: str) -> bool: ...
25
+ def endswith(self, suffix: str) -> bool: ...
26
+ def replace(self, old: str, new: str) -> str: ...
27
+
28
+
29
+ ChoicesType = Sequence[Tuple[Union[int, str], StringLike]]
30
+
31
+ T = TypeVar('T')
@@ -1,131 +0,0 @@
1
- import base64
2
- import hashlib
3
- import itertools
4
- import random
5
- import re
6
- import secrets
7
- import string
8
- import unicodedata
9
-
10
- _control_chars = "".join(
11
- map(chr, itertools.chain(range(0x00, 0x20), range(0x7F, 0xA0))))
12
- _re_control_char = re.compile("[%s]" % re.escape(_control_chars))
13
- _re_combine_whitespace = re.compile(r"\s+")
14
-
15
-
16
- def slugify(value, allow_unicode=False):
17
- """
18
- Maps directly to Django's slugify function.
19
- Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
20
- dashes to single dashes. Remove characters that aren't alphanumerics,
21
- underscores, or hyphens. Convert to lowercase. Also strip leading and
22
- trailing whitespace, dashes, and underscores.
23
- """
24
- value = str(value)
25
- if allow_unicode:
26
- value = unicodedata.normalize("NFKC", value)
27
- else:
28
- value = (
29
- unicodedata.normalize("NFKD", value)
30
- .encode("ascii", "ignore")
31
- .decode("ascii")
32
- )
33
- value = re.sub(r"[^\w\s-]", "", value.lower())
34
- return re.sub(r"[-\s]+", "-", value).strip("-_")
35
-
36
-
37
- def random_16():
38
- return "".join(random.choices(string.ascii_letters + string.digits, k=16))
39
-
40
-
41
- def to_human_string(s):
42
- """
43
- This replaces all tabs and newlines with spaces and removes all non-printing
44
- control characters.
45
- """
46
- if not isinstance(s, str):
47
- return s, False
48
-
49
- c = _re_combine_whitespace.sub(" ", s).strip()
50
- clean_string = _re_control_char.sub("", c)
51
- if clean_string == s:
52
- return s, False
53
- return clean_string, True
54
-
55
-
56
- def is_quoted_string(s, strip=False):
57
- is_quoted = False
58
- result = s
59
- if not isinstance(s, str):
60
- return is_quoted, result
61
-
62
- if s[0] == s[-1]:
63
- if s[0] in ['"', "'"]:
64
- is_quoted = True
65
- if strip:
66
- if s[0] == "'":
67
- result = s.strip("'")
68
- elif s[0] == '"':
69
- result = s.strip('"')
70
- return is_quoted, result
71
-
72
-
73
- def is_numeric_string(s, convert=False):
74
- is_numeric = False
75
- result = s
76
- f = None
77
- if not isinstance(s, str):
78
- return is_numeric, result
79
- try:
80
- f = float(s)
81
- is_numeric = True
82
- except ValueError:
83
- is_numeric = False
84
-
85
- if is_numeric and convert:
86
- result = int(f) if f.is_integer() else f
87
-
88
- return is_numeric, result
89
-
90
-
91
- def custom_slug(s):
92
- # Remove all non-word characters (everything except numbers and letters)
93
- s = re.sub(r"[^\w\s]", "", s)
94
-
95
- # Replace all runs of whitespace with a single dash
96
- s = re.sub(r"\s+", "_", s)
97
-
98
- return s
99
-
100
-
101
- def b64_encode(s):
102
- return base64.b64encode(s).decode("utf-8").strip("=")
103
-
104
-
105
- def b64_decode(s):
106
- pad = "=" * (-len(s) % 4)
107
- return base64.b64decode(s + pad)
108
-
109
-
110
- def calc_hash(*args):
111
- """
112
- Calculate a hash over the set of arguments.
113
- This is useful to compare models against each other for equality
114
- if the assumption is that if some combination of fields are equal, then
115
- the models represent equal ideas.
116
- """
117
- s = '_'.join(map(str, args))
118
- return hashlib.sha1(s.encode("utf-16")).hexdigest()
119
-
120
-
121
- def generate_random_password(n=10):
122
- alphabet = string.ascii_letters + string.digits
123
- while True:
124
- password = "".join(secrets.choice(alphabet) for i in range(n))
125
- if (
126
- any(c.islower() for c in password)
127
- and any(c.isupper() for c in password)
128
- and sum(c.isdigit() for c in password) >= 3
129
- ):
130
- break
131
- return password