reslot 0.1__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.1/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2025 Zachary Avram Spector
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
reslot-0.1/PKG-INFO ADDED
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: reslot
3
+ Version: 0.1
4
+ Summary: Class decorator to enable @cached_property on classes with __slots__
5
+ Author-email: Zachary Spector <public@zacharyspector.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://codeberg.org/clayote/reslot
8
+ Project-URL: Bug tracker, https://codeberg.org/clayote/reslot/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Software Development :: Libraries
12
+ Classifier: Development Status :: 2 - Pre-Alpha
13
+ Requires-Python: ~=3.12
14
+ License-File: LICENSE
15
+ Dynamic: license-file
reslot-0.1/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # reslot
2
+
3
+
4
+ A class decorator that lets you use `@cached_property` on classes with `__slots__`.
5
+
6
+ The decorated class needs a `__dict__` slot, but there won't exactly be a
7
+ dictionary in it. Instead, `@reslot` will give it a custom `MutableMapping`
8
+ with *its own* slots. `@cached_property` will keep its cache there.
9
+
@@ -0,0 +1,20 @@
1
+ [project]
2
+ name = "reslot"
3
+ version = "0.1"
4
+ authors = [
5
+ {name="Zachary Spector",email="public@zacharyspector.com"},
6
+ ]
7
+ description="Class decorator to enable @cached_property on classes with __slots__"
8
+ license = "MIT"
9
+ license-files = ["LICENSE"]
10
+ requires-python = "~=3.12"
11
+ classifiers = [
12
+ "Programming Language :: Python :: 3",
13
+ "Operating System :: OS Independent",
14
+ "Topic :: Software Development :: Libraries",
15
+ "Development Status :: 2 - Pre-Alpha"
16
+ ]
17
+
18
+ [project.urls]
19
+ "Homepage" = "https://codeberg.org/clayote/reslot"
20
+ "Bug tracker" = "https://codeberg.org/clayote/reslot/issues"
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: reslot
3
+ Version: 0.1
4
+ Summary: Class decorator to enable @cached_property on classes with __slots__
5
+ Author-email: Zachary Spector <public@zacharyspector.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://codeberg.org/clayote/reslot
8
+ Project-URL: Bug tracker, https://codeberg.org/clayote/reslot/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Software Development :: Libraries
12
+ Classifier: Development Status :: 2 - Pre-Alpha
13
+ Requires-Python: ~=3.12
14
+ License-File: LICENSE
15
+ Dynamic: license-file
@@ -0,0 +1,8 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ reslot.py
5
+ reslot.egg-info/PKG-INFO
6
+ reslot.egg-info/SOURCES.txt
7
+ reslot.egg-info/dependency_links.txt
8
+ reslot.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ reslot
reslot-0.1/reslot.py ADDED
@@ -0,0 +1,133 @@
1
+ from collections.abc import MutableMapping
2
+ from functools import wraps, cached_property
3
+ from inspect import getclasstree
4
+ from typing import TypeVar
5
+
6
+ _UNSET = object()
7
+
8
+ _T = TypeVar("_T")
9
+
10
+
11
+ def collect_all_slots(cls) -> set[str]:
12
+ def recurse(l: list[type | tuple[type, type] | list]):
13
+ if isinstance(l, type):
14
+ yield l
15
+ elif isinstance(l, tuple):
16
+ yield l[0]
17
+ else:
18
+ for ll in l:
19
+ yield from recurse(ll)
20
+
21
+ slots = set()
22
+ if hasattr(cls, "__slots__"):
23
+ slots.update(cls.__slots__)
24
+ for clls in recurse(getclasstree([cls])):
25
+ if hasattr(clls, "__slots__"):
26
+ slots.update(clls.__slots__)
27
+ return slots
28
+
29
+
30
+ def surround_name(prefix: str | None, name: str, suffix: str | None) -> str:
31
+ if prefix is None:
32
+ if name.startswith("_"):
33
+ prefix = ""
34
+ else:
35
+ prefix = "_"
36
+ if suffix is None:
37
+ if prefix:
38
+ suffix = ""
39
+ else:
40
+ suffix = "_"
41
+ return prefix + name + suffix
42
+
43
+
44
+ def reslot(
45
+ cls: type[_T] | None = None,
46
+ prefix: str | None = None,
47
+ suffix: str | None = None,
48
+ ) -> type[_T]:
49
+ """Class decorator to enable ``@cached_property`` with ``__slots__``
50
+
51
+ The decorated class needs a ``__dict__`` slot, but we won't put an actual
52
+ dictionary in it. Instead, we'll put a mapping with the needed slots in it.
53
+
54
+ :param cls: The class to enable ``@cached_property`` on. If left ``None``,
55
+ a partial function will be returned, which may then be used as a
56
+ decorator.
57
+ :param prefix: String to put at the beginning of the slot name. Defaults
58
+ to ``None``, in which case we'll prefix the slot name with an underscore
59
+ unless it already starts with one. We won't default to a prefix then,
60
+ because that would invoke name mangling.
61
+ :param suffix: String to put at the end of the slot name. Defaults to
62
+ ``None``, in which case we won't use a suffix, unless ``prefix`` is
63
+ also ``None`` or the empty string, in which case the default suffix is
64
+ ``"_"``.
65
+
66
+ """
67
+
68
+ def really_reslot(cls: type[_T]) -> type[_T]:
69
+ if not hasattr(cls, "__slots__"):
70
+ raise TypeError("Class doesn't have __slots__")
71
+ slots = collect_all_slots(cls)
72
+ if "__dict__" not in slots:
73
+ raise TypeError("Need __dict__ slot")
74
+ slots.remove("__dict__")
75
+
76
+ class SlotRedirector(MutableMapping):
77
+ __slots__ = slots
78
+
79
+ def __setitem__(self, key, value, /):
80
+ try:
81
+ setattr(self, key, value)
82
+ except AttributeError:
83
+ raise KeyError("No such slot", key) from None
84
+
85
+ def __delitem__(self, key, /):
86
+ try:
87
+ delattr(self, key)
88
+ except AttributeError:
89
+ raise KeyError("Slot not set", key)
90
+
91
+ def __getitem__(self, key, /):
92
+ try:
93
+ return getattr(self, key)
94
+ except AttributeError:
95
+ raise KeyError("Slot not set", key)
96
+
97
+ def __len__(self):
98
+ return len(self.__slots__)
99
+
100
+ def __iter__(self):
101
+ return iter(self.__slots__)
102
+
103
+ if hasattr(cls, "__getattr__"):
104
+ core_getattr = cls.__getattr__
105
+
106
+ @wraps(core_getattr)
107
+ def __getattr__(self: _T, attr: str):
108
+ if attr == "__dict__":
109
+ val = getattr(super(cls, self), "__dict__", _UNSET)
110
+ if val is _UNSET:
111
+ val = SlotRedirector()
112
+ setattr(self, "__dict__", val)
113
+ return val
114
+ return core_getattr(self, attr)
115
+
116
+ cls.__getattr__ = __getattr__
117
+ else:
118
+
119
+ def __getattr__(self: _T, attr: str):
120
+ if attr == "__dict__":
121
+ val = getattr(super(cls, self), "__dict__", _UNSET)
122
+ if val is _UNSET:
123
+ val = SlotRedirector()
124
+ setattr(self, "__dict__", val)
125
+ return val
126
+ return getattr(super(type(self), self), attr)
127
+
128
+ cls.__getattr__ = __getattr__
129
+ return cls
130
+
131
+ if cls is None:
132
+ return really_reslot
133
+ return really_reslot(cls)
reslot-0.1/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+