reverse-lookup 1.0.0__py3-none-any.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.
- reverse_lookup/__init__.py +1 -0
- reverse_lookup/reverse_lookup.py +46 -0
- reverse_lookup-1.0.0.dist-info/METADATA +76 -0
- reverse_lookup-1.0.0.dist-info/RECORD +7 -0
- reverse_lookup-1.0.0.dist-info/WHEEL +5 -0
- reverse_lookup-1.0.0.dist-info/licenses/LICENSE +7 -0
- reverse_lookup-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .reverse_lookup import ReverseLookup, Inner
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from inspect import isfunction, ismemberdescriptor
|
|
2
|
+
from types import MethodType
|
|
3
|
+
|
|
4
|
+
def lookup(self: object, mro: tuple[type, ...], name: str):
|
|
5
|
+
for t in mro:
|
|
6
|
+
try:
|
|
7
|
+
attr = object.__getattribute__(t, name)
|
|
8
|
+
except AttributeError:
|
|
9
|
+
continue
|
|
10
|
+
if ismemberdescriptor(attr):
|
|
11
|
+
return attr.__get__(self, t)
|
|
12
|
+
if isfunction(attr):
|
|
13
|
+
return MethodType(attr, self)
|
|
14
|
+
return attr
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
return object.__getattribute__(self, '__dict__')[name]
|
|
18
|
+
except (AttributeError, KeyError) as e:
|
|
19
|
+
...
|
|
20
|
+
raise AttributeError(name=name, obj=self)
|
|
21
|
+
|
|
22
|
+
class ReverseLookup():
|
|
23
|
+
def __getattribute__(self, name: str):
|
|
24
|
+
mro = super().__getattribute__('__class__').mro()
|
|
25
|
+
return lookup(self, mro, name)
|
|
26
|
+
|
|
27
|
+
@classmethod
|
|
28
|
+
def mro(cls):
|
|
29
|
+
return list(reversed(cls.__mro__))
|
|
30
|
+
|
|
31
|
+
class Inner:
|
|
32
|
+
__slots__ = ('__obj__', '__start_type__')
|
|
33
|
+
|
|
34
|
+
def __init__(self, obj: object, start_type: type):
|
|
35
|
+
self.__obj__ = obj
|
|
36
|
+
self.__start_type__ = start_type
|
|
37
|
+
|
|
38
|
+
def __getattribute__(self, name: str):
|
|
39
|
+
if name in ('__obj__', '__start_type__'):
|
|
40
|
+
return super().__getattribute__(name)
|
|
41
|
+
start_type = self.__start_type__
|
|
42
|
+
obj = self.__obj__
|
|
43
|
+
mro = object.__getattribute__(obj, '__class__').mro()
|
|
44
|
+
new_mro = mro[mro.index(start_type)+1:]
|
|
45
|
+
return lookup(obj, new_mro, name)
|
|
46
|
+
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reverse-lookup
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Implementation of reversed lookup allowing classes to decide when to allow control to its subclasses
|
|
5
|
+
Author-email: Matias David Szlajen <matiasszlajen@tutamail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/Mszlajen/python-reverse-lookup
|
|
8
|
+
Keywords: metaprogramming,lookup,mro,classes
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# Reverse Lookup
|
|
17
|
+
|
|
18
|
+
Implementation of a reverse lookup similar to [Beta](https://beta.cs.au.dk/) where parent classes have priority and decide when to allow the subclasses (or the instance) to extend its behaviour.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Reverse Lookup is made using pure python metaprogamming interfaces so it can be installed easily from pypi
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
python -m pip install reverse_lookup
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Using Reverse Lookup is as simple as inhereting a class.
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from reverse_lookup import ReverseLookup
|
|
34
|
+
|
|
35
|
+
class A(ReverseLookup):
|
|
36
|
+
...
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Now all instances of `A` will have a lookup that looks like `object -> A -> <A object>`.
|
|
40
|
+
This works for subclasses too.
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
class A(ReverseLookup):
|
|
44
|
+
def m(self):
|
|
45
|
+
return 'A'
|
|
46
|
+
|
|
47
|
+
class B(A):
|
|
48
|
+
def m(self):
|
|
49
|
+
return 'B'
|
|
50
|
+
|
|
51
|
+
B().m() # => 'A'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
To allow subclasses to extend the logic you can use `Inner`.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from reverse_lookup import ReverseLookup, Inner
|
|
58
|
+
|
|
59
|
+
class A(ReverseLookup):
|
|
60
|
+
def m(self):
|
|
61
|
+
return 'A' + Inner(self, A).m()
|
|
62
|
+
|
|
63
|
+
class B(A):
|
|
64
|
+
def m(self):
|
|
65
|
+
return 'B'
|
|
66
|
+
|
|
67
|
+
B().m() #=> 'AB'
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Like the built-in `super`, `Inner` can be send any message and it will do a lookup for it starting at the step following the class recived as parameter in the mro, it will fail with `AttributeError` if there is not a next implementation and special methods need to be called explicitly.
|
|
71
|
+
|
|
72
|
+
Since python doesn't allow monkey patching of standard classes (ex. object), special method such as `__str__ ` still use the all lookup.
|
|
73
|
+
|
|
74
|
+
## Motivation
|
|
75
|
+
|
|
76
|
+
We mentioned ina class that this was posible using metaprogramming and I though it was a fun project.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
reverse_lookup/__init__.py,sha256=V9undre_eyaQlop-BkGT485zZpoGbuUR24X-Sl79bC0,48
|
|
2
|
+
reverse_lookup/reverse_lookup.py,sha256=Htp1-3i-1lk8quomz_1h2nxsyGcLV2IfwQDMNgA2Lvo,1431
|
|
3
|
+
reverse_lookup-1.0.0.dist-info/licenses/LICENSE,sha256=96q385yfsODXvGdRwyBES-BNqN4xT5KdzxynGtGdtb8,1075
|
|
4
|
+
reverse_lookup-1.0.0.dist-info/METADATA,sha256=_PmsUNqYZo8U2qxMiEmG49YZ3KnfjwgnWn7pviipmVg,2287
|
|
5
|
+
reverse_lookup-1.0.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
6
|
+
reverse_lookup-1.0.0.dist-info/top_level.txt,sha256=dxgEm-kfnrU6YuvgMpnTUYfQZd9ZeHGO9z-z2qMAL4M,15
|
|
7
|
+
reverse_lookup-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Matias David Szlajen
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
reverse_lookup
|