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