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