reslot 0.7__tar.gz → 0.9__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 reslot might be problematic. Click here for more details.
- {reslot-0.7 → reslot-0.9}/PKG-INFO +2 -2
- {reslot-0.7 → reslot-0.9}/pyproject.toml +2 -2
- {reslot-0.7 → reslot-0.9}/reslot.egg-info/PKG-INFO +2 -2
- reslot-0.9/reslot.py +109 -0
- reslot-0.7/reslot.py +0 -109
- {reslot-0.7 → reslot-0.9}/LICENSE +0 -0
- {reslot-0.7 → reslot-0.9}/README.md +0 -0
- {reslot-0.7 → reslot-0.9}/reslot.egg-info/SOURCES.txt +0 -0
- {reslot-0.7 → reslot-0.9}/reslot.egg-info/dependency_links.txt +0 -0
- {reslot-0.7 → reslot-0.9}/reslot.egg-info/top_level.txt +0 -0
- {reslot-0.7 → reslot-0.9}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: reslot
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.9
|
|
4
4
|
Summary: Class decorator to enable @cached_property on classes with __slots__
|
|
5
5
|
Author-email: Zachary Spector <public@zacharyspector.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -9,7 +9,7 @@ Project-URL: Bug tracker, https://codeberg.org/clayote/reslot/issues
|
|
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
|
10
10
|
Classifier: Operating System :: OS Independent
|
|
11
11
|
Classifier: Topic :: Software Development :: Libraries
|
|
12
|
-
Classifier: Development Status ::
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
13
|
Requires-Python: ~=3.8
|
|
14
14
|
Description-Content-Type: text/markdown
|
|
15
15
|
License-File: LICENSE
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "reslot"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.9"
|
|
8
8
|
authors = [
|
|
9
9
|
{name="Zachary Spector",email="public@zacharyspector.com"},
|
|
10
10
|
]
|
|
@@ -17,7 +17,7 @@ classifiers = [
|
|
|
17
17
|
"Programming Language :: Python :: 3",
|
|
18
18
|
"Operating System :: OS Independent",
|
|
19
19
|
"Topic :: Software Development :: Libraries",
|
|
20
|
-
"Development Status ::
|
|
20
|
+
"Development Status :: 3 - Alpha"
|
|
21
21
|
]
|
|
22
22
|
|
|
23
23
|
[project.urls]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: reslot
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.9
|
|
4
4
|
Summary: Class decorator to enable @cached_property on classes with __slots__
|
|
5
5
|
Author-email: Zachary Spector <public@zacharyspector.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -9,7 +9,7 @@ Project-URL: Bug tracker, https://codeberg.org/clayote/reslot/issues
|
|
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
|
10
10
|
Classifier: Operating System :: OS Independent
|
|
11
11
|
Classifier: Topic :: Software Development :: Libraries
|
|
12
|
-
Classifier: Development Status ::
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
13
|
Requires-Python: ~=3.8
|
|
14
14
|
Description-Content-Type: text/markdown
|
|
15
15
|
License-File: LICENSE
|
reslot-0.9/reslot.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from collections.abc import MutableMapping
|
|
2
|
+
from functools import cached_property, partial, update_wrapper
|
|
3
|
+
from inspect import getclasstree, getdoc
|
|
4
|
+
from typing import (Callable, Dict, TypeVar, Iterator, Type, Optional, Set,
|
|
5
|
+
Tuple, Union, List)
|
|
6
|
+
|
|
7
|
+
_UNSET = object()
|
|
8
|
+
|
|
9
|
+
_T = TypeVar("_T")
|
|
10
|
+
|
|
11
|
+
walk_class_tree_hint = List[Union[Type, Tuple[Type, Type], "walk_class_tree_hint"]]
|
|
12
|
+
|
|
13
|
+
def walk_class_tree(l: walk_class_tree_hint) -> Iterator[Type]:
|
|
14
|
+
if isinstance(l, type):
|
|
15
|
+
yield l
|
|
16
|
+
elif isinstance(l, tuple):
|
|
17
|
+
yield l[0]
|
|
18
|
+
if len(l) > 1:
|
|
19
|
+
yield from walk_class_tree(l[1])
|
|
20
|
+
else:
|
|
21
|
+
for ll in l:
|
|
22
|
+
yield from walk_class_tree(ll)
|
|
23
|
+
|
|
24
|
+
def collect_all_slots(cls: Type) -> Set[str]:
|
|
25
|
+
slots = set()
|
|
26
|
+
if hasattr(cls, "__slots__"):
|
|
27
|
+
slots.update(cls.__slots__)
|
|
28
|
+
for clls in walk_class_tree(getclasstree([cls])):
|
|
29
|
+
if hasattr(clls, "__slots__"):
|
|
30
|
+
slots.update(clls.__slots__)
|
|
31
|
+
return slots
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def iter_needed_slots(cls: Type) -> Iterator[Tuple[str, Optional[str]]]:
|
|
35
|
+
for clls in walk_class_tree([cls]):
|
|
36
|
+
for attr in dir(clls):
|
|
37
|
+
try:
|
|
38
|
+
val = getattr(clls, attr)
|
|
39
|
+
except AttributeError:
|
|
40
|
+
continue
|
|
41
|
+
if not isinstance(val, cached_property):
|
|
42
|
+
continue
|
|
43
|
+
if val.attrname is None:
|
|
44
|
+
raise TypeError("cached_property without name")
|
|
45
|
+
yield val.attrname, getdoc(val.func)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def init(slot_dict: Dict[str, Optional[str]], core_init: Optional[Callable],
|
|
49
|
+
self, *args, **kwargs):
|
|
50
|
+
class SlotRedirector(MutableMapping, dict):
|
|
51
|
+
# I think inheriting from dict means we still have an empty
|
|
52
|
+
# dictionary floating in memory for each SlotRedirector instantiated.
|
|
53
|
+
# That's eighty bytes we don't need...but at least it doesn't grow.
|
|
54
|
+
__slots__ = slot_dict
|
|
55
|
+
|
|
56
|
+
def __setitem__(self, key, value, /):
|
|
57
|
+
try:
|
|
58
|
+
setattr(self, key, value)
|
|
59
|
+
except AttributeError:
|
|
60
|
+
raise KeyError("No such slot", key) from None
|
|
61
|
+
|
|
62
|
+
def __delitem__(self, key, /):
|
|
63
|
+
try:
|
|
64
|
+
delattr(self, key)
|
|
65
|
+
except AttributeError:
|
|
66
|
+
raise KeyError("Slot not set", key)
|
|
67
|
+
|
|
68
|
+
def __getitem__(self, key, /):
|
|
69
|
+
try:
|
|
70
|
+
return getattr(self, key)
|
|
71
|
+
except AttributeError:
|
|
72
|
+
raise KeyError("Slot not set", key)
|
|
73
|
+
|
|
74
|
+
def __len__(self):
|
|
75
|
+
return len(self.__slots__)
|
|
76
|
+
|
|
77
|
+
def __iter__(self):
|
|
78
|
+
return iter(self.__slots__)
|
|
79
|
+
self.__dict__ = SlotRedirector()
|
|
80
|
+
if callable(core_init):
|
|
81
|
+
core_init(self, *args, **kwargs)
|
|
82
|
+
else:
|
|
83
|
+
super(type(self), self).__init__(*args, **kwargs)
|
|
84
|
+
|
|
85
|
+
def reslot(cls: Type[_T]) -> Type[_T]:
|
|
86
|
+
"""Class decorator to enable ``@cached_property`` with ``__slots__``
|
|
87
|
+
|
|
88
|
+
The decorated class needs a ``__dict__`` slot, but we won't put an actual
|
|
89
|
+
dictionary in it. Instead, we'll put a mapping with the needed slots in it.
|
|
90
|
+
"""
|
|
91
|
+
if not hasattr(cls, "__slots__"):
|
|
92
|
+
raise TypeError("Class doesn't have __slots__")
|
|
93
|
+
slots = collect_all_slots(cls)
|
|
94
|
+
if "__dict__" not in slots:
|
|
95
|
+
raise TypeError("Need __dict__ slot")
|
|
96
|
+
slot_dict = dict(iter_needed_slots(cls))
|
|
97
|
+
if hasattr(cls, "__slots__"):
|
|
98
|
+
if isinstance(cls.__slots__, dict):
|
|
99
|
+
slot_dict.update(cls.__slots__)
|
|
100
|
+
else:
|
|
101
|
+
for slot in cls.__slots__:
|
|
102
|
+
slot_dict[slot] = None
|
|
103
|
+
if hasattr(cls, "__init__"):
|
|
104
|
+
part = partial(init, slot_dict, cls.__init__)
|
|
105
|
+
update_wrapper(part, cls.__init__)
|
|
106
|
+
else:
|
|
107
|
+
part = partial(init, slot_dict, None)
|
|
108
|
+
cls.__init__ = part
|
|
109
|
+
return cls
|
reslot-0.7/reslot.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import builtins
|
|
2
|
-
from collections.abc import MutableMapping
|
|
3
|
-
from functools import wraps, cached_property
|
|
4
|
-
from inspect import getclasstree, getdoc
|
|
5
|
-
from typing import TypeVar, Iterator, Type, Optional, Set, Tuple
|
|
6
|
-
|
|
7
|
-
_UNSET = object()
|
|
8
|
-
|
|
9
|
-
_T = TypeVar("_T")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def collect_all_slots(cls) -> Set[str]:
|
|
13
|
-
def recurse(l):
|
|
14
|
-
if isinstance(l, type):
|
|
15
|
-
yield l
|
|
16
|
-
elif isinstance(l, tuple):
|
|
17
|
-
yield l[0]
|
|
18
|
-
yield from map(recurse, l[1])
|
|
19
|
-
else:
|
|
20
|
-
for ll in l:
|
|
21
|
-
yield from recurse(ll)
|
|
22
|
-
|
|
23
|
-
slots = set()
|
|
24
|
-
if hasattr(cls, "__slots__"):
|
|
25
|
-
slots.update(cls.__slots__)
|
|
26
|
-
for clls in recurse(getclasstree([cls])):
|
|
27
|
-
if hasattr(clls, "__slots__"):
|
|
28
|
-
slots.update(clls.__slots__)
|
|
29
|
-
return slots
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def iter_needed_slots(cls: Type) -> Iterator[Tuple[str, str]]:
|
|
33
|
-
for attr in dir(cls):
|
|
34
|
-
try:
|
|
35
|
-
val = getattr(cls, attr)
|
|
36
|
-
except AttributeError:
|
|
37
|
-
continue
|
|
38
|
-
if not isinstance(val, cached_property):
|
|
39
|
-
continue
|
|
40
|
-
if val.attrname is None:
|
|
41
|
-
raise TypeError("cached_property without name")
|
|
42
|
-
yield val.attrname, getdoc(val.func)
|
|
43
|
-
|
|
44
|
-
def reslot(
|
|
45
|
-
cls: Optional[Type[_T]] = None,
|
|
46
|
-
) -> Type[_T]:
|
|
47
|
-
"""Class decorator to enable ``@cached_property`` with ``__slots__``
|
|
48
|
-
|
|
49
|
-
The decorated class needs a ``__dict__`` slot, but we won't put an actual
|
|
50
|
-
dictionary in it. Instead, we'll put a mapping with the needed slots in it.
|
|
51
|
-
"""
|
|
52
|
-
|
|
53
|
-
def really_reslot(cls: Type[_T]) -> Type[_T]:
|
|
54
|
-
if not hasattr(cls, "__slots__"):
|
|
55
|
-
raise TypeError("Class doesn't have __slots__")
|
|
56
|
-
slots = collect_all_slots(cls)
|
|
57
|
-
if "__dict__" not in slots:
|
|
58
|
-
raise TypeError("Need __dict__ slot")
|
|
59
|
-
|
|
60
|
-
class SlotRedirector(MutableMapping, dict):
|
|
61
|
-
# I think inheriting from dict means we still have an empty
|
|
62
|
-
# dictionary floating in memory for each SlotRedirector instantiated.
|
|
63
|
-
# That's eighty bytes we don't need...but at least it doesn't grow.
|
|
64
|
-
__slots__ = dict(iter_needed_slots(cls))
|
|
65
|
-
|
|
66
|
-
def __setitem__(self, key, value, /):
|
|
67
|
-
try:
|
|
68
|
-
setattr(self, key, value)
|
|
69
|
-
except AttributeError:
|
|
70
|
-
raise KeyError("No such slot", key) from None
|
|
71
|
-
|
|
72
|
-
def __delitem__(self, key, /):
|
|
73
|
-
try:
|
|
74
|
-
delattr(self, key)
|
|
75
|
-
except AttributeError:
|
|
76
|
-
raise KeyError("Slot not set", key)
|
|
77
|
-
|
|
78
|
-
def __getitem__(self, key, /):
|
|
79
|
-
try:
|
|
80
|
-
return getattr(self, key)
|
|
81
|
-
except AttributeError:
|
|
82
|
-
raise KeyError("Slot not set", key)
|
|
83
|
-
|
|
84
|
-
def __len__(self):
|
|
85
|
-
return len(self.__slots__)
|
|
86
|
-
|
|
87
|
-
def __iter__(self):
|
|
88
|
-
return iter(self.__slots__)
|
|
89
|
-
|
|
90
|
-
if hasattr(cls, "__init__"):
|
|
91
|
-
core__init__ = cls.__init__
|
|
92
|
-
|
|
93
|
-
@wraps(core__init__)
|
|
94
|
-
def __init__(self, *args, **kwargs):
|
|
95
|
-
self.__dict__ = SlotRedirector()
|
|
96
|
-
core__init__(self, *args, **kwargs)
|
|
97
|
-
|
|
98
|
-
cls.__init__ = __init__
|
|
99
|
-
else:
|
|
100
|
-
def __init__(self, *args, **kwargs):
|
|
101
|
-
self.__dict__ = SlotRedirector()
|
|
102
|
-
super(cls, self).__init__(*args, **kwargs)
|
|
103
|
-
|
|
104
|
-
cls.__init__ = __init__
|
|
105
|
-
return cls
|
|
106
|
-
|
|
107
|
-
if cls is None:
|
|
108
|
-
return really_reslot
|
|
109
|
-
return really_reslot(cls)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|