lacus.utils 1.0.0__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.
- lacus/utils/__init__.py +11 -0
- lacus/utils/describe_type.py +150 -0
- lacus/utils/generate_random_sequence.py +40 -0
- lacus/utils/types.py +9 -0
- lacus_utils-1.0.0.dist-info/METADATA +151 -0
- lacus_utils-1.0.0.dist-info/RECORD +9 -0
- lacus_utils-1.0.0.dist-info/WHEEL +5 -0
- lacus_utils-1.0.0.dist-info/licenses/LICENSE +9 -0
- lacus_utils-1.0.0.dist-info/top_level.txt +1 -0
lacus/utils/__init__.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from collections.abc import Callable
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _describe_item(item: Any) -> str:
|
|
7
|
+
"""Return a type label for an element inside a list or tuple."""
|
|
8
|
+
if item is None:
|
|
9
|
+
return "NoneType"
|
|
10
|
+
|
|
11
|
+
if isinstance(item, bool):
|
|
12
|
+
return "boolean"
|
|
13
|
+
|
|
14
|
+
if isinstance(item, int):
|
|
15
|
+
return "number"
|
|
16
|
+
|
|
17
|
+
if isinstance(item, float):
|
|
18
|
+
if math.isnan(item):
|
|
19
|
+
return "NaN"
|
|
20
|
+
|
|
21
|
+
if math.isinf(item):
|
|
22
|
+
return "Infinity"
|
|
23
|
+
|
|
24
|
+
return "number"
|
|
25
|
+
|
|
26
|
+
if isinstance(item, str):
|
|
27
|
+
return "string"
|
|
28
|
+
|
|
29
|
+
return _describe_type(item)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _describe_sequence(
|
|
33
|
+
items: list[Any] | tuple[Any, ...],
|
|
34
|
+
empty_label: str,
|
|
35
|
+
suffix: str,
|
|
36
|
+
) -> str:
|
|
37
|
+
if not items:
|
|
38
|
+
return empty_label
|
|
39
|
+
|
|
40
|
+
unique_types: dict[str, None] = {}
|
|
41
|
+
|
|
42
|
+
for item in items:
|
|
43
|
+
label = _describe_item(item)
|
|
44
|
+
|
|
45
|
+
if label not in unique_types:
|
|
46
|
+
unique_types[label] = None
|
|
47
|
+
|
|
48
|
+
type_labels = list(unique_types.keys())
|
|
49
|
+
|
|
50
|
+
if len(type_labels) == 1:
|
|
51
|
+
return f"{type_labels[0]}{suffix}"
|
|
52
|
+
|
|
53
|
+
return f"({' | '.join(type_labels)}){suffix}"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _describe_type(value: Any) -> str:
|
|
57
|
+
if isinstance(value, list):
|
|
58
|
+
return _describe_sequence(value, "Array (empty)", "[]")
|
|
59
|
+
|
|
60
|
+
if isinstance(value, tuple):
|
|
61
|
+
return _describe_sequence(value, "tuple (empty)", " tuple")
|
|
62
|
+
|
|
63
|
+
if value is None:
|
|
64
|
+
return "NoneType"
|
|
65
|
+
|
|
66
|
+
if isinstance(value, bool):
|
|
67
|
+
return "boolean"
|
|
68
|
+
|
|
69
|
+
if isinstance(value, int):
|
|
70
|
+
return "integer number"
|
|
71
|
+
|
|
72
|
+
if isinstance(value, float):
|
|
73
|
+
if math.isnan(value):
|
|
74
|
+
return "NaN"
|
|
75
|
+
|
|
76
|
+
if math.isinf(value):
|
|
77
|
+
return "Infinity"
|
|
78
|
+
|
|
79
|
+
return "float number"
|
|
80
|
+
|
|
81
|
+
if isinstance(value, complex):
|
|
82
|
+
return "complex number"
|
|
83
|
+
|
|
84
|
+
if isinstance(value, str):
|
|
85
|
+
return "string"
|
|
86
|
+
|
|
87
|
+
if isinstance(value, dict):
|
|
88
|
+
return "dict"
|
|
89
|
+
|
|
90
|
+
if isinstance(value, set):
|
|
91
|
+
return "set"
|
|
92
|
+
|
|
93
|
+
if isinstance(value, frozenset):
|
|
94
|
+
return "frozenset"
|
|
95
|
+
|
|
96
|
+
if isinstance(value, bytes):
|
|
97
|
+
return "bytes"
|
|
98
|
+
|
|
99
|
+
if isinstance(value, bytearray):
|
|
100
|
+
return "bytearray"
|
|
101
|
+
|
|
102
|
+
if isinstance(value, type):
|
|
103
|
+
return "type"
|
|
104
|
+
|
|
105
|
+
if isinstance(value, Callable):
|
|
106
|
+
return "function"
|
|
107
|
+
|
|
108
|
+
return "object"
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def describe_type(value: Any) -> str:
|
|
112
|
+
"""Describe the type of a value for error messages.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
value: Any value to describe.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
A human-readable type label. For example, ``None`` returns ``"NoneType"``,
|
|
119
|
+
``42`` returns ``"integer number"``, ``[1, 2, 3]`` returns ``"number[]"``,
|
|
120
|
+
and ``(1, "a")`` returns ``"(number | string) tuple"``.
|
|
121
|
+
|
|
122
|
+
Examples:
|
|
123
|
+
>>> describe_type(None)
|
|
124
|
+
'NoneType'
|
|
125
|
+
>>> describe_type("hello")
|
|
126
|
+
'string'
|
|
127
|
+
>>> describe_type(True)
|
|
128
|
+
'boolean'
|
|
129
|
+
>>> describe_type(42)
|
|
130
|
+
'integer number'
|
|
131
|
+
>>> describe_type(3.14)
|
|
132
|
+
'float number'
|
|
133
|
+
>>> describe_type(float("nan"))
|
|
134
|
+
'NaN'
|
|
135
|
+
>>> describe_type(float("inf"))
|
|
136
|
+
'Infinity'
|
|
137
|
+
>>> describe_type([])
|
|
138
|
+
'Array (empty)'
|
|
139
|
+
>>> describe_type([1, 2, 3])
|
|
140
|
+
'number[]'
|
|
141
|
+
>>> describe_type([1, "a", 2])
|
|
142
|
+
'(number | string)[]'
|
|
143
|
+
>>> describe_type({})
|
|
144
|
+
'dict'
|
|
145
|
+
>>> describe_type(())
|
|
146
|
+
'tuple (empty)'
|
|
147
|
+
>>> describe_type((1, 2))
|
|
148
|
+
'number tuple'
|
|
149
|
+
"""
|
|
150
|
+
return _describe_type(value)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import secrets
|
|
2
|
+
|
|
3
|
+
from .types import SequenceType
|
|
4
|
+
|
|
5
|
+
_NUMERIC = "0123456789"
|
|
6
|
+
_ALPHABETIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
7
|
+
_ALPHANUMERIC = _NUMERIC + _ALPHABETIC
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def generate_random_sequence(size: int, sequence_type: SequenceType) -> str:
|
|
11
|
+
"""Generate a random character sequence of the given length and type.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
size: Length of the sequence.
|
|
15
|
+
sequence_type: Character set to draw from. One of ``"numeric"`` (``0-9``),
|
|
16
|
+
``"alphabetic"`` (``A-Z``), or ``"alphanumeric"`` (``0-9A-Z``).
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
A random string of the requested length using uppercase letters and/or
|
|
20
|
+
digits, depending on ``sequence_type``.
|
|
21
|
+
|
|
22
|
+
Examples:
|
|
23
|
+
>>> generate_random_sequence(10, "numeric") # doctest: +SKIP
|
|
24
|
+
'9956000611'
|
|
25
|
+
>>> generate_random_sequence(6, "alphabetic") # doctest: +SKIP
|
|
26
|
+
'AXQMZB'
|
|
27
|
+
>>> generate_random_sequence(8, "alphanumeric") # doctest: +SKIP
|
|
28
|
+
'8ZFB2K09'
|
|
29
|
+
"""
|
|
30
|
+
if size < 0:
|
|
31
|
+
raise ValueError(f"size must be non-negative, got {size}")
|
|
32
|
+
|
|
33
|
+
if sequence_type == "numeric":
|
|
34
|
+
chars = _NUMERIC
|
|
35
|
+
elif sequence_type == "alphabetic":
|
|
36
|
+
chars = _ALPHABETIC
|
|
37
|
+
else:
|
|
38
|
+
chars = _ALPHANUMERIC
|
|
39
|
+
|
|
40
|
+
return "".join(secrets.choice(chars) for _ in range(size))
|
lacus/utils/types.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
|
|
3
|
+
SequenceType = Literal["alphabetic", "alphanumeric", "numeric"]
|
|
4
|
+
"""Character type for random sequence generation.
|
|
5
|
+
|
|
6
|
+
- ``"alphanumeric"``: digits and uppercase letters (``0-9A-Z``).
|
|
7
|
+
- ``"numeric"``: digits only (``0-9``).
|
|
8
|
+
- ``"alphabetic"``: uppercase letters only (``A-Z``).
|
|
9
|
+
"""
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lacus.utils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Reusable utilities for Lacus Solutions Python packages
|
|
5
|
+
Author: Julio L. Muller
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/LacusSolutions/br-utils-py
|
|
8
|
+
Project-URL: Source, https://github.com/LacusSolutions/br-utils-py
|
|
9
|
+
Project-URL: Tracker, https://github.com/LacusSolutions/br-utils-py/issues
|
|
10
|
+
Keywords: utils,utilities,helpers,random,sequence,type
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Natural Language :: English
|
|
14
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
15
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
16
|
+
Classifier: Operating System :: POSIX
|
|
17
|
+
Classifier: Operating System :: Unix
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
25
|
+
Classifier: Topic :: Utilities
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# Lacus Solutions' Utils
|
|
32
|
+
|
|
33
|
+
[](https://pypi.org/project/lacus.utils)
|
|
34
|
+
[](https://pypi.org/project/lacus.utils)
|
|
35
|
+
[](https://www.python.org/)
|
|
36
|
+
[](https://github.com/LacusSolutions/br-utils-py/actions)
|
|
37
|
+
[](https://github.com/LacusSolutions/br-utils-py)
|
|
38
|
+
[](https://github.com/LacusSolutions/br-utils-py/blob/main/LICENSE)
|
|
39
|
+
|
|
40
|
+
Reusable utilities library for Lacus Solutions' Python packages.
|
|
41
|
+
|
|
42
|
+
## Python Support
|
|
43
|
+
|
|
44
|
+
|  |  |  |  |  |
|
|
45
|
+
|--- | --- | --- | --- | --- |
|
|
46
|
+
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- **Type description**: Python-native type labels for error messages (`NoneType`, `dict`, `tuple`, built-ins, lists)
|
|
51
|
+
- **Random sequences**: Generate numeric, alphabetic, or alphanumeric sequences of any length
|
|
52
|
+
- **Zero dependencies**: No external runtime packages required
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
$ pip install lacus.utils
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Import
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from lacus.utils import describe_type, generate_random_sequence, SequenceType
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Quick Start
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
describe_type(None) # 'NoneType'
|
|
70
|
+
describe_type("hello") # 'string'
|
|
71
|
+
describe_type(42) # 'integer number'
|
|
72
|
+
describe_type(3.14) # 'float number'
|
|
73
|
+
describe_type(float("nan")) # 'NaN'
|
|
74
|
+
describe_type({}) # 'dict'
|
|
75
|
+
describe_type([1, 2, 3]) # 'number[]'
|
|
76
|
+
describe_type([1, "a", 2]) # '(number | string)[]'
|
|
77
|
+
describe_type((1, 2)) # 'number tuple'
|
|
78
|
+
|
|
79
|
+
generate_random_sequence(10, "numeric") # e.g. '9956000611'
|
|
80
|
+
generate_random_sequence(6, "alphabetic") # e.g. 'AXQMZB'
|
|
81
|
+
generate_random_sequence(8, "alphanumeric") # e.g. '8ZFB2K09'
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## API
|
|
85
|
+
|
|
86
|
+
All functions are implemented in [`src/lacus/utils/`](src/lacus/utils/) and covered by tests in [`tests/`](tests/).
|
|
87
|
+
|
|
88
|
+
### `describe_type(value) -> str`
|
|
89
|
+
|
|
90
|
+
Describes the type of a value for error messages.
|
|
91
|
+
|
|
92
|
+
| Input | Result |
|
|
93
|
+
|--------|--------|
|
|
94
|
+
| `None` | `'NoneType'` |
|
|
95
|
+
| `str` | `'string'` |
|
|
96
|
+
| `bool` | `'boolean'` |
|
|
97
|
+
| `int` | `'integer number'` |
|
|
98
|
+
| `float` (finite) | `'float number'` |
|
|
99
|
+
| `float('nan')` | `'NaN'` |
|
|
100
|
+
| `float('inf')` / `float('-inf')` | `'Infinity'` |
|
|
101
|
+
| `complex` | `'complex number'` |
|
|
102
|
+
| `dict` | `'dict'` |
|
|
103
|
+
| `set` / `frozenset` | `'set'` / `'frozenset'` |
|
|
104
|
+
| `bytes` / `bytearray` | `'bytes'` / `'bytearray'` |
|
|
105
|
+
| callable | `'function'` |
|
|
106
|
+
| class (`int`, `str`, …) | `'type'` |
|
|
107
|
+
| custom class instance | `'object'` |
|
|
108
|
+
| `[]` | `'Array (empty)'` |
|
|
109
|
+
| `[1, 2, 3]` | `'number[]'` |
|
|
110
|
+
| `[1, 'a', 2]` | `'(number | string)[]'` |
|
|
111
|
+
| `()` | `'tuple (empty)'` |
|
|
112
|
+
| `(1, 2)` | `'number tuple'` |
|
|
113
|
+
|
|
114
|
+
### `generate_random_sequence(size: int, sequence_type: SequenceType) -> str`
|
|
115
|
+
|
|
116
|
+
Generates a random character sequence of the given length and type.
|
|
117
|
+
|
|
118
|
+
- **`size`**: Length of the sequence (e.g. `10`).
|
|
119
|
+
- **`sequence_type`**: One of:
|
|
120
|
+
- **`'numeric'`**: digits `0-9`
|
|
121
|
+
- **`'alphabetic'`**: uppercase letters `A-Z`
|
|
122
|
+
- **`'alphanumeric'`**: digits and uppercase letters `0-9A-Z`
|
|
123
|
+
|
|
124
|
+
### Exports summary
|
|
125
|
+
|
|
126
|
+
| Export | Description |
|
|
127
|
+
|--------|-------------|
|
|
128
|
+
| `describe_type` | Type description for error messages |
|
|
129
|
+
| `generate_random_sequence` | Random sequence generation |
|
|
130
|
+
| `SequenceType` | Literal type: `'alphabetic' \| 'alphanumeric' \| 'numeric'` |
|
|
131
|
+
|
|
132
|
+
## Contribution & Support
|
|
133
|
+
|
|
134
|
+
We welcome contributions! Please see our [Contributing Guidelines](https://github.com/LacusSolutions/br-utils-py/blob/main/CONTRIBUTING.md) for details. If you find this project helpful, please consider:
|
|
135
|
+
|
|
136
|
+
- Starring the repository
|
|
137
|
+
- Contributing to the codebase
|
|
138
|
+
- [Suggesting new features](https://github.com/LacusSolutions/br-utils-py/issues)
|
|
139
|
+
- [Reporting bugs](https://github.com/LacusSolutions/br-utils-py/issues)
|
|
140
|
+
|
|
141
|
+
## License
|
|
142
|
+
|
|
143
|
+
This project is licensed under the MIT License - see the [LICENSE](https://github.com/LacusSolutions/br-utils-py/blob/main/LICENSE) file for details.
|
|
144
|
+
|
|
145
|
+
## Changelog
|
|
146
|
+
|
|
147
|
+
See [CHANGELOG](./CHANGELOG.md) for a list of changes and version history.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
Made with ❤️ by [Lacus Solutions](https://github.com/LacusSolutions)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
lacus/utils/__init__.py,sha256=sj-DwiPX_PiXur1AY5Ml9_9ShJmq4FuzhM8ZRdvddc8,247
|
|
2
|
+
lacus/utils/describe_type.py,sha256=4DSLuwQr583vY9JDTK3kBffCtipWgf1Fys9KlqAxjho,3414
|
|
3
|
+
lacus/utils/generate_random_sequence.py,sha256=pXp-BSIyyuCgFGwEf8tR3agqVyiUZ3QRQhEtCkyOdFg,1270
|
|
4
|
+
lacus/utils/types.py,sha256=VGlc6TeuPCPG-NNN0yZGGWaW-MHHWCg5eFHr6S0gLso,306
|
|
5
|
+
lacus_utils-1.0.0.dist-info/licenses/LICENSE,sha256=Ul6ngVvCBnV8xCk721l1FrGjZWMR6NKP8BTKOiCKNio,1072
|
|
6
|
+
lacus_utils-1.0.0.dist-info/METADATA,sha256=yZjC0R7d-_El2EoavdGOuD6BpbpvzZYXE6PjBDRi_gQ,6109
|
|
7
|
+
lacus_utils-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
8
|
+
lacus_utils-1.0.0.dist-info/top_level.txt,sha256=jyR9rnFUDV4mjHwJG6gkGAUI5LBKynvDIGXBoenli7o,6
|
|
9
|
+
lacus_utils-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Julio L. Muller
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
lacus
|