private-attribute-cpp 2.1.14__cp315-cp315-win_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.
- private_attribute.cp315-win_arm64.pyd +0 -0
- private_attribute.pyi +211 -0
- private_attribute_cpp-2.1.14.data/platlib/msvcp140-dfb0e03a4e9979ecd6332ad9d7325a56.dll +0 -0
- private_attribute_cpp-2.1.14.dist-info/DELVEWHEEL +2 -0
- private_attribute_cpp-2.1.14.dist-info/METADATA +271 -0
- private_attribute_cpp-2.1.14.dist-info/RECORD +9 -0
- private_attribute_cpp-2.1.14.dist-info/WHEEL +5 -0
- private_attribute_cpp-2.1.14.dist-info/licenses/LICENSE +21 -0
- private_attribute_cpp-2.1.14.dist-info/top_level.txt +2 -0
|
Binary file
|
private_attribute.pyi
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"""A module that provides a metaclass for creating classes with private attributes.
|
|
2
|
+
Private attributes are defined in the `__private_attrs__` sequence and are only
|
|
3
|
+
You can use the `PrivateAttrBase` metaclass to create classes with private attributes.
|
|
4
|
+
The attributes which are private are not on the instance's `__dict__` and cannot be accessed outside
|
|
5
|
+
but in the methods defined in class it is reachable.
|
|
6
|
+
Usage example:
|
|
7
|
+
```python
|
|
8
|
+
class MyClass(PrivateAttrBase):
|
|
9
|
+
__private_attrs__ = ('_private_attr1',)
|
|
10
|
+
def __init__(self):
|
|
11
|
+
self._private_attr1 = 1
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def public_attr1(self):
|
|
15
|
+
return self._private_attr1
|
|
16
|
+
```
|
|
17
|
+
"""
|
|
18
|
+
from typing import Any, TypeVar, Callable, TypedDict, Sequence, Generic, Optional, cast
|
|
19
|
+
from types import FunctionType
|
|
20
|
+
|
|
21
|
+
# define the dict that must have a key "__private_attrs__" and value must be the sequence of strings
|
|
22
|
+
class _PrivateAttrDict(TypedDict):
|
|
23
|
+
__private_attrs__: Optional[Sequence[str]]
|
|
24
|
+
|
|
25
|
+
_T = TypeVar('_T')
|
|
26
|
+
|
|
27
|
+
class _PrivateWrap(Generic[_T]):
|
|
28
|
+
@property
|
|
29
|
+
def result(self) -> _T:
|
|
30
|
+
"the final result of decorating"
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def funcs(self) -> tuple[FunctionType]:
|
|
35
|
+
"the original functions"
|
|
36
|
+
...
|
|
37
|
+
|
|
38
|
+
def __getattr__(self, name: str) -> Any:
|
|
39
|
+
return getattr(self.result, name)
|
|
40
|
+
|
|
41
|
+
def __call__(self, *args: Any, **kwargs: Any) -> Any:
|
|
42
|
+
if callable(self.result):
|
|
43
|
+
return self.result(*args, **kwargs)
|
|
44
|
+
raise TypeError(f'{self.result!r} is not callable')
|
|
45
|
+
|
|
46
|
+
def __get__(self, instance: Any, owner: type) -> Any:
|
|
47
|
+
if hasattr(self.result, "__get__"):
|
|
48
|
+
descriptor = cast(Any, self.result)
|
|
49
|
+
return descriptor.__get__(instance, owner)
|
|
50
|
+
return self.result
|
|
51
|
+
|
|
52
|
+
_TVar = TypeVar('_TVar')
|
|
53
|
+
|
|
54
|
+
class PrivateWrapProxy(Generic[_TVar, _T]):
|
|
55
|
+
"""
|
|
56
|
+
PrivateWrapProxy is a proxy for private attributes.
|
|
57
|
+
Usage:
|
|
58
|
+
```
|
|
59
|
+
from private_attribute import PrivateWrapProxy, PrivateAttrBase
|
|
60
|
+
|
|
61
|
+
class MyClass(PrivateAttrBase):
|
|
62
|
+
__private_attrs__ = ()
|
|
63
|
+
@PrivateWrapProxy(decorator)
|
|
64
|
+
def my_method(self): ...
|
|
65
|
+
|
|
66
|
+
@PrivateWrapProxy(decorator)
|
|
67
|
+
def my_method2(self): ...
|
|
68
|
+
```
|
|
69
|
+
It returned a '_PrivateWrap' object.
|
|
70
|
+
|
|
71
|
+
If you need to decorate more function, use like this:
|
|
72
|
+
```
|
|
73
|
+
from private_attribute import PrivateWrapProxy, PrivateAttrBase
|
|
74
|
+
|
|
75
|
+
class MyClass(PrivateAttrBase):
|
|
76
|
+
__private_attrs__ = ()
|
|
77
|
+
@PrivateWrapProxy(decorator)
|
|
78
|
+
def my_method(self): ...
|
|
79
|
+
|
|
80
|
+
@PrivateWrapProxy(my_method.some_decorator, my_method)
|
|
81
|
+
def my_method(self): ...
|
|
82
|
+
```
|
|
83
|
+
"""
|
|
84
|
+
def __init__(self, decorator: Callable[[_TVar], _T], orig: _PrivateWrap[Any]|None = None, /) -> None: ...
|
|
85
|
+
def __call__(self, func: _TVar | _PrivateWrap[_TVar], /) -> _PrivateWrap[_T]: ...
|
|
86
|
+
|
|
87
|
+
class PrivateAttrType(type):
|
|
88
|
+
"metaclass for private attributes"
|
|
89
|
+
def __new__(cls, name: str, bases: tuple,
|
|
90
|
+
attrs: _PrivateAttrDict, /, *,
|
|
91
|
+
private_func: Callable[[int, str], str]|None = None) -> PrivateAttrType: ...
|
|
92
|
+
|
|
93
|
+
class PrivateAttrBase(metaclass=PrivateAttrType):
|
|
94
|
+
"The class to help to create private attribute. It does not have any special behavior."
|
|
95
|
+
__slots__ = ()
|
|
96
|
+
__private_attrs__ = ()
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class _PrivateTemp:
|
|
100
|
+
@property
|
|
101
|
+
def name(self) -> str: ...
|
|
102
|
+
@property
|
|
103
|
+
def bases(self) -> tuple[type]: ...
|
|
104
|
+
@property
|
|
105
|
+
def attrs(self) -> dict[str, Any]: ...
|
|
106
|
+
@property
|
|
107
|
+
def kwds(self) -> dict[str, Any]: ...
|
|
108
|
+
|
|
109
|
+
def prepare(name: str, bases: tuple, attrs: _PrivateAttrDict, /, **kwds) -> _PrivateTemp:
|
|
110
|
+
"""
|
|
111
|
+
function for custom metaclass to create private attributes class.
|
|
112
|
+
|
|
113
|
+
def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
|
|
114
|
+
the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
|
|
115
|
+
|
|
116
|
+
def postprocess(type: type, tmp: tempobject) -> None:
|
|
117
|
+
the function to postprocess for creating private attributes class. The custom metaclass can call this
|
|
118
|
+
|
|
119
|
+
def register_metaclass(metaclass: type) -> None:
|
|
120
|
+
the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
|
|
121
|
+
otherwise the private attributes class created by this custom metaclass will not work.
|
|
122
|
+
|
|
123
|
+
All usage of this module should be like:
|
|
124
|
+
```
|
|
125
|
+
from abc import ABCMeta
|
|
126
|
+
import private_attribute
|
|
127
|
+
|
|
128
|
+
class PrivateAbcMeta(ABCMeta):
|
|
129
|
+
def __new__(cls, *args, **kwargs):
|
|
130
|
+
temp = private_attribute.prepare(*args, **kwargs)
|
|
131
|
+
typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
|
|
132
|
+
private_attribute.postprocess(typ, temp)
|
|
133
|
+
return typ
|
|
134
|
+
|
|
135
|
+
private_attribute.register_metaclass(PrivateAbcMeta)
|
|
136
|
+
```
|
|
137
|
+
"""
|
|
138
|
+
...
|
|
139
|
+
|
|
140
|
+
def postprocess(typ: type, temp: _PrivateTemp, /) -> None:
|
|
141
|
+
"""
|
|
142
|
+
function for custom metaclass to create private attributes class.
|
|
143
|
+
|
|
144
|
+
def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
|
|
145
|
+
the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
|
|
146
|
+
|
|
147
|
+
def postprocess(type: type, tmp: tempobject) -> None:
|
|
148
|
+
the function to postprocess for creating private attributes class. The custom metaclass can call this
|
|
149
|
+
|
|
150
|
+
def register_metaclass(metaclass: type) -> None:
|
|
151
|
+
the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
|
|
152
|
+
otherwise the private attributes class created by this custom metaclass will not work.
|
|
153
|
+
|
|
154
|
+
All usage of this module should be like:
|
|
155
|
+
```
|
|
156
|
+
from abc import ABCMeta
|
|
157
|
+
import private_attribute
|
|
158
|
+
|
|
159
|
+
class PrivateAbcMeta(ABCMeta):
|
|
160
|
+
def __new__(cls, *args, **kwargs):
|
|
161
|
+
temp = private_attribute.prepare(*args, **kwargs)
|
|
162
|
+
typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
|
|
163
|
+
private_attribute.postprocess(typ, temp)
|
|
164
|
+
return typ
|
|
165
|
+
|
|
166
|
+
private_attribute.register_metaclass(PrivateAbcMeta)
|
|
167
|
+
```
|
|
168
|
+
"""
|
|
169
|
+
...
|
|
170
|
+
def register_metaclass(typ: type, /) -> None:
|
|
171
|
+
"""
|
|
172
|
+
function for custom metaclass to create private attributes class.
|
|
173
|
+
|
|
174
|
+
def prepare(name: str, bases: tuple, attrs: dict, **kwds) -> tempobject:
|
|
175
|
+
the function to prepare for creating private attributes class. It will return a temporary object which has the same information as the arguments.
|
|
176
|
+
|
|
177
|
+
def postprocess(type: type, tmp: tempobject) -> None:
|
|
178
|
+
the function to postprocess for creating private attributes class. The custom metaclass can call this
|
|
179
|
+
|
|
180
|
+
def register_metaclass(metaclass: type) -> None:
|
|
181
|
+
the function to register custom metaclass. The custom metaclass must call this function to register itself before creating any private attributes class,
|
|
182
|
+
otherwise the private attributes class created by this custom metaclass will not work.
|
|
183
|
+
|
|
184
|
+
All usage of this module should be like:
|
|
185
|
+
```
|
|
186
|
+
from abc import ABCMeta
|
|
187
|
+
import private_attribute
|
|
188
|
+
|
|
189
|
+
class PrivateAbcMeta(ABCMeta):
|
|
190
|
+
def __new__(cls, *args, **kwargs):
|
|
191
|
+
temp = private_attribute.prepare(*args, **kwargs)
|
|
192
|
+
typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
|
|
193
|
+
private_attribute.postprocess(typ, temp)
|
|
194
|
+
return typ
|
|
195
|
+
|
|
196
|
+
private_attribute.register_metaclass(PrivateAbcMeta)
|
|
197
|
+
```
|
|
198
|
+
"""
|
|
199
|
+
...
|
|
200
|
+
def ensure_type(typ: type, /) -> None:
|
|
201
|
+
"""function for custom metaclass to ensure the type is a private attributes class.
|
|
202
|
+
def ensure_type(type: type) -> None:
|
|
203
|
+
the function to ensure the type is a private attributes class `tp_getattro`, `tp_setattro` and `tp_finalizer`.
|
|
204
|
+
"""
|
|
205
|
+
...
|
|
206
|
+
def ensure_metaclass(typ: type, /) -> None:
|
|
207
|
+
"""function for custom metaclass to ensure the metaclass is working.
|
|
208
|
+
def ensure_metaclass(metaclass: type) -> None:
|
|
209
|
+
the function to ensure the metaclass `tp_getattro`, `tp_setattro` and `tp_finalizer`.
|
|
210
|
+
"""
|
|
211
|
+
...
|
|
Binary file
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Version: 1.13.1
|
|
2
|
+
Arguments: ['C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-_ko9vbhl\\cp315-win_arm64\\build\\venv\\Scripts\\delvewheel', 'repair', '-w', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-_ko9vbhl\\cp315-win_arm64\\repaired_wheel', '-v', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-_ko9vbhl\\cp315-win_arm64\\built_wheel\\private_attribute_cpp-2.1.14-cp315-cp315-win_arm64.whl']
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: private_attribute_cpp
|
|
3
|
+
Version: 2.1.14
|
|
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
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: author
|
|
13
|
+
Dynamic: author-email
|
|
14
|
+
Dynamic: description
|
|
15
|
+
Dynamic: description-content-type
|
|
16
|
+
Dynamic: home-page
|
|
17
|
+
Dynamic: license
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
Dynamic: requires-python
|
|
20
|
+
Dynamic: summary
|
|
21
|
+
|
|
22
|
+
# Private Attribute (c++ implementation)
|
|
23
|
+
|
|
24
|
+
## Introduction
|
|
25
|
+
|
|
26
|
+
This package provide a way to create the private attribute like "C++" does.
|
|
27
|
+
|
|
28
|
+
## All Base API
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from private_attribute import (PrivateAttrBase, PrivateWrapProxy) # 1 Import public API
|
|
32
|
+
|
|
33
|
+
def my_generate_func(obj_id, attr_name): # 2 Optional: custom name generator
|
|
34
|
+
return f"_hidden_{obj_id}_{attr_name}"
|
|
35
|
+
|
|
36
|
+
class MyClass(PrivateAttrBase, private_func=my_generate_func): # 3 Inherit + optional custom generator
|
|
37
|
+
__private_attrs__ = ['a', 'b', 'c', 'result', 'conflicted_name'] # 4 Must declare all private attrs
|
|
38
|
+
|
|
39
|
+
def __init__(self):
|
|
40
|
+
self.a = 1
|
|
41
|
+
self.b = 2
|
|
42
|
+
self.c = 3
|
|
43
|
+
self.result = 42 # deliberately conflicts with internal names
|
|
44
|
+
|
|
45
|
+
# Normal methods can freely access private attributes
|
|
46
|
+
def public_way(self):
|
|
47
|
+
print(self.a, self.b, self.c)
|
|
48
|
+
|
|
49
|
+
# Real-world case: method wrapped by multiple decorators
|
|
50
|
+
@PrivateWrapProxy(memoize()) # 5 Apply any decorator safely
|
|
51
|
+
@PrivateWrapProxy(login_required()) # 5 Stack as many as needed
|
|
52
|
+
@PrivateWrapProxy(rate_limit(calls=10)) # 5
|
|
53
|
+
def expensive_api_call(self, x): # First definition (will be wrapped)
|
|
54
|
+
def inner(...):
|
|
55
|
+
return some_implementation(self.a, self.b, self.c, x)
|
|
56
|
+
inner(...)
|
|
57
|
+
return heavy_computation(self.a, self.b, self.c, x)
|
|
58
|
+
|
|
59
|
+
# Fix decorator order + resolve name conflicts
|
|
60
|
+
@PrivateWrapProxy(expensive_api_call.result.name2, expensive_api_call) # 6 Chain .result to push decorators down
|
|
61
|
+
@PrivateWrapProxy(expensive_api_call.result.name1, expensive_api_call) # 6 Resolve conflict with internal names
|
|
62
|
+
def expensive_api_call(self, x): # Final real implementation
|
|
63
|
+
return heavy_computation(self.a, self.b, self.c, x)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# ====================== Usage ======================
|
|
67
|
+
obj = MyClass()
|
|
68
|
+
obj.public_way() # prints: 1 2 3
|
|
69
|
+
|
|
70
|
+
print(hasattr(obj, 'a')) # False – truly hidden from outside
|
|
71
|
+
print(obj.expensive_api_call(10)) # works with all decorators applied
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
| # | API | Purpose | Required? |
|
|
75
|
+
| --- | ---------------------------------------- | ------------------------------------------------------- | ----------- |
|
|
76
|
+
| 1 | PrivateAttrBase | Base class – must inherit | Yes |
|
|
77
|
+
| 1 | PrivateWrapProxy | Decorator wrapper for arbitrary decorators | When needed |
|
|
78
|
+
| 2 | private_func=callable | Custom hidden-name generator | Optional |
|
|
79
|
+
| 3 | Pass private_func in class definition | Same as above | Optional |
|
|
80
|
+
| 4 | \_\_private_attrs\_\_ list | Declare which attributes are private | Optional (changed in 2.1.4) |
|
|
81
|
+
| 5 | @PrivateWrapProxy(...) | Make any decorator compatible with private attributes | When needed |
|
|
82
|
+
| 6 | method.result.xxx chain + dummy wrap | Fix decorator order and name conflicts | When needed |
|
|
83
|
+
|
|
84
|
+
## Usage
|
|
85
|
+
|
|
86
|
+
This is a simple usage about the module:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from private_attribute import PrivateAttrBase
|
|
90
|
+
|
|
91
|
+
class MyClass(PrivateAttrBase):
|
|
92
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
93
|
+
def __init__(self):
|
|
94
|
+
self.a = 1
|
|
95
|
+
self.b = 2
|
|
96
|
+
self.c = 3
|
|
97
|
+
|
|
98
|
+
def public_way(self):
|
|
99
|
+
print(self.a, self.b, self.c)
|
|
100
|
+
|
|
101
|
+
obj = MyClass()
|
|
102
|
+
obj.public_way() # (1, 2, 3)
|
|
103
|
+
|
|
104
|
+
print(hasattr(obj, 'a')) # False
|
|
105
|
+
print(hasattr(obj, 'b')) # False
|
|
106
|
+
print(hasattr(obj, 'c')) # False
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
All of the attributes in `__private_attrs__` will be hidden from the outside world, and stored by another name.
|
|
110
|
+
|
|
111
|
+
You can use your function to generate the name. It needs the id of the obj and the name of the attribute:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
def my_generate_func(obj_id, attr_name):
|
|
115
|
+
return some_string
|
|
116
|
+
|
|
117
|
+
class MyClass(PrivateAttrBase, private_func=my_generate_func):
|
|
118
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
119
|
+
def __init__(self):
|
|
120
|
+
self.a = 1
|
|
121
|
+
self.b = 2
|
|
122
|
+
self.c = 3
|
|
123
|
+
|
|
124
|
+
def public_way(self):
|
|
125
|
+
print(self.a, self.b, self.c)
|
|
126
|
+
|
|
127
|
+
obj = MyClass()
|
|
128
|
+
obj.public_way() # (1, 2, 3)
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
If the method will be decorated, the `property`, `classmethod` and `staticmethod` will be supported.
|
|
133
|
+
For the other, you can use the `PrivateWrapProxy` to wrap the function:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from private_attribute import PrivateAttrBase, PrivateWrapProxy
|
|
137
|
+
|
|
138
|
+
class MyClass(PrivateAttrBase):
|
|
139
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
140
|
+
@PrivateWrapProxy(decorator1())
|
|
141
|
+
@PrivateWrapProxy(decorator2())
|
|
142
|
+
def method1(self):
|
|
143
|
+
...
|
|
144
|
+
|
|
145
|
+
@PrivateWrapProxy(method1.attr_name, method1) # Use the argument "method1" to save old func
|
|
146
|
+
def method1(self):
|
|
147
|
+
...
|
|
148
|
+
|
|
149
|
+
@PrivateWrapProxy(decorator3())
|
|
150
|
+
def method2(self):
|
|
151
|
+
...
|
|
152
|
+
|
|
153
|
+
@PrivateWrapProxy(method2.attr_name, method2) # Use the argument "method2" to save old func
|
|
154
|
+
def method2(self):
|
|
155
|
+
...
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The `PrivateWrapProxy` is a decorator, and it will wrap the function with the decorator. When it decorates the method, it returns a `_PrivateWrap` object.
|
|
161
|
+
|
|
162
|
+
The `_PrivateWrap` has the public api `result` and `funcs`. `result` returns the original decoratored result and `funcs` returns the tuple of the original functions.
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from private_attribute import PrivateAttrBase, PrivateWrapProxy
|
|
166
|
+
|
|
167
|
+
class MyClass(PrivateAttrBase):
|
|
168
|
+
__private_attrs__ = ['a', 'b', 'c']
|
|
169
|
+
@PrivateWrapProxy(decorator1())
|
|
170
|
+
@PrivateWrapProxy(decorator2())
|
|
171
|
+
def method1(self):
|
|
172
|
+
...
|
|
173
|
+
|
|
174
|
+
@PrivateWrapProxy(method1.result.conflict_attr_name1, method1) # Use the argument "method1" to save old func
|
|
175
|
+
def method1(self):
|
|
176
|
+
...
|
|
177
|
+
|
|
178
|
+
@PrivateWrapProxy(method1.result.conflict_attr_name2, method1)
|
|
179
|
+
def method1(self):
|
|
180
|
+
...
|
|
181
|
+
|
|
182
|
+
@PrivateWrapProxy(decorator3())
|
|
183
|
+
def method2(self):
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Advanced API
|
|
187
|
+
|
|
188
|
+
### define your metaclass based on one metaclass
|
|
189
|
+
|
|
190
|
+
You can define your metaclass based on one metaclass:
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
from abc import ABCMeta, abstractmethod
|
|
194
|
+
import private_attribute
|
|
195
|
+
|
|
196
|
+
class PrivateAbcMeta(ABCMeta):
|
|
197
|
+
def __new__(cls, name, bases, attrs, **kwargs):
|
|
198
|
+
temp = private_attribute.prepare(name, bases, attrs, **kwargs)
|
|
199
|
+
typ = super().__new__(cls, temp.name, temp.bases, temp.attrs, **temp.kwds)
|
|
200
|
+
private_attribute.postprocess(typ, temp)
|
|
201
|
+
return typ
|
|
202
|
+
|
|
203
|
+
private_attribute.register_metaclass(PrivateAbcMeta)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
By this way you create a metaclass both can behave as ABC and private attribute:
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
class MyClass(metaclass=PrivateAbcMeta):
|
|
210
|
+
__private_attrs__ = ()
|
|
211
|
+
__slots__ = ()
|
|
212
|
+
|
|
213
|
+
@abstractmethod
|
|
214
|
+
def my_function(self): ...
|
|
215
|
+
|
|
216
|
+
class MyImplement(MyClass):
|
|
217
|
+
__private_attrs__ = ("_a",)
|
|
218
|
+
def __init__(self, value=1):
|
|
219
|
+
self._a = value
|
|
220
|
+
|
|
221
|
+
def my_function(self):
|
|
222
|
+
return self._a
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Finally:
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
>>> a = MyImplement(1)
|
|
229
|
+
>>> a.my_function()
|
|
230
|
+
1
|
|
231
|
+
>>> a._a
|
|
232
|
+
Traceback (most recent call last):
|
|
233
|
+
File "<pyshell#2>", line 1, in <module>
|
|
234
|
+
a._a
|
|
235
|
+
AttributeError: private attribute
|
|
236
|
+
>>> MyClass()
|
|
237
|
+
Traceback (most recent call last):
|
|
238
|
+
File "<pyshell#3>", line 1, in <module>
|
|
239
|
+
MyClass()
|
|
240
|
+
TypeError: Can't instantiate abstract class MyClass without an implementation for abstract method 'my_function'
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Notes
|
|
244
|
+
|
|
245
|
+
- ~~All of the private attributes class must contain the `__private_attrs__` attribute.~~(changed in 2.1.4)
|
|
246
|
+
- The `__private_attrs__` attribute must be a sequence of strings or just one string.
|
|
247
|
+
- You cannot define the name which in `__slots__` to `__private_attrs__`.
|
|
248
|
+
- 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__`.
|
|
249
|
+
- All of the object that is the instance of the class "PrivateAttrBase" or its subclass are default to be unable to be pickled.
|
|
250
|
+
- Finally the attributes' names in `__private_attrs__` will be change to a tuple with two hash.
|
|
251
|
+
- Finally the `_PrivateWrap` object will be recoveried to the original object.
|
|
252
|
+
- Don't use a decorator which will return the `_PrivateWrap` in `PrivateWrapProxy` which will raise `TypeError`.
|
|
253
|
+
- One class defined in another class cannot use another class's private attribute.
|
|
254
|
+
- 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__`.
|
|
255
|
+
- Since 2.1.0: the code of a subclass can no longer access the private attributes of its parent classes - a parent's private attribute is only reachable from the parent's own code, or from a class that declares the same name in its own `__private_attrs__`.
|
|
256
|
+
- Since 2.1.0: if a subclass defines an attribute with the same name as a parent's private attribute, they are stored separately (instance attributes per declaring class, class-level attributes in `all_type_subclass_attr[parent][subclass]`). Class-level resolution is per-subject through the parent's code: reading the name on a subclass subject returns the subclass's own value, and the parent's own value is untouched. Such a same-name definition does NOT grant the subclass's own code access to the name.
|
|
257
|
+
- CPython may change "tp_getattro", "tp_setattro" and so on when you change the attribute "\_\_getattribute\_\_", "\_\_setattr\_\_" and so on. If you are fear about it, you can use `ensure_type` to reset those tp slots. For the other metaclasses, you can use `ensure_metaclass` to reset those tp slots. Also, don't set those methods on these classes in your code.
|
|
258
|
+
- `private_attribute.register_metaclass` must be called with the metaclass which supports weakref.
|
|
259
|
+
- Don't set `__static_attributes__` in private attribute class, or it will be removed.
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
MIT
|
|
264
|
+
|
|
265
|
+
## Requirement
|
|
266
|
+
|
|
267
|
+
This package require the c++ module "[picosha2](https://github.com/okdshin/PicoSHA2)" to compute the sha256 hash.
|
|
268
|
+
|
|
269
|
+
## Support
|
|
270
|
+
|
|
271
|
+
Now it only support Cpython.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
private_attribute.cp315-win_arm64.pyd,sha256=uxBWaSsVTwE2l4pKinAxZN5uo1y1UZ-n0VOg18cpr4k,243712
|
|
2
|
+
private_attribute.pyi,sha256=kyC8djcsqhDVmP60eLEebvQlmsUv0I5EIDAndmDFFFo,8313
|
|
3
|
+
private_attribute_cpp-2.1.14.data/platlib/msvcp140-dfb0e03a4e9979ecd6332ad9d7325a56.dll,sha256=37DgOk6ZeezWMyrZ1zJaVi-2igGnmP2IEJQh0j00uFM,1365048
|
|
4
|
+
private_attribute_cpp-2.1.14.dist-info/DELVEWHEEL,sha256=GJY7n9rEPI9_VMzju9Flo4XHyo9hPgqDcyNt5CslCG8,421
|
|
5
|
+
private_attribute_cpp-2.1.14.dist-info/METADATA,sha256=y3cG53ujM-bFmMmK_HePfQ2I4Pd8_WfMxYmwnVf-Rl4,10599
|
|
6
|
+
private_attribute_cpp-2.1.14.dist-info/RECORD,,
|
|
7
|
+
private_attribute_cpp-2.1.14.dist-info/top_level.txt,sha256=vOfJKfFO3AgjCIvyK6ppYDBTyJSsEAkf5w34knGZ3JE,19
|
|
8
|
+
private_attribute_cpp-2.1.14.dist-info/WHEEL,sha256=g1f1kqn4IVvU4p3uh1e39gJzHxbtC9umA5gNjEXIByA,101
|
|
9
|
+
private_attribute_cpp-2.1.14.dist-info/licenses/LICENSE,sha256=9CxoYH5h5JhKa_BiNrnxhtzSZXC2fUEPFmJjWryvF0g,1099
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Locked-chess-official
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|