xloft 0.1.19__py3-none-any.whl → 0.10.10__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.
- xloft/__init__.py +35 -14
- xloft/converters/__init__.py +22 -0
- xloft/converters/human_size.py +42 -0
- xloft/converters/roman.py +119 -0
- xloft/errors.py +43 -24
- xloft/itis.py +63 -0
- xloft/types/__init__.py +20 -0
- xloft/types/alias_dict.py +359 -0
- xloft/{namedtuple.py → types/named_tuple.py} +240 -255
- xloft-0.10.10.dist-info/METADATA +281 -0
- xloft-0.10.10.dist-info/RECORD +14 -0
- {xloft-0.1.19.dist-info → xloft-0.10.10.dist-info}/WHEEL +1 -1
- {xloft-0.1.19.dist-info → xloft-0.10.10.dist-info}/licenses/LICENSE +21 -21
- xloft/humanism.py +0 -30
- xloft-0.1.19.dist-info/METADATA +0 -175
- xloft-0.1.19.dist-info/RECORD +0 -9
|
@@ -1,255 +1,240 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
from
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def
|
|
73
|
-
"""
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
"""
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
"""
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
>>>
|
|
144
|
-
>>> nt
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
>>>
|
|
162
|
-
>>>
|
|
163
|
-
>>>
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
>>>
|
|
197
|
-
>>> nt
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
True
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
Args:
|
|
243
|
-
value: Value of key.
|
|
244
|
-
|
|
245
|
-
Examples:
|
|
246
|
-
>>> from xloft import NamedTuple
|
|
247
|
-
>>> nt = NamedTuple(x=10, y="Hello")
|
|
248
|
-
>>> nt.has_value(10)
|
|
249
|
-
True
|
|
250
|
-
|
|
251
|
-
Returns:
|
|
252
|
-
True if the value exists, otherwise False.
|
|
253
|
-
"""
|
|
254
|
-
values = self.values()
|
|
255
|
-
return value in values
|
|
1
|
+
# XLOFT - X-Library of tools.
|
|
2
|
+
# Copyright (c) 2025 Gennady Kostyunin
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
"""`NamedTuple` - Imitates the behavior of the *named tuple*.
|
|
5
|
+
|
|
6
|
+
Class `NamedTuple` contains the following methods:
|
|
7
|
+
|
|
8
|
+
- `get` - Return the value for key if key is in the dictionary, else `None`.
|
|
9
|
+
- `update` - Update a value of key.
|
|
10
|
+
- `to_dict` - Convert to the dictionary.
|
|
11
|
+
- `items` - Returns a generator of list of `NamedTuple` elements grouped into tuples.
|
|
12
|
+
- `keys` - Get a generator of list of keys.
|
|
13
|
+
- `values` - Get a generator of list of values.
|
|
14
|
+
- `has_key` - Returns True if the key exists, otherwise False.
|
|
15
|
+
- `has_value` - Returns True if the value exists, otherwise False.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
__all__ = ("NamedTuple",)
|
|
21
|
+
|
|
22
|
+
import copy
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
from xloft.errors import (
|
|
26
|
+
AttributeCannotBeDeleteError,
|
|
27
|
+
AttributeDoesNotSetValueError,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class NamedTuple:
|
|
32
|
+
"""This class imitates the behavior of the `named tuple`."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, **kwargs: dict[str, Any]) -> None: # noqa: D107
|
|
35
|
+
self.__dict__["_store"] = kwargs
|
|
36
|
+
|
|
37
|
+
def __len__(self) -> int:
|
|
38
|
+
"""Get the number of elements in the tuple.
|
|
39
|
+
|
|
40
|
+
Examples:
|
|
41
|
+
>>> from xloft import NamedTuple
|
|
42
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
43
|
+
>>> len(nt)
|
|
44
|
+
2
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
The number of elements in the tuple.
|
|
48
|
+
"""
|
|
49
|
+
return len(self._store)
|
|
50
|
+
|
|
51
|
+
def __getattr__(self, name: str) -> Any:
|
|
52
|
+
"""Getter.
|
|
53
|
+
|
|
54
|
+
Examples:
|
|
55
|
+
>>> from xloft import NamedTuple
|
|
56
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
57
|
+
>>> nt.x
|
|
58
|
+
10
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
name: Key name.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
Value of key.
|
|
65
|
+
"""
|
|
66
|
+
return self._store[name]
|
|
67
|
+
|
|
68
|
+
def __setattr__(self, name: str, value: Any) -> None:
|
|
69
|
+
"""Blocked Setter."""
|
|
70
|
+
raise AttributeDoesNotSetValueError(name)
|
|
71
|
+
|
|
72
|
+
def __delattr__(self, name: str) -> None:
|
|
73
|
+
"""Blocked Deleter."""
|
|
74
|
+
raise AttributeCannotBeDeleteError(name)
|
|
75
|
+
|
|
76
|
+
def __getitem__(self, key: str) -> Any:
|
|
77
|
+
"""Get value by [key_name].
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
key: Key name.
|
|
81
|
+
|
|
82
|
+
Examples:
|
|
83
|
+
>>> from xloft import NamedTuple
|
|
84
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
85
|
+
>>> nt["x"]
|
|
86
|
+
10
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
Deep copy of the value associated with the key.
|
|
90
|
+
"""
|
|
91
|
+
value = self._store[key]
|
|
92
|
+
return copy.deepcopy(value)
|
|
93
|
+
|
|
94
|
+
def get(self, key: str, default: Any = None) -> Any:
|
|
95
|
+
"""Return the value for key if key is in the dictionary, else `None`.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
key: Key name.
|
|
99
|
+
|
|
100
|
+
Examples:
|
|
101
|
+
>>> from xloft import NamedTuple
|
|
102
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
103
|
+
>>> nt.get("x")
|
|
104
|
+
10
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
Deep copy of the value associated with the alias or value by default.
|
|
108
|
+
"""
|
|
109
|
+
value = self._store.get(key)
|
|
110
|
+
if value is not None:
|
|
111
|
+
return copy.deepcopy(value)
|
|
112
|
+
return default
|
|
113
|
+
|
|
114
|
+
def update(self, key: str, value: Any) -> None:
|
|
115
|
+
"""Update a value of key.
|
|
116
|
+
|
|
117
|
+
Attention: This is an uncharacteristic action for the type `tuple`.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
key: Key name.
|
|
121
|
+
value: Value of key.
|
|
122
|
+
|
|
123
|
+
Examples:
|
|
124
|
+
>>> from xloft import NamedTuple
|
|
125
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
126
|
+
>>> nt.update("x", 20)
|
|
127
|
+
>>> nt.x
|
|
128
|
+
20
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
None
|
|
132
|
+
"""
|
|
133
|
+
keys: list[str] = self._store.keys()
|
|
134
|
+
if key not in keys:
|
|
135
|
+
err_msg = f"The key `{key}` is missing!"
|
|
136
|
+
raise KeyError(err_msg)
|
|
137
|
+
self._store[key] = value
|
|
138
|
+
|
|
139
|
+
def to_dict(self) -> dict[str, Any]:
|
|
140
|
+
"""Convert to the dictionary.
|
|
141
|
+
|
|
142
|
+
Examples:
|
|
143
|
+
>>> from xloft import NamedTuple
|
|
144
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
145
|
+
>>> d = nt.to_dict()
|
|
146
|
+
>>> d["x"]
|
|
147
|
+
10
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
Dictionary with keys and values of the tuple.
|
|
151
|
+
"""
|
|
152
|
+
return dict(self._store)
|
|
153
|
+
|
|
154
|
+
def items(self) -> Any:
|
|
155
|
+
"""Returns a generator of list containing a tuple for each key-value pair.
|
|
156
|
+
|
|
157
|
+
This is convenient for use in a `for` loop.
|
|
158
|
+
If you need to get a list, do it list(instance.items()).
|
|
159
|
+
|
|
160
|
+
Examples:
|
|
161
|
+
>>> from xloft import NamedTuple
|
|
162
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
163
|
+
>>> for key, val in nt.items():
|
|
164
|
+
... print(f"Key: {key}, Value: {val}")
|
|
165
|
+
"Key: x, Value: 10"
|
|
166
|
+
"Key: y, Value: Hello"
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
Returns a list containing a tuple for each key-value pair.
|
|
170
|
+
Type: `list[tuple[str, Any]]`.
|
|
171
|
+
"""
|
|
172
|
+
return self._store.items()
|
|
173
|
+
|
|
174
|
+
def keys(self) -> Any:
|
|
175
|
+
"""Get a generator of list of keys.
|
|
176
|
+
|
|
177
|
+
If you need to get a list, do it list(instance.items()).
|
|
178
|
+
|
|
179
|
+
Examples:
|
|
180
|
+
>>> from xloft import NamedTuple
|
|
181
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
182
|
+
>>> list(nt.keys())
|
|
183
|
+
["x", "y"]
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
List of keys.
|
|
187
|
+
"""
|
|
188
|
+
return self._store.keys()
|
|
189
|
+
|
|
190
|
+
def values(self) -> Any:
|
|
191
|
+
"""Get a generator of list of values.
|
|
192
|
+
|
|
193
|
+
If you need to get a list, do it list(instance.items()).
|
|
194
|
+
|
|
195
|
+
Examples:
|
|
196
|
+
>>> from xloft import NamedTuple
|
|
197
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
198
|
+
>>> list(nt.values())
|
|
199
|
+
[10, "Hello"]
|
|
200
|
+
|
|
201
|
+
Returns:
|
|
202
|
+
List of values.
|
|
203
|
+
"""
|
|
204
|
+
return self._store.values()
|
|
205
|
+
|
|
206
|
+
def has_key(self, key: str) -> bool:
|
|
207
|
+
"""Check if the key exists.
|
|
208
|
+
|
|
209
|
+
Args:
|
|
210
|
+
key: Key name.
|
|
211
|
+
|
|
212
|
+
Examples:
|
|
213
|
+
>>> from xloft import NamedTuple
|
|
214
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
215
|
+
>>> nt.has_key("x")
|
|
216
|
+
True
|
|
217
|
+
|
|
218
|
+
Returns:
|
|
219
|
+
True if the key exists, otherwise False.
|
|
220
|
+
"""
|
|
221
|
+
keys = self._store.keys()
|
|
222
|
+
return key in keys
|
|
223
|
+
|
|
224
|
+
def has_value(self, value: Any) -> bool:
|
|
225
|
+
"""Check if the value exists.
|
|
226
|
+
|
|
227
|
+
Args:
|
|
228
|
+
value: Value of key.
|
|
229
|
+
|
|
230
|
+
Examples:
|
|
231
|
+
>>> from xloft import NamedTuple
|
|
232
|
+
>>> nt = NamedTuple(x=10, y="Hello")
|
|
233
|
+
>>> nt.has_value(10)
|
|
234
|
+
True
|
|
235
|
+
|
|
236
|
+
Returns:
|
|
237
|
+
True if the value exists, otherwise False.
|
|
238
|
+
"""
|
|
239
|
+
values = self._store.values()
|
|
240
|
+
return value in values
|