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.
@@ -1,255 +1,240 @@
1
- """This module contains the implementation of the `NamedTuple` class.
2
-
3
- `NamedTuple` class imitates the behavior of the _named tuple_.
4
-
5
- Examples:
6
- >>> from xloft import NamedTuple
7
- >>> nt = NamedTuple(x=10, y="Hello", _id="507c7f79bcf86cd7994f6c0e")
8
- >>> nt.x
9
- 10
10
- >>> nt.y
11
- Hello
12
- >>> nt._id
13
- 507c7f79bcf86cd7994f6c0e
14
- >>> nt.z
15
- KeyError
16
- >>> len(nt)
17
- 3
18
- >>> nt.keys()
19
- ["x", "y", "_id"]
20
- >>> nt.values()
21
- [10, "Hello", "507c7f79bcf86cd7994f6c0e"]
22
- >>> nt.has_key("x")
23
- True
24
- >>> nt.has_key("z")
25
- False
26
- >>> nt.has_value(10)
27
- True
28
- >>> nt.has_value([1, 2, 3])
29
- False
30
- >>> nt.get("x")
31
- 10
32
- >>> nt.get("z")
33
- None
34
- >>> d = nt.to_dict()
35
- >>> d["x"]
36
- 10
37
- >>> for key, val in nt.items():
38
- ... print(f"Key: {key}, Value: {val}")
39
- "Key: x, Value: 10"
40
- "Key: y, Value: Hello"
41
- "Key: _id, value: 507c7f79bcf86cd7994f6c0e"
42
- >>> nt.update("x", 20)
43
- >>> nt.x
44
- 20
45
- >>> nt.update("z", [1, 2, 3])
46
- KeyError
47
- >>> nt["z"] = [1, 2, 3]
48
- TypeError
49
- >>> nt.x = 20
50
- Error: AttributeDoesNotSetValue
51
- >>> del nt.x
52
- Error: AttributeCannotBeDelete
53
- """
54
-
55
- from typing import Any
56
-
57
- from xloft.errors import (
58
- AttributeCannotBeDelete,
59
- AttributeDoesNotSetValue,
60
- )
61
-
62
-
63
- class NamedTuple:
64
- """This class imitates the behavior of the _named tuple_."""
65
-
66
- def __init__(self, **kwargs: dict[str, Any]) -> None: # noqa: D107
67
- self.__dict__["_jWjSaNy1RbtQinsN_keys"] = []
68
- for name, value in kwargs.items():
69
- self.__dict__[name] = value
70
- self._jWjSaNy1RbtQinsN_keys.append(name)
71
-
72
- def __len__(self) -> int:
73
- """Get the number of elements.
74
-
75
- Examples:
76
- >>> from xloft import NamedTuple
77
- >>> nt = NamedTuple(x=10, y="Hello")
78
- >>> len(nt)
79
- 2
80
-
81
- Returns:
82
- The number of elements in the tuple.
83
- """
84
- return len(self._jWjSaNy1RbtQinsN_keys)
85
-
86
- def __getattr__(self, name: str) -> Any:
87
- """Getter.
88
-
89
- Examples:
90
- >>> from xloft import NamedTuple
91
- >>> nt = NamedTuple(x=10, y="Hello")
92
- >>> nt.x
93
- 10
94
-
95
- Args:
96
- name: Key name.
97
-
98
- Returns:
99
- Value of key.
100
- """
101
- return self.__dict__[name]
102
-
103
- def __setattr__(self, name: str, value: Any) -> None:
104
- """Blocked Setter."""
105
- raise AttributeDoesNotSetValue(name)
106
-
107
- def __delattr__(self, name: str) -> None:
108
- """Blocked Deleter."""
109
- raise AttributeCannotBeDelete(name)
110
-
111
- def get(self, key: str) -> Any:
112
- """Return the value for key if key is in the dictionary, else `None`.
113
-
114
- Args:
115
- key: Key name.
116
-
117
- Examples:
118
- >>> from xloft import NamedTuple
119
- >>> nt = NamedTuple(x=10, y="Hello")
120
- >>> nt.get("x")
121
- 10
122
-
123
- Returns:
124
- Value of key.
125
- """
126
- value = self.__dict__.get(key)
127
- if value is not None:
128
- return value
129
- return None
130
-
131
- def update(self, key: str, value: Any) -> None:
132
- """Update a value of key.
133
-
134
- Attention: This is an uncharacteristic action for the type `tuple`.
135
-
136
- Args:
137
- key: Key name.
138
- value: Value of key.
139
-
140
- Examples:
141
- >>> from xloft import NamedTuple
142
- >>> nt = NamedTuple(x=10, y="Hello")
143
- >>> nt.update("x", 20)
144
- >>> nt.x
145
- 20
146
-
147
- Returns:
148
- None
149
- """
150
- keys: list[str] = self._jWjSaNy1RbtQinsN_keys
151
- if key not in keys:
152
- err_msg = f"The key `{key}` is missing!"
153
- raise KeyError(err_msg)
154
- self.__dict__[key] = value
155
-
156
- def to_dict(self) -> dict[str, Any]:
157
- """Convert to the dictionary.
158
-
159
- Examples:
160
- >>> from xloft import NamedTuple
161
- >>> nt = NamedTuple(x=10, y="Hello")
162
- >>> d = nt.to_dict()
163
- >>> d["x"]
164
- 10
165
-
166
- Returns:
167
- Dictionary with keys and values of the tuple.
168
- """
169
- attrs: dict[str, Any] = self.__dict__
170
- keys: list[str] = self._jWjSaNy1RbtQinsN_keys
171
- return {key: attrs[key] for key in keys}
172
-
173
- def items(self) -> list[tuple[str, Any]]:
174
- """Return a set-like object providing a view on the NamedTuple's items.
175
-
176
- Examples:
177
- >>> from xloft import NamedTuple
178
- >>> nt = NamedTuple(x=10, y="Hello")
179
- >>> for key, val in nt.items():
180
- ... print(f"Key: {key}, Value: {val}")
181
- "Key: x, Value: 10"
182
- "Key: y, Value: Hello"
183
-
184
- Returns:
185
- list[tuple[str, Any]]
186
- """
187
- attrs: dict[str, Any] = self.__dict__
188
- keys: list[str] = self._jWjSaNy1RbtQinsN_keys
189
- return [(key, attrs[key]) for key in keys]
190
-
191
- def keys(self) -> list[str]:
192
- """Get a list of keys.
193
-
194
- Examples:
195
- >>> from xloft import NamedTuple
196
- >>> nt = NamedTuple(x=10, y="Hello")
197
- >>> nt.keys()
198
- ["x", "y"]
199
-
200
- Returns:
201
- List of keys.
202
- """
203
- return self._jWjSaNy1RbtQinsN_keys
204
-
205
- def values(self) -> list[Any]:
206
- """Get a list of values.
207
-
208
- Examples:
209
- >>> from xloft import NamedTuple
210
- >>> nt = NamedTuple(x=10, y="Hello")
211
- >>> nt.values()
212
- [10, "Hello"]
213
-
214
- Returns:
215
- List of values.
216
- """
217
- attrs: dict[str, Any] = self.__dict__
218
- keys: list[str] = self._jWjSaNy1RbtQinsN_keys
219
- return [attrs[key] for key in keys]
220
-
221
- def has_key(self, key: str) -> bool:
222
- """Returns True if the key exists, otherwise False.
223
-
224
- Args:
225
- key: Key name.
226
-
227
- Examples:
228
- >>> from xloft import NamedTuple
229
- >>> nt = NamedTuple(x=10, y="Hello")
230
- >>> nt.has_key("x")
231
- True
232
-
233
- Returns:
234
- True if the key exists, otherwise False.
235
- """
236
- keys: list[str] = self._jWjSaNy1RbtQinsN_keys
237
- return key in keys
238
-
239
- def has_value(self, value: Any) -> bool:
240
- """Returns True if the value exists, otherwise False.
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