python-dictattr 0.0.3__tar.gz → 0.0.4__tar.gz
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.
- {python_dictattr-0.0.3 → python_dictattr-0.0.4}/PKG-INFO +1 -1
- {python_dictattr-0.0.3 → python_dictattr-0.0.4}/dictattr/__init__.py +30 -17
- {python_dictattr-0.0.3 → python_dictattr-0.0.4}/pyproject.toml +1 -1
- {python_dictattr-0.0.3 → python_dictattr-0.0.4}/LICENSE +0 -0
- {python_dictattr-0.0.3 → python_dictattr-0.0.4}/dictattr/py.typed +0 -0
- {python_dictattr-0.0.3 → python_dictattr-0.0.4}/readme.md +0 -0
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
from __future__ import annotations
|
|
5
5
|
|
|
6
6
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
7
|
-
__version__ = (0, 0,
|
|
7
|
+
__version__ = (0, 0, 4)
|
|
8
8
|
__all__ = [
|
|
9
|
-
"odict", "AttrDict", "MapAttr", "MuMapAttr",
|
|
10
|
-
"
|
|
9
|
+
"odict", "AttrDict", "MapAttr", "MuMapAttr", "DictAttr",
|
|
10
|
+
"ChainDictAttr", "UserDictAttr", "PriorityUserDictAttr",
|
|
11
11
|
]
|
|
12
12
|
|
|
13
13
|
from collections import UserDict
|
|
@@ -124,25 +124,41 @@ class ChainDictAttr(DictAttr[K, V | "ChainDictAttr"]):
|
|
|
124
124
|
return d
|
|
125
125
|
|
|
126
126
|
|
|
127
|
-
class UserDictAttr(UserDict
|
|
127
|
+
class UserDictAttr(UserDict):
|
|
128
|
+
|
|
129
|
+
def __getattr__(self, attr, /):
|
|
130
|
+
try:
|
|
131
|
+
return self[attr]
|
|
132
|
+
except KeyError as e:
|
|
133
|
+
raise AttributeError(attr) from e
|
|
134
|
+
|
|
135
|
+
def __getitem__(self, key, /):
|
|
136
|
+
d = super().__getitem__(key)
|
|
137
|
+
if isinstance(d, Mapping) and not isinstance(d, __class__): # type: ignore
|
|
138
|
+
return type(self)(d)
|
|
139
|
+
return d
|
|
140
|
+
|
|
141
|
+
def __repr__(self, /) -> str:
|
|
142
|
+
cls = type(self)
|
|
143
|
+
name = cls.__qualname__
|
|
144
|
+
if (module := cls.__module__) != "__main__":
|
|
145
|
+
name = f"{module}.{name}"
|
|
146
|
+
return f"{name}.of({self.data!r})"
|
|
128
147
|
|
|
129
148
|
@classmethod
|
|
130
149
|
def of(cls, m: Mapping, /) -> Self:
|
|
131
150
|
self = cls()
|
|
132
|
-
self.__dict__["data"] = m
|
|
151
|
+
self.__dict__["data"] = m
|
|
133
152
|
return self
|
|
134
153
|
|
|
154
|
+
|
|
155
|
+
class PriorityUserDictAttr(UserDictAttr):
|
|
156
|
+
|
|
135
157
|
def __delattr__(self, attr, /):
|
|
136
158
|
try:
|
|
137
159
|
del self[attr]
|
|
138
160
|
except KeyError:
|
|
139
|
-
|
|
140
|
-
super().__delattr__(attr)
|
|
141
|
-
except KeyError:
|
|
142
|
-
raise AttributeError(attr)
|
|
143
|
-
|
|
144
|
-
def __getattr__(self, attr, /):
|
|
145
|
-
return getattr(self.data, attr)
|
|
161
|
+
super().__delattr__(attr)
|
|
146
162
|
|
|
147
163
|
def __getattribute__(self, attr, /):
|
|
148
164
|
if attr in ("__dict__", "data") or attr == f"__{attr.strip('_')}__":
|
|
@@ -152,11 +168,8 @@ class UserDictAttr(UserDict[K, V]):
|
|
|
152
168
|
except KeyError:
|
|
153
169
|
return super().__getattribute__(attr)
|
|
154
170
|
|
|
155
|
-
def
|
|
156
|
-
|
|
157
|
-
if type(d) is dict:
|
|
158
|
-
return type(self)(d)
|
|
159
|
-
return d
|
|
171
|
+
def __getattr__(self, attr, /):
|
|
172
|
+
raise AttributeError(attr)
|
|
160
173
|
|
|
161
174
|
def __setattr__(self, attr, val, /):
|
|
162
175
|
if attr == "data" and "data" not in self.__dict__:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|