xloft 0.1.5__py3-none-any.whl → 0.1.7__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.

Potentially problematic release.


This version of xloft might be problematic. Click here for more details.

xloft/namedtuple.py CHANGED
@@ -12,8 +12,16 @@ class NamedTuple:
12
12
  """Named Tuple."""
13
13
 
14
14
  def __init__(self, **kwargs: dict[str, Any]) -> None: # noqa: D107
15
+ self.__dict__["_keys"] = []
15
16
  for name, value in kwargs.items():
16
17
  self.__dict__[name] = value
18
+ self._keys.append(name)
19
+ else:
20
+ self.__dict__["_len"] = len(self._keys)
21
+
22
+ def __len__(self) -> int:
23
+ """Get the number of elements."""
24
+ return self._len
17
25
 
18
26
  def __getattr__(self, name: str) -> Any:
19
27
  """Getter."""
@@ -27,24 +35,47 @@ class NamedTuple:
27
35
  """Fail Deleter."""
28
36
  raise AttributeCannotBeDelete(name)
29
37
 
30
- def get(self, key: str, default: Any | None = None) -> Any | None:
31
- """Return the value for key if key is in the dictionary, else default."""
38
+ def get(self, key: str) -> Any:
39
+ """Return the value for key if key is in the dictionary, else `None`."""
32
40
  value = self.__dict__.get(key)
33
41
  if value is not None:
34
42
  return value
35
- return default
43
+ return None
36
44
 
37
45
  def update(self, key: str, value: Any) -> Any:
38
46
  """Update a value of key.
39
47
 
40
48
  Attention: This is an uncharacteristic action for the type `tuple`.
41
49
  """
50
+ if not key in self._keys:
51
+ err_msg = f"The key `{key}` is missing!"
52
+ raise KeyError(err_msg)
42
53
  self.__dict__[key] = value
43
54
 
44
55
  def to_dict(self) -> dict[str, Any]:
45
56
  """Convert to the dictionary."""
46
- return {key: value for key, value in self.__dict__.items() if not callable(value)}
57
+ return {
58
+ key: value
59
+ for key, value in self.__dict__.items()
60
+ if not callable(value) and not key in ["_keys", "_len"]
61
+ }
47
62
 
48
63
  def items(self) -> list[tuple[str, Any]]:
49
64
  """Return a set-like object providing a view on the NamedTuple's items."""
50
- return [(key, value) for key, value in self.__dict__.items() if not callable(value)]
65
+ return [
66
+ (key, value)
67
+ for key, value in self.__dict__.items()
68
+ if not callable(value) and not key in ["_keys", "_len"]
69
+ ]
70
+
71
+ def keys(self) -> list[str]:
72
+ """Get a list of keys."""
73
+ return self._keys
74
+
75
+ def values(self) -> list[Any]:
76
+ """Get a list of values."""
77
+ return [
78
+ value
79
+ for key, value in self.__dict__.items()
80
+ if not callable(value) and not key in ["_keys", "_len"]
81
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xloft
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: (XLOFT) X-Library of tools
5
5
  Project-URL: Homepage, https://github.com/kebasyaty/xloft
6
6
  Project-URL: Repository, https://github.com/kebasyaty/xloft
@@ -91,13 +91,18 @@ nt.x # => 10
91
91
  nt.y # => "Hello"
92
92
  nt.z # => raise: KeyError
93
93
 
94
+ len(nt) # => 2
95
+ nt.keys() # => ["x", "y"]
96
+ nt.values() # => [10, "Hello"]
97
+
94
98
  nt["x"] # => raise: KeyError
95
99
  nt["y"] # => raise: KeyError
100
+ nt["x"] = 20 # => TypeError
101
+ nt["y"] = "Hi" # => TypeError
96
102
 
97
103
  nt.get("x") # => 10
98
104
  nt.get("y") # => "Hello"
99
105
  nt.get("z") # => None
100
- nt.get("z", [1, 2, 3] ) # => [1, 2, 3]
101
106
 
102
107
  nt.x = 20 # => raise: AttributeDoesNotSetValue
103
108
  nt.y = "Hi" # => raise: AttributeDoesNotSetValue
@@ -107,8 +112,7 @@ nt.update("x", 20)
107
112
  nt.update("y", "Hi")
108
113
  nt.x # => 20
109
114
  nt.y # => "Hi"
110
- nt.update("z", [1, 2, 3])
111
- nt.z # => [1, 2, 3]
115
+ nt.update("z", [1, 2, 3]) # => raise: KeyError
112
116
 
113
117
  d = nt.to_dict()
114
118
  d["x"] # => 10
@@ -0,0 +1,8 @@
1
+ xloft/__init__.py,sha256=yV847dKh9ROtw1fX5NHvPaHN-9cHYOfu9URQ--OVKXw,107
2
+ xloft/errors.py,sha256=GYXvi2l01VUDQSs6skiOfQsKLF6tFuUhJMqNkL7BJNI,857
3
+ xloft/namedtuple.py,sha256=h_1UEKR7A0WD-WUGNQBVBTGwcpRmLTyQ8czl-fVmmP4,2452
4
+ xloft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ xloft-0.1.7.dist-info/METADATA,sha256=N7LrkeK_XCDXuSF5a3mIuLK3kIDum4jGDw_rQg7CNaA,5430
6
+ xloft-0.1.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ xloft-0.1.7.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
8
+ xloft-0.1.7.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- xloft/__init__.py,sha256=yV847dKh9ROtw1fX5NHvPaHN-9cHYOfu9URQ--OVKXw,107
2
- xloft/errors.py,sha256=GYXvi2l01VUDQSs6skiOfQsKLF6tFuUhJMqNkL7BJNI,857
3
- xloft/namedtuple.py,sha256=05XP8vgA9_AuSyQyx2G4Wbbf2ZiPffNBjtB-w_VDAe0,1612
4
- xloft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- xloft-0.1.5.dist-info/METADATA,sha256=i5pYt6789F3R_SS-5oHra7G_0QQWXvwIN58CeMKAgzo,5333
6
- xloft-0.1.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- xloft-0.1.5.dist-info/licenses/LICENSE,sha256=2zZINd6m_jNYlowdQImlEizyhSui5cBAJZRhWQURcEc,1095
8
- xloft-0.1.5.dist-info/RECORD,,
File without changes