private-attribute-cpp 1.0.2__cp313-cp313-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.

Potentially problematic release.


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

Binary file
private_attribute.pyi ADDED
@@ -0,0 +1,18 @@
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: ...
@@ -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,6 @@
1
+ private_attribute.cp313-win32.pyd,sha256=Hq9Fy4JETl49MerirAMDhJZtBYLnMGbbRB3PCzmTShg,125440
2
+ private_attribute.pyi,sha256=HJKzlAC5tD25DH6sjbq4Zv_Vzvwaw-WaW7hBaqchIZ4,701
3
+ private_attribute_cpp-1.0.2.dist-info/METADATA,sha256=W4U7UI8qBgGL6lS7kyhWpOFBhIGHVIQon3NxqxHazNU,7984
4
+ private_attribute_cpp-1.0.2.dist-info/WHEEL,sha256=0ABLuJ37exXk5N_efmYNs2NU9NK1K2Qlod_6bYkofEA,97
5
+ private_attribute_cpp-1.0.2.dist-info/top_level.txt,sha256=vOfJKfFO3AgjCIvyK6ppYDBTyJSsEAkf5w34knGZ3JE,19
6
+ private_attribute_cpp-1.0.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: false
4
+ Tag: cp313-cp313-win32
5
+
@@ -0,0 +1,2 @@
1
+
2
+ private_attribute