private-attribute-cpp 1.2.1__cp312-cp312-win32.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.
|
Binary file
|
private_attribute.pyi
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from typing import Any, TypeVar, Callable, TypedDict, Sequence
|
|
2
|
+
|
|
3
|
+
# define the dict that must have a key "__private_attrs__" and value must be the sequence of strings
|
|
4
|
+
class PrivateAttrDict(TypedDict):
|
|
5
|
+
__private_attrs__: Sequence[str]
|
|
6
|
+
|
|
7
|
+
T = TypeVar('T')
|
|
8
|
+
|
|
9
|
+
class _PrivateWrap[T]:
|
|
10
|
+
@property
|
|
11
|
+
def result(self) -> T: ...
|
|
12
|
+
|
|
13
|
+
class PrivateWrapProxy:
|
|
14
|
+
def __init__(self, decorator: Callable[[Any], T], orig: _PrivateWrap|None = None, /) -> None: ...
|
|
15
|
+
def __call__(self, func: Any) -> _PrivateWrap[T]: ...
|
|
16
|
+
|
|
17
|
+
class PrivateAttrType(type):
|
|
18
|
+
def __new__(cls, name: str, bases: tuple, attrs: PrivateAttrDict, private_func: Callable[[int, str], str]|None = None) -> PrivateAttrType: ...
|
|
19
|
+
|
|
20
|
+
class PrivateAttrBase(metaclass=PrivateAttrType):
|
|
21
|
+
__slots__ = ()
|
|
22
|
+
__private_attrs__ = ()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class private_temp:
|
|
26
|
+
@property
|
|
27
|
+
def name(self) -> str: ...
|
|
28
|
+
@property
|
|
29
|
+
def bases(self) -> tuple[type]: ...
|
|
30
|
+
@property
|
|
31
|
+
def attrs(self) -> PrivateAttrDict: ...
|
|
32
|
+
@property
|
|
33
|
+
def kwds(self) -> dict[str, Any]: ...
|
|
34
|
+
|
|
35
|
+
def prepare(name, bases, attrs, **kwds) -> private_temp: ...
|
|
36
|
+
def postprocess(typ: type, temp: private_temp) -> None: ...
|
|
37
|
+
def register_metaclass(typ: type) -> None: ...
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: private_attribute_cpp
|
|
3
|
+
Version: 1.2.1
|
|
4
|
+
Summary: A Python package that provides a way to define private attributes in C++ implementation.
|
|
5
|
+
Home-page: https://github.com/Locked-chess-official/private_attribute_cpp
|
|
6
|
+
Author: HuangHaoHua
|
|
7
|
+
Author-email: 13140752715@example.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Dynamic: author
|
|
11
|
+
Dynamic: author-email
|
|
12
|
+
Dynamic: description
|
|
13
|
+
Dynamic: description-content-type
|
|
14
|
+
Dynamic: home-page
|
|
15
|
+
Dynamic: license
|
|
16
|
+
Dynamic: summary
|
|
17
|
+
|
|
18
|
+
# Private Attribute (c++ implementation)
|
|
19
|
+
|
|
20
|
+
## Introduction
|
|
21
|
+
|
|
22
|
+
This package provide a way to create the private attribute like "C++" does.
|
|
23
|
+
|
|
24
|
+
## All Base API
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from private_attribute import (PrivateAttrBase, PrivateWrapProxy) # 1 Import public API
|
|
28
|
+
|
|
29
|
+
def my_generate_func(obj_id, attr_name): # 2 Optional: custom name generator
|
|
30
|
+
return f"_hidden_{obj_id}_{attr_name}"
|
|
31
|
+
|
|
32
|
+
class MyClass(PrivateAttrBase, private_func=my_generate_func): # 3 Inherit + optional custom generator
|
|
33
|
+
__private_attrs__ = ['a', 'b', 'c', 'result', 'conflicted_name'] # 4 Must declare all private attrs
|
|
34
|
+
|
|
35
|
+
def __init__(self):
|
|
36
|
+
self.a = 1
|
|
37
|
+
self.b = 2
|
|
38
|
+
self.c = 3
|
|
39
|
+
self.result = 42 # deliberately conflicts with internal names
|
|
40
|
+
|
|
41
|
+
# Normal methods can freely access private attributes
|
|
42
|
+
def public_way(self):
|
|
43
|
+
print(self.a, self.b, self.c)
|
|
44
|
+
|
|
45
|
+
# Real-world case: method wrapped by multiple decorators
|
|
46
|
+
@PrivateWrapProxy(memoize()) # 5 Apply any decorator safely
|
|
47
|
+
@PrivateWrapProxy(login_required()) # 5 Stack as many as needed
|
|
48
|
+
@PrivateWrapProxy(rate_limit(calls=10)) # 5
|
|
49
|
+
def expensive_api_call(self, x): # First definition (will be wrapped)
|
|
50
|
+
def inner(...):
|
|
51
|
+
return some_implementation(self.a, self.b, self.c, x)
|
|
52
|
+
inner(...)
|
|
53
|
+
return heavy_computation(self.a, self.b, self.c, x)
|
|
54
|
+
|
|
55
|
+
# Fix decorator order + resolve name conflicts
|
|
56
|
+
@PrivateWrapProxy(expensive_api_call.result.name2, expensive_api_call) # 6 Chain .result to push decorators down
|
|
57
|
+
@PrivateWrapProxy(expensive_api_call.result.name1, expensive_api_call) # 6 Resolve conflict with internal names
|
|
58
|
+
def expensive_api_call(self, x): # Final real implementation
|
|
59
|
+
return heavy_computation(self.a, self.b, self.c, x)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# ====================== Usage ======================
|
|
63
|
+
obj = MyClass()
|
|
64
|
+
obj.public_way() # prints: 1 2 3
|
|
65
|
+
|
|
66
|
+
print(hasattr(obj, 'a')) # False – truly hidden from outside
|
|
67
|
+
print(obj.expensive_api_call(10)) # works with all decorators applied
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
| # | API | Purpose | Required? |
|
|
71
|
+
|---|----------------------------------------|-------------------------------------------------------|-----------|
|
|
72
|
+
| 1 | PrivateAttrBase | Base class – must inherit | Yes |
|
|
73
|
+
| 1 | PrivateWrapProxy | Decorator wrapper for arbitrary decorators | When needed |
|
|
74
|
+
| 2 | private_func=callable | Custom hidden-name generator | Optional |
|
|
75
|
+
| 3 | Pass private_func in class definition | Same as above | Optional |
|
|
76
|
+
| 4 | \_\_private_attrs\_\_ list | Declare which attributes are private | Yes |
|
|
77
|
+
| 5 | @PrivateWrapProxy(...) | Make any decorator compatible with private attributes | When needed |
|
|
78
|
+
| 6 | method.result.xxx chain + dummy wrap | Fix decorator order and name conflicts | When needed |
|
|
79
|
+
|
|
80
|
+
## Usage
|
|
81
|
+
|
|
82
|
+
This is a simple usage about the module:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from private_attribute import PrivateAttrBase
|
|
86
|
+
|
|
87
|
+
class MyClass(PrivateAttrBase):
|
|
88
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
89
|
+
def __init__(self):
|
|
90
|
+
self.a = 1
|
|
91
|
+
self.b = 2
|
|
92
|
+
self.c = 3
|
|
93
|
+
|
|
94
|
+
def public_way(self):
|
|
95
|
+
print(self.a, self.b, self.c)
|
|
96
|
+
|
|
97
|
+
obj = MyClass()
|
|
98
|
+
obj.public_way() # (1, 2, 3)
|
|
99
|
+
|
|
100
|
+
print(hasattr(obj, 'a')) # False
|
|
101
|
+
print(hasattr(obj, 'b')) # False
|
|
102
|
+
print(hasattr(obj, 'c')) # False
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
All of the attributes in `__private_attrs__` will be hidden from the outside world, and stored by another name.
|
|
106
|
+
|
|
107
|
+
You can use your function to generate the name. It needs the id of the obj and the name of the attribute:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
def my_generate_func(obj_id, attr_name):
|
|
111
|
+
return some_string
|
|
112
|
+
|
|
113
|
+
class MyClass(PrivateAttrBase, private_func=my_generate_func):
|
|
114
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
115
|
+
def __init__(self):
|
|
116
|
+
self.a = 1
|
|
117
|
+
self.b = 2
|
|
118
|
+
self.c = 3
|
|
119
|
+
|
|
120
|
+
def public_way(self):
|
|
121
|
+
print(self.a, self.b, self.c)
|
|
122
|
+
|
|
123
|
+
obj = MyClass()
|
|
124
|
+
obj.public_way() # (1, 2, 3)
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
If the method will be decorated, the `property`, `classmethod` and `staticmethod` will be supported.
|
|
129
|
+
For the other, you can use the `PrivateWrapProxy` to wrap the function:
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
from private_attribute import PrivateAttrBase, PrivateWrapProxy
|
|
133
|
+
|
|
134
|
+
class MyClass(PrivateAttrBase):
|
|
135
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
136
|
+
@PrivateWrapProxy(decorator1())
|
|
137
|
+
@PrivateWrapProxy(decorator2())
|
|
138
|
+
def method1(self):
|
|
139
|
+
...
|
|
140
|
+
|
|
141
|
+
@method1.attr_name
|
|
142
|
+
@PrivateWrapProxy(lambda _: _) # use empty function to wrap
|
|
143
|
+
def method1(self):
|
|
144
|
+
...
|
|
145
|
+
|
|
146
|
+
@PrivateWrapProxy(decorator3())
|
|
147
|
+
def method2(self):
|
|
148
|
+
...
|
|
149
|
+
|
|
150
|
+
@method2.attr_name
|
|
151
|
+
@PrivateWrapProxy(lambda _: _)
|
|
152
|
+
def method2(self):
|
|
153
|
+
...
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The `PrivateWrapProxy` is a decorator, and it will wrap the function with the decorator. When it decorates the method, it returns a `_PrivateWrap` object.
|
|
159
|
+
|
|
160
|
+
The `_PrivateWrap` has the public api `result`. It returns the original decoratored result.
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from private_attribute import PrivateAttrBase, PrivateWrapProxy
|
|
164
|
+
|
|
165
|
+
class MyClass(PrivateAttrBase):
|
|
166
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
167
|
+
@PrivateWrapProxy(decorator1())
|
|
168
|
+
@PrivateWrapProxy(decorator2())
|
|
169
|
+
def method1(self):
|
|
170
|
+
...
|
|
171
|
+
|
|
172
|
+
@PrivateWrapProxy(method1.result.conflict_attr_name1, method1) # Use the argument "method1" to save old func
|
|
173
|
+
def method1(self):
|
|
174
|
+
...
|
|
175
|
+
|
|
176
|
+
@PrivateWrapProxy(method1.result.conflict_attr_name2, method1)
|
|
177
|
+
def method1(self):
|
|
178
|
+
...
|
|
179
|
+
|
|
180
|
+
@PrivateWrapProxy(decorator3())
|
|
181
|
+
def method2(self):
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Advanced API
|
|
185
|
+
|
|
186
|
+
### define your metaclass based on one metaclass
|
|
187
|
+
|
|
188
|
+
You can define your metaclass based on one metaclass:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from abc import ABCMeta, abstractmethod
|
|
192
|
+
import private_attribute
|
|
193
|
+
|
|
194
|
+
class PrivateAbcMeta(ABCMeta):
|
|
195
|
+
def __new__(cls, name, bases, attrs, **kwargs):
|
|
196
|
+
temp = private_attribute.prepare(name, bases, attrs, **kwargs)
|
|
197
|
+
typ = super().__new__(cls, temp.name, temp.base, temp.attrs, **temp.kwds)
|
|
198
|
+
private_attribute.postprocess(typ, temp)
|
|
199
|
+
return typ
|
|
200
|
+
|
|
201
|
+
private_attribute.register_metaclass(PrivateAbcMeta)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
By this way you create a metaclass both can behave as ABC and private attribute:
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
class MyClass(metaclass=PrivateAbcMeta):
|
|
208
|
+
__private_attrs__ = ()
|
|
209
|
+
__slots__ = ()
|
|
210
|
+
|
|
211
|
+
@abstractmethod
|
|
212
|
+
def my_function(self): ...
|
|
213
|
+
|
|
214
|
+
class MyImplement(MyClass):
|
|
215
|
+
__private_attrs__ = ("_a",)
|
|
216
|
+
def __init__(self, value=1):
|
|
217
|
+
self._a = value
|
|
218
|
+
|
|
219
|
+
def my_function(self):
|
|
220
|
+
return self._a
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Finally:
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
>>> a = MyImplement(1)
|
|
227
|
+
>>> a.my_function()
|
|
228
|
+
1
|
|
229
|
+
>>> a._a
|
|
230
|
+
Traceback (most recent call last):
|
|
231
|
+
File "<pyshell#2>", line 1, in <module>
|
|
232
|
+
a._a
|
|
233
|
+
AttributeError: private attribute
|
|
234
|
+
>>> MyClass()
|
|
235
|
+
Traceback (most recent call last):
|
|
236
|
+
File "<pyshell#3>", line 1, in <module>
|
|
237
|
+
MyClass()
|
|
238
|
+
TypeError: Can't instantiate abstract class MyClass without an implementation for abstract method 'my_function'
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Notes
|
|
242
|
+
|
|
243
|
+
- All of the private attributes class must contain the `__private_attrs__` attribute.
|
|
244
|
+
- The `__private_attrs__` attribute must be a sequence of strings.
|
|
245
|
+
- You cannot define the name which in `__slots__` to `__private_attrs__`.
|
|
246
|
+
- When you define `__slots__` and `__private_attrs__` in one class, the attributes in `__private_attrs__` can also be defined in the methods, even though they are not in `__slots__`.
|
|
247
|
+
- All of the object that is the instance of the class "PrivateAttrBase" or its subclass are default to be unable to be pickled.
|
|
248
|
+
- Finally the attributes' names in `__private_attrs__` will be change to a tuple with two hash.
|
|
249
|
+
- Finally the `_PrivateWrap` object will be recoveried to the original object.
|
|
250
|
+
- One class defined in another class cannot use another class's private attribute.
|
|
251
|
+
- One parent class defined an attribute which not in `__private_attrs__` or not a `PrivateAttrType` instance, the child class shouldn't contain the attribute in its `__private_attrs__`.
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT
|
|
256
|
+
|
|
257
|
+
## Requirement
|
|
258
|
+
|
|
259
|
+
This package require the c++ module "picosha2" to compute the sha256 hash.
|
|
260
|
+
|
|
261
|
+
## Support
|
|
262
|
+
|
|
263
|
+
Now it doesn't support "PyPy".
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
private_attribute.cp312-win32.pyd,sha256=pl35VEUGQjYM9UVS_V5uKsWSz_tbzD37dgNiKvhHEjA,144896
|
|
2
|
+
private_attribute.pyi,sha256=f-C03v19ws4k7Tx21f6kVc7C7cEwlaqfXN8cqVDj6Vo,1223
|
|
3
|
+
private_attribute_cpp-1.2.1.dist-info/METADATA,sha256=Qc3__t7xASANLjAf7XZ1w3jL2SWp9u96qMtqfaYilXg,8853
|
|
4
|
+
private_attribute_cpp-1.2.1.dist-info/WHEEL,sha256=JQV-xY5E6RTZnUqfqfY1oq4KqhknSvUaAcZOOtrKwyw,98
|
|
5
|
+
private_attribute_cpp-1.2.1.dist-info/top_level.txt,sha256=vOfJKfFO3AgjCIvyK6ppYDBTyJSsEAkf5w34knGZ3JE,19
|
|
6
|
+
private_attribute_cpp-1.2.1.dist-info/RECORD,,
|