private-attribute-cpp 1.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.

Potentially problematic release.


This version of private-attribute-cpp might be problematic. Click here for more details.

@@ -0,0 +1,2 @@
1
+ include README.md
2
+ include picosha2.h
@@ -0,0 +1,213 @@
1
+ Metadata-Version: 2.4
2
+ Name: private_attribute_cpp
3
+ Version: 1.0.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 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
+ @expensive_api_call.non_conflict_attr_name1 # 6 Easy access to internal names
56
+ @expensive_api_call.non_conflict_attr_name2 # 6 Easy use when the name has no conflict
57
+ @PrivateWrapProxy(lambda f: f) # 5 dummy wrapper just to restore order
58
+ def expensive_api_call(self, x): # Second definition (will be wrapped)
59
+ return heavy_computation(self.a, self.b, self.c, x)
60
+
61
+ # Fix decorator order + resolve name conflicts
62
+ @PrivateWrapProxy(expensive_api_call.result.conflicted_name2, expensive_api_call) # 7 Chain .result to push decorators down
63
+ @PrivateWrapProxy(expensive_api_call.result.conflicted_name1, expensive_api_call) # 7 Resolve conflict with internal names
64
+ def expensive_api_call(self, x): # Final real implementation
65
+ return heavy_computation(self.a, self.b, self.c, x)
66
+
67
+
68
+ # ====================== Usage ======================
69
+ obj = MyClass()
70
+ obj.public_way() # prints: 1 2 3
71
+
72
+ print(hasattr(obj, 'a')) # False – truly hidden from outside
73
+ print(obj.expensive_api_call(10)) # works with all decorators applied
74
+ ```
75
+
76
+ | # | API | Purpose | Required? |
77
+ |---|----------------------------------------|-------------------------------------------------------|-----------|
78
+ | 1 | PrivateAttrBase | Base class – must inherit | Yes |
79
+ | 1 | PrivateWrapProxy | Decorator wrapper for arbitrary decorators | When needed |
80
+ | 2 | private_func=callable | Custom hidden-name generator | Optional |
81
+ | 3 | Pass private_func in class definition | Same as above | Optional |
82
+ | 4 | \_\_private_attrs\_\_ list | Declare which attributes are private | Yes |
83
+ | 5 | @PrivateWrapProxy(...) | Make any decorator compatible with private attributes | When needed |
84
+ | 6 | method.xxx | Normal api name proxy | Based on its api |
85
+ | 7 | method.result.xxx chain + dummy wrap | Fix decorator order and name conflicts | When needed |
86
+
87
+ ## Usage
88
+
89
+ This is a simple usage about the module:
90
+
91
+ ```python
92
+ from private_attribute import PrivateAttrBase
93
+
94
+ class MyClass(PrivateAttrBase):
95
+ __private_attrs__ = ['a', 'b', 'c']
96
+ def __init__(self):
97
+ self.a = 1
98
+ self.b = 2
99
+ self.c = 3
100
+
101
+ def public_way(self):
102
+ print(self.a, self.b, self.c)
103
+
104
+ obj = MyClass()
105
+ obj.public_way() # (1, 2, 3)
106
+
107
+ print(hasattr(obj, 'a')) # False
108
+ print(hasattr(obj, 'b')) # False
109
+ print(hasattr(obj, 'c')) # False
110
+ ```
111
+
112
+ All of the attributes in `__private_attrs__` will be hidden from the outside world, and stored by another name.
113
+
114
+ You can use your function to generate the name. It needs the id of the obj and the name of the attribute:
115
+
116
+ ```python
117
+ def my_generate_func(obj_id, attr_name):
118
+ return some_string
119
+
120
+ class MyClass(PrivateAttrBase, private_func=my_generate_func):
121
+ __private_attrs__ = ['a', 'b', 'c']
122
+ def __init__(self):
123
+ self.a = 1
124
+ self.b = 2
125
+ self.c = 3
126
+
127
+ def public_way(self):
128
+ print(self.a, self.b, self.c)
129
+
130
+ obj = MyClass()
131
+ obj.public_way() # (1, 2, 3)
132
+
133
+ ```
134
+
135
+ If the method will be decorated, the `property`, `classmethod` and `staticmethod` will be supported.
136
+ For the other, you can use the `PrivateWrapProxy` to wrap the function:
137
+
138
+ ```python
139
+ from private_attribute import PrivateAttrBase, PrivateWrapProxy
140
+
141
+ class MyClass(PrivateAttrBase):
142
+ __private_attrs__ = ['a', 'b', 'c']
143
+ @PrivateWrapProxy(decorator1())
144
+ @PrivateWrapProxy(decorator2())
145
+ def method1(self):
146
+ ...
147
+
148
+ @method1.attr_name
149
+ @PrivateWrapProxy(lambda _: _) # use empty function to wrap
150
+ def method1(self):
151
+ ...
152
+
153
+ @PrivateWrapProxy(decorator3())
154
+ def method2(self):
155
+ ...
156
+
157
+ @method2.attr_name
158
+ @PrivateWrapProxy(lambda _: _)
159
+ def method2(self):
160
+ ...
161
+
162
+
163
+ ```
164
+
165
+ The `PrivateWrapProxy` is a decorator, and it will wrap the function with the decorator. When it decorates the method, it returns a `_PrivateWrap` object.
166
+
167
+ The `_PrivateWrap` has the public api `result`. It returns the original decoratored result.
168
+
169
+ ```python
170
+ from private_attribute import PrivateAttrBase, PrivateWrapProxy
171
+
172
+ class MyClass(PrivateAttrBase):
173
+ __private_attrs__ = ['a', 'b', 'c']
174
+ @PrivateWrapProxy(decorator1())
175
+ @PrivateWrapProxy(decorator2())
176
+ def method1(self):
177
+ ...
178
+
179
+ @PrivateWrapProxy(method1.result.conflict_attr_name1, method1) # Use the argument "method1" to save old func
180
+ def method1(self):
181
+ ...
182
+
183
+ @PrivateWrapProxy(method1.result.conflict_attr_name2, method1)
184
+ def method1(self):
185
+ ...
186
+
187
+ @PrivateWrapProxy(decorator3())
188
+ def method2(self):
189
+ ```
190
+
191
+ ## Notes
192
+
193
+ - All of the private attributes class must contain the `__private_attrs__` attribute.
194
+ - The `__private_attrs__` attribute must be a sequence of strings.
195
+ - You cannot define the name which in `__slots__` to `__private_attrs__`.
196
+ - 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__`.
197
+ - All of the object that is the instance of the class "PrivateAttrBase" or its subclass are default to be unable to be pickled.
198
+ - Finally the attributes' names in `__private_attrs__` will be change to a tuple with two hash.
199
+ - Finally the `_PrivateWrap` object will be recoveried to the original object.
200
+ - One class defined in another class cannot use another class's private attribute.
201
+ - 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__`.
202
+
203
+ ## License
204
+
205
+ MIT
206
+
207
+ ## Requirement
208
+
209
+ This package require the c++ module "picosha2" to compute the sha256 hash.
210
+
211
+ ## Support
212
+
213
+ Now it doesn't support "PyPy".
@@ -0,0 +1,196 @@
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 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
+ @expensive_api_call.non_conflict_attr_name1 # 6 Easy access to internal names
39
+ @expensive_api_call.non_conflict_attr_name2 # 6 Easy use when the name has no conflict
40
+ @PrivateWrapProxy(lambda f: f) # 5 dummy wrapper just to restore order
41
+ def expensive_api_call(self, x): # Second definition (will be wrapped)
42
+ return heavy_computation(self.a, self.b, self.c, x)
43
+
44
+ # Fix decorator order + resolve name conflicts
45
+ @PrivateWrapProxy(expensive_api_call.result.conflicted_name2, expensive_api_call) # 7 Chain .result to push decorators down
46
+ @PrivateWrapProxy(expensive_api_call.result.conflicted_name1, expensive_api_call) # 7 Resolve conflict with internal names
47
+ def expensive_api_call(self, x): # Final real implementation
48
+ return heavy_computation(self.a, self.b, self.c, x)
49
+
50
+
51
+ # ====================== Usage ======================
52
+ obj = MyClass()
53
+ obj.public_way() # prints: 1 2 3
54
+
55
+ print(hasattr(obj, 'a')) # False – truly hidden from outside
56
+ print(obj.expensive_api_call(10)) # works with all decorators applied
57
+ ```
58
+
59
+ | # | API | Purpose | Required? |
60
+ |---|----------------------------------------|-------------------------------------------------------|-----------|
61
+ | 1 | PrivateAttrBase | Base class – must inherit | Yes |
62
+ | 1 | PrivateWrapProxy | Decorator wrapper for arbitrary decorators | When needed |
63
+ | 2 | private_func=callable | Custom hidden-name generator | Optional |
64
+ | 3 | Pass private_func in class definition | Same as above | Optional |
65
+ | 4 | \_\_private_attrs\_\_ list | Declare which attributes are private | Yes |
66
+ | 5 | @PrivateWrapProxy(...) | Make any decorator compatible with private attributes | When needed |
67
+ | 6 | method.xxx | Normal api name proxy | Based on its api |
68
+ | 7 | method.result.xxx chain + dummy wrap | Fix decorator order and name conflicts | When needed |
69
+
70
+ ## Usage
71
+
72
+ This is a simple usage about the module:
73
+
74
+ ```python
75
+ from private_attribute import PrivateAttrBase
76
+
77
+ class MyClass(PrivateAttrBase):
78
+ __private_attrs__ = ['a', 'b', 'c']
79
+ def __init__(self):
80
+ self.a = 1
81
+ self.b = 2
82
+ self.c = 3
83
+
84
+ def public_way(self):
85
+ print(self.a, self.b, self.c)
86
+
87
+ obj = MyClass()
88
+ obj.public_way() # (1, 2, 3)
89
+
90
+ print(hasattr(obj, 'a')) # False
91
+ print(hasattr(obj, 'b')) # False
92
+ print(hasattr(obj, 'c')) # False
93
+ ```
94
+
95
+ All of the attributes in `__private_attrs__` will be hidden from the outside world, and stored by another name.
96
+
97
+ You can use your function to generate the name. It needs the id of the obj and the name of the attribute:
98
+
99
+ ```python
100
+ def my_generate_func(obj_id, attr_name):
101
+ return some_string
102
+
103
+ class MyClass(PrivateAttrBase, private_func=my_generate_func):
104
+ __private_attrs__ = ['a', 'b', 'c']
105
+ def __init__(self):
106
+ self.a = 1
107
+ self.b = 2
108
+ self.c = 3
109
+
110
+ def public_way(self):
111
+ print(self.a, self.b, self.c)
112
+
113
+ obj = MyClass()
114
+ obj.public_way() # (1, 2, 3)
115
+
116
+ ```
117
+
118
+ If the method will be decorated, the `property`, `classmethod` and `staticmethod` will be supported.
119
+ For the other, you can use the `PrivateWrapProxy` to wrap the function:
120
+
121
+ ```python
122
+ from private_attribute import PrivateAttrBase, PrivateWrapProxy
123
+
124
+ class MyClass(PrivateAttrBase):
125
+ __private_attrs__ = ['a', 'b', 'c']
126
+ @PrivateWrapProxy(decorator1())
127
+ @PrivateWrapProxy(decorator2())
128
+ def method1(self):
129
+ ...
130
+
131
+ @method1.attr_name
132
+ @PrivateWrapProxy(lambda _: _) # use empty function to wrap
133
+ def method1(self):
134
+ ...
135
+
136
+ @PrivateWrapProxy(decorator3())
137
+ def method2(self):
138
+ ...
139
+
140
+ @method2.attr_name
141
+ @PrivateWrapProxy(lambda _: _)
142
+ def method2(self):
143
+ ...
144
+
145
+
146
+ ```
147
+
148
+ The `PrivateWrapProxy` is a decorator, and it will wrap the function with the decorator. When it decorates the method, it returns a `_PrivateWrap` object.
149
+
150
+ The `_PrivateWrap` has the public api `result`. It returns the original decoratored result.
151
+
152
+ ```python
153
+ from private_attribute import PrivateAttrBase, PrivateWrapProxy
154
+
155
+ class MyClass(PrivateAttrBase):
156
+ __private_attrs__ = ['a', 'b', 'c']
157
+ @PrivateWrapProxy(decorator1())
158
+ @PrivateWrapProxy(decorator2())
159
+ def method1(self):
160
+ ...
161
+
162
+ @PrivateWrapProxy(method1.result.conflict_attr_name1, method1) # Use the argument "method1" to save old func
163
+ def method1(self):
164
+ ...
165
+
166
+ @PrivateWrapProxy(method1.result.conflict_attr_name2, method1)
167
+ def method1(self):
168
+ ...
169
+
170
+ @PrivateWrapProxy(decorator3())
171
+ def method2(self):
172
+ ```
173
+
174
+ ## Notes
175
+
176
+ - All of the private attributes class must contain the `__private_attrs__` attribute.
177
+ - The `__private_attrs__` attribute must be a sequence of strings.
178
+ - You cannot define the name which in `__slots__` to `__private_attrs__`.
179
+ - 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__`.
180
+ - All of the object that is the instance of the class "PrivateAttrBase" or its subclass are default to be unable to be pickled.
181
+ - Finally the attributes' names in `__private_attrs__` will be change to a tuple with two hash.
182
+ - Finally the `_PrivateWrap` object will be recoveried to the original object.
183
+ - One class defined in another class cannot use another class's private attribute.
184
+ - 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__`.
185
+
186
+ ## License
187
+
188
+ MIT
189
+
190
+ ## Requirement
191
+
192
+ This package require the c++ module "picosha2" to compute the sha256 hash.
193
+
194
+ ## Support
195
+
196
+ Now it doesn't support "PyPy".