reverse-lookup 1.0.0__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.
@@ -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,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,61 @@
1
+ # Reverse Lookup
2
+
3
+ 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.
4
+
5
+ ## Installation
6
+
7
+ Reverse Lookup is made using pure python metaprogamming interfaces so it can be installed easily from pypi
8
+
9
+ ```bash
10
+ python -m pip install reverse_lookup
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Using Reverse Lookup is as simple as inhereting a class.
16
+
17
+ ```python
18
+ from reverse_lookup import ReverseLookup
19
+
20
+ class A(ReverseLookup):
21
+ ...
22
+ ```
23
+
24
+ Now all instances of `A` will have a lookup that looks like `object -> A -> <A object>`.
25
+ This works for subclasses too.
26
+
27
+ ```python
28
+ class A(ReverseLookup):
29
+ def m(self):
30
+ return 'A'
31
+
32
+ class B(A):
33
+ def m(self):
34
+ return 'B'
35
+
36
+ B().m() # => 'A'
37
+ ```
38
+
39
+ To allow subclasses to extend the logic you can use `Inner`.
40
+
41
+ ```python
42
+ from reverse_lookup import ReverseLookup, Inner
43
+
44
+ class A(ReverseLookup):
45
+ def m(self):
46
+ return 'A' + Inner(self, A).m()
47
+
48
+ class B(A):
49
+ def m(self):
50
+ return 'B'
51
+
52
+ B().m() #=> 'AB'
53
+ ```
54
+
55
+ 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.
56
+
57
+ Since python doesn't allow monkey patching of standard classes (ex. object), special method such as `__str__ ` still use the all lookup.
58
+
59
+ ## Motivation
60
+
61
+ We mentioned ina class that this was posible using metaprogramming and I though it was a fun project.
@@ -0,0 +1,22 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "reverse-lookup"
7
+ version = "1.0.0"
8
+ description = "Implementation of reversed lookup allowing classes to decide when to allow control to its subclasses"
9
+ readme = "README.md"
10
+ authors = [{name = "Matias David Szlajen", email = "matiasszlajen@tutamail.com" }]
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ classifiers = [
14
+ "Programming Language :: Python",
15
+ "Programming Language :: Python :: 3",
16
+ ]
17
+ keywords = ["metaprogramming", "lookup", "mro", "classes"]
18
+ dependencies = []
19
+ requires-python = ">=3.9"
20
+
21
+ [project.urls]
22
+ Repository = "https://github.com/Mszlajen/python-reverse-lookup"
@@ -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,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ reverse_lookup/__init__.py
5
+ reverse_lookup/reverse_lookup.py
6
+ reverse_lookup.egg-info/PKG-INFO
7
+ reverse_lookup.egg-info/SOURCES.txt
8
+ reverse_lookup.egg-info/dependency_links.txt
9
+ reverse_lookup.egg-info/top_level.txt
10
+ tests/test.py
@@ -0,0 +1 @@
1
+ reverse_lookup
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,102 @@
1
+ import unittest
2
+ from reverse_lookup import ReverseLookup, Inner
3
+
4
+ class ReverseLookUpTest(unittest.TestCase):
5
+ def test_A_to_B_use_A(self):
6
+ class A(ReverseLookup):
7
+ def m(self):
8
+ return 'A'
9
+
10
+ class B(A):
11
+ def m(self):
12
+ return 'B'
13
+
14
+ self.assertEqual(B().m(), 'A')
15
+
16
+ def test_A_to_B_uses_inner(self):
17
+ class A(ReverseLookup):
18
+ def m(self):
19
+ return 'A' + Inner(self, A).m()
20
+
21
+ class B(A):
22
+ def m(self):
23
+ return 'B'
24
+
25
+ self.assertEqual(B().m(), 'AB')
26
+
27
+ def test_A_uses_inner_without_implementation(self):
28
+ class A(ReverseLookup):
29
+ def m(self):
30
+ return 'A' + Inner(self, A).m()
31
+
32
+ with self.assertRaises(AttributeError):
33
+ A().m()
34
+
35
+ @unittest.skip("Will not implement")
36
+ def test_B_uses_reversed_when_accessing_from_class(self):
37
+ class A(ReverseLookup):
38
+ def m(self) -> str:
39
+ return 'A'
40
+
41
+ class B(A):
42
+ def m(self):
43
+ return 'B'
44
+
45
+ self.assertEqual(B.m(B()), 'A')
46
+
47
+ def test_B_has_a_reversed_mro(self):
48
+ class A(ReverseLookup):
49
+ ...
50
+
51
+ class B(A):
52
+ ...
53
+
54
+ self.assertListEqual(B.mro(), [object, ReverseLookup, A, B])
55
+
56
+ def test_A_uses_its_own_to_string(self):
57
+ class A(ReverseLookup):
58
+ def __str__(self):
59
+ return 'A'
60
+
61
+ self.assertEqual(str(A()), 'A')
62
+
63
+ def test_attributes_in_object_are_found(self):
64
+ class A(ReverseLookup):
65
+ ...
66
+
67
+ a = A()
68
+ a.foo = "Bar"
69
+
70
+ self.assertEqual(a.foo, "Bar")
71
+
72
+ def test_attributes_in_object_from_class_are_found(self):
73
+ class A(ReverseLookup):
74
+ def __init__(self):
75
+ self.foo = "Bar"
76
+ a = A()
77
+ self.assertEqual(a.foo, "Bar")
78
+
79
+ def test_attributes_in_object_from_slots_are_found(self):
80
+ class A(ReverseLookup):
81
+ __slots__ = ('foo', )
82
+
83
+ a = A()
84
+ a.foo = "Bar"
85
+ self.assertEqual(a.foo, "Bar")
86
+
87
+ def test_attributes_from_class_override_object(self):
88
+ class A(ReverseLookup):
89
+ foo = "Bar"
90
+
91
+ a = A()
92
+ a.foo = "Zas"
93
+ self.assertEqual(a.foo, "Bar")
94
+
95
+ def test_inner_works_with_instances(self):
96
+ class A(ReverseLookup):
97
+ def m(self):
98
+ return 'A' + Inner(self, A).m()
99
+
100
+ a = A()
101
+ a.m = lambda: "a"
102
+ self.assertEqual(a.m(), "Aa")