python-dictattr 0.0.1__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.
LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
dictattr/.DS_Store ADDED
Binary file
dictattr/__init__.py ADDED
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env python3
2
+ # encoding: utf-8
3
+
4
+ __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
+ __version__ = (0, 0, 1)
6
+ __all__ = [
7
+ "odict", "AttrDict", "MapAttr", "MuMapAttr", "DictAttr", "ChainDictAttr",
8
+ ]
9
+
10
+ from collections.abc import Iterator, Mapping, MutableMapping
11
+ from typing import Generic, Self, TypeVar
12
+
13
+
14
+ K = TypeVar("K")
15
+ V = TypeVar("V")
16
+
17
+
18
+ class odict(dict[K, V]):
19
+
20
+ __getattr__ = dict.__getitem__
21
+ __setattr__ = dict.__setitem__ # type: ignore
22
+ __delattr__ = dict.__delitem__ # type: ignore
23
+
24
+
25
+ class AttrDict(dict[K, V]):
26
+
27
+ def __init__(self, /, *args, **kwds):
28
+ super().__init__(*args, **kwds)
29
+ self.__dict__ = self # type: ignore
30
+
31
+
32
+ @Mapping.register
33
+ class MapAttr(Generic[K, V]):
34
+
35
+ def __init__(self, d: None | dict = None, /):
36
+ self.__dict__: dict[K, V] # type: ignore
37
+ if d is not None:
38
+ self.__dict__ = d
39
+
40
+ def __contains__(self, key, /) -> bool:
41
+ return key in self.__dict__
42
+
43
+ def __getitem__(self, key, /) -> V:
44
+ return self.__dict__[key]
45
+
46
+ def __iter__(self, /) -> Iterator[K]:
47
+ return iter(self.__dict__)
48
+
49
+ def __len__(self, /) -> int:
50
+ return len(self.__dict__)
51
+
52
+ def __repr__(self, /) -> str:
53
+ cls = type(self)
54
+ if (mod := cls.__module__) == "__main__":
55
+ return f"{cls.__qualname__}({self.__dict__})"
56
+ else:
57
+ return f"{mod}.{cls.__qualname__}({self.__dict__})"
58
+
59
+ @classmethod
60
+ def of(cls, /, *args, **kwds) -> Self:
61
+ return cls(dict(*args, **kwds))
62
+
63
+
64
+ @MutableMapping.register
65
+ class MuMapAttr(MapAttr[K, V]):
66
+
67
+ def __delitem__(self, key, /):
68
+ del self.__dict__[key]
69
+
70
+ def __setitem__(self, key: K, val: V, /):
71
+ self.__dict__[key] = val
72
+
73
+
74
+ class DictAttr(MuMapAttr):
75
+
76
+ def __getattr__(self, attr, /):
77
+ return getattr(self.__dict__, attr)
78
+
79
+ def __getattribute__(self, attr, /):
80
+ if attr is "__dict__":
81
+ return super().__getattribute__(attr)
82
+ try:
83
+ return self[attr]
84
+ except KeyError:
85
+ return super().__getattribute__(attr)
86
+
87
+ def __getitem__(self, key, /):
88
+ d = self.__dict__[key]
89
+ if type(d) is dict:
90
+ return type(self)(d)
91
+ return d
92
+
93
+
94
+ class ChainDictAttr(DictAttr):
95
+
96
+ def __getitem__(self, key, /):
97
+ try:
98
+ super().__getitem__(key)
99
+ except KeyError:
100
+ d = type(self)()
101
+ self.__dict__[key] = d.__dict__
102
+ return d
103
+
dictattr/py.typed ADDED
File without changes
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,41 @@
1
+ Metadata-Version: 2.1
2
+ Name: python-dictattr
3
+ Version: 0.0.1
4
+ Summary: Python dictattr.
5
+ Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-dictattr
6
+ License: MIT
7
+ Keywords: dictattr,attrdict
8
+ Author: ChenyangGao
9
+ Author-email: wosiwujm@gmail.com
10
+ Requires-Python: >=3.11,<4.0
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Topic :: Software Development
21
+ Classifier: Topic :: Software Development :: Libraries
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-dictattr
24
+ Description-Content-Type: text/markdown
25
+
26
+ # Python dictattr.
27
+
28
+ ## Installation
29
+
30
+ You can install from [pypi](https://pypi.org/project/python-dictattr/)
31
+
32
+ ```console
33
+ pip install -U python-dictattr
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ ```python
39
+ import dictattr
40
+ ```
41
+
@@ -0,0 +1,8 @@
1
+ LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
+ dictattr/.DS_Store,sha256=1lFlJ5EFymdzGAUAaI30vcaaLHt3F1LwpG7xILf9jsM,6148
3
+ dictattr/__init__.py,sha256=AQRbZxZxznS-3ApmWlwGjVZ6wbVuumlPPfr0xyUzVyk,2475
4
+ dictattr/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ python_dictattr-0.0.1.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
6
+ python_dictattr-0.0.1.dist-info/METADATA,sha256=ofprxePOcwsttqMRXXM_gHmKqgv9Fr6uhOH-rW3b29A,1273
7
+ python_dictattr-0.0.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
8
+ python_dictattr-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.8.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any