private-attribute-cpp 1.0.11__cp314-cp314t-macosx_11_0_arm64.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,22 @@
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__ = ()
@@ -0,0 +1,206 @@
1
+ Metadata-Version: 2.4
2
+ Name: private_attribute_cpp
3
+ Version: 1.0.11
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
+ # 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
+ ## Notes
185
+
186
+ - All of the private attributes class must contain the `__private_attrs__` attribute.
187
+ - The `__private_attrs__` attribute must be a sequence of strings.
188
+ - You cannot define the name which in `__slots__` to `__private_attrs__`.
189
+ - 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__`.
190
+ - All of the object that is the instance of the class "PrivateAttrBase" or its subclass are default to be unable to be pickled.
191
+ - Finally the attributes' names in `__private_attrs__` will be change to a tuple with two hash.
192
+ - Finally the `_PrivateWrap` object will be recoveried to the original object.
193
+ - One class defined in another class cannot use another class's private attribute.
194
+ - 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__`.
195
+
196
+ ## License
197
+
198
+ MIT
199
+
200
+ ## Requirement
201
+
202
+ This package require the c++ module "picosha2" to compute the sha256 hash.
203
+
204
+ ## Support
205
+
206
+ Now it doesn't support "PyPy".
@@ -0,0 +1,6 @@
1
+ private_attribute.pyi,sha256=OHoRCWWfgguFKPdB4SVQlouj5DBOGwfJnM_O52Y4KE0,782
2
+ private_attribute.cpython-314t-darwin.so,sha256=J5esO2ML-YSoIUjUNuoez3u4ruNyroZGbC-Su7njjIU,266744
3
+ private_attribute_cpp-1.0.11.dist-info/RECORD,,
4
+ private_attribute_cpp-1.0.11.dist-info/WHEEL,sha256=26nyvDx4qlf6NyRSh1NSNrXJDCQeX0hnJ7EH1bB1egM,137
5
+ private_attribute_cpp-1.0.11.dist-info/top_level.txt,sha256=vOfJKfFO3AgjCIvyK6ppYDBTyJSsEAkf5w34knGZ3JE,19
6
+ private_attribute_cpp-1.0.11.dist-info/METADATA,sha256=QKPj5vXbuBcje3RsHsCHvn3ntFglITc7fAGSfOWh8gE,7194
@@ -0,0 +1,6 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: false
4
+ Tag: cp314-cp314t-macosx_11_0_arm64
5
+ Generator: delocate 0.13.0
6
+
@@ -0,0 +1,2 @@
1
+
2
+ private_attribute