python-dictattr 0.0.1__tar.gz → 0.0.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-dictattr
3
- Version: 0.0.1
3
+ Version: 0.0.2
4
4
  Summary: Python dictattr.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-dictattr
6
6
  License: MIT
@@ -1,12 +1,16 @@
1
1
  #!/usr/bin/env python3
2
2
  # encoding: utf-8
3
3
 
4
+ from __future__ import annotations
5
+
4
6
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 1)
7
+ __version__ = (0, 0, 2)
6
8
  __all__ = [
7
- "odict", "AttrDict", "MapAttr", "MuMapAttr", "DictAttr", "ChainDictAttr",
9
+ "odict", "AttrDict", "MapAttr", "MuMapAttr",
10
+ "DictAttr", "ChainDictAttr", "UserDictAttr",
8
11
  ]
9
12
 
13
+ from collections import UserDict
10
14
  from collections.abc import Iterator, Mapping, MutableMapping
11
15
  from typing import Generic, Self, TypeVar
12
16
 
@@ -71,33 +75,73 @@ class MuMapAttr(MapAttr[K, V]):
71
75
  self.__dict__[key] = val
72
76
 
73
77
 
74
- class DictAttr(MuMapAttr):
78
+ class DictAttr(MuMapAttr[K, V]):
75
79
 
76
80
  def __getattr__(self, attr, /):
77
81
  return getattr(self.__dict__, attr)
78
82
 
79
83
  def __getattribute__(self, attr, /):
80
- if attr is "__dict__":
84
+ if attr == "__dict__":
81
85
  return super().__getattribute__(attr)
82
86
  try:
83
87
  return self[attr]
84
88
  except KeyError:
85
89
  return super().__getattribute__(attr)
86
90
 
87
- def __getitem__(self, key, /):
91
+ def __getitem__(self, key, /) -> V | Self: # type: ignore
88
92
  d = self.__dict__[key]
89
93
  if type(d) is dict:
90
94
  return type(self)(d)
91
95
  return d
92
96
 
93
97
 
94
- class ChainDictAttr(DictAttr):
98
+ class ChainDictAttr(DictAttr[K, V | "ChainDictAttr"]):
95
99
 
96
- def __getitem__(self, key, /):
100
+ def __getitem__(self, key, /) -> V | ChainDictAttr:
97
101
  try:
98
- super().__getitem__(key)
102
+ return super().__getitem__(key)
99
103
  except KeyError:
100
- d = type(self)()
101
- self.__dict__[key] = d.__dict__
104
+ d = self.__dict__[key] = type(self)()
102
105
  return d
103
106
 
107
+
108
+ class UserDictAttr(UserDict[K, V]):
109
+
110
+ @classmethod
111
+ def of(cls, m: Mapping, /) -> Self:
112
+ self = cls()
113
+ self.__dict__["data"] = m # type: ignore
114
+ return self
115
+
116
+ def __delattr__(self, attr, /):
117
+ try:
118
+ del self[attr]
119
+ except KeyError:
120
+ try:
121
+ super().__delattr__(attr)
122
+ except KeyError:
123
+ raise AttributeError(attr)
124
+
125
+ def __getattr__(self, attr, /):
126
+ return getattr(self.data, attr)
127
+
128
+ def __getattribute__(self, attr, /):
129
+ if attr in ("__dict__", "data") or attr == f"__{attr.strip('_')}__":
130
+ return super().__getattribute__(attr)
131
+ try:
132
+ return self[attr]
133
+ except KeyError:
134
+ return super().__getattribute__(attr)
135
+
136
+ def __getitem__(self, key, /) -> V | Self: # type: ignore
137
+ d = super().__getitem__(key)
138
+ if type(d) is dict:
139
+ return type(self)(d)
140
+ return d
141
+
142
+ def __setattr__(self, attr, val, /):
143
+ if attr == "data" and "data" not in self.__dict__:
144
+ self.__dict__["data"] = val
145
+ else:
146
+ self[attr] = val
147
+
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-dictattr"
3
- version = "0.0.1"
3
+ version = "0.0.2"
4
4
  description = "Python dictattr."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
Binary file
File without changes