parent-lookup 0.1.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.
- parent_lookup-0.1.0/.coveragerc +13 -0
- parent_lookup-0.1.0/.editorconfig +15 -0
- parent_lookup-0.1.0/.gitignore +160 -0
- parent_lookup-0.1.0/.pre-commit-config.yaml +20 -0
- parent_lookup-0.1.0/LICENSE +21 -0
- parent_lookup-0.1.0/PKG-INFO +254 -0
- parent_lookup-0.1.0/README.md +202 -0
- parent_lookup-0.1.0/pyproject.toml +161 -0
- parent_lookup-0.1.0/pytest.ini +5 -0
- parent_lookup-0.1.0/ruff.toml +158 -0
- parent_lookup-0.1.0/src/parent_lookup/__init__.py +13 -0
- parent_lookup-0.1.0/src/parent_lookup/lookup.py +352 -0
- parent_lookup-0.1.0/src/parent_lookup/py.typed +0 -0
- parent_lookup-0.1.0/src/parent_lookup/utils/__init__.py +1 -0
- parent_lookup-0.1.0/src/parent_lookup/utils/logging.py +67 -0
- parent_lookup-0.1.0/tests/.gitignore +2 -0
- parent_lookup-0.1.0/tests/__init__.py +0 -0
- parent_lookup-0.1.0/tests/conftest.py +72 -0
- parent_lookup-0.1.0/tests/lookup/child_module.py +24 -0
- parent_lookup-0.1.0/tests/lookup/child_module_with_future_annotations.py +27 -0
- parent_lookup-0.1.0/tests/lookup/custom_base_module.py +20 -0
- parent_lookup-0.1.0/tests/lookup/custom_child_module.py +28 -0
- parent_lookup-0.1.0/tests/lookup/custom_parent_module.py +32 -0
- parent_lookup-0.1.0/tests/lookup/parent_module.py +45 -0
- parent_lookup-0.1.0/tests/lookup/parent_module_with_future_annotations.py +20 -0
- parent_lookup-0.1.0/tests/lookup/test_lookup.py +267 -0
- parent_lookup-0.1.0/tests/test_working_directory/.gitignore +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# EditorConfig is awesome: https://EditorConfig.org
|
|
2
|
+
|
|
3
|
+
# top-most EditorConfig file
|
|
4
|
+
root = true
|
|
5
|
+
|
|
6
|
+
[*]
|
|
7
|
+
indent_style = space
|
|
8
|
+
indent_size = 4
|
|
9
|
+
charset = utf-8
|
|
10
|
+
trim_trailing_whitespace = true
|
|
11
|
+
insert_final_newline = true
|
|
12
|
+
|
|
13
|
+
[*.{yml,yaml}]
|
|
14
|
+
indent_style = space
|
|
15
|
+
indent_size = 2
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
pip-wheel-metadata/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.tox/
|
|
43
|
+
.nox/
|
|
44
|
+
.coverage
|
|
45
|
+
.coverage.*
|
|
46
|
+
.cache
|
|
47
|
+
nosetests.xml
|
|
48
|
+
coverage.xml
|
|
49
|
+
*.cover
|
|
50
|
+
*.py,cover
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
_autosummary
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
.python-version
|
|
89
|
+
|
|
90
|
+
# uv
|
|
91
|
+
# It is generally recommended to include `uv.lock` in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, `uv` might install dependencies in one environment that don't work in another.
|
|
94
|
+
# In such case, `uv.lock` should be added to `.gitignore`
|
|
95
|
+
uv.lock
|
|
96
|
+
|
|
97
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
98
|
+
__pypackages__/
|
|
99
|
+
|
|
100
|
+
# Celery stuff
|
|
101
|
+
celerybeat-schedule
|
|
102
|
+
celerybeat.pid
|
|
103
|
+
|
|
104
|
+
# SageMath parsed files
|
|
105
|
+
*.sage.py
|
|
106
|
+
|
|
107
|
+
# Environments
|
|
108
|
+
.env
|
|
109
|
+
.venv
|
|
110
|
+
env/
|
|
111
|
+
venv/
|
|
112
|
+
ENV/
|
|
113
|
+
env.bak/
|
|
114
|
+
venv.bak/
|
|
115
|
+
|
|
116
|
+
# Spyder project settings
|
|
117
|
+
.spyderproject
|
|
118
|
+
.spyproject
|
|
119
|
+
|
|
120
|
+
# Rope project settings
|
|
121
|
+
.ropeproject
|
|
122
|
+
|
|
123
|
+
# mkdocs documentation
|
|
124
|
+
/site
|
|
125
|
+
|
|
126
|
+
# mypy
|
|
127
|
+
.mypy_cache/
|
|
128
|
+
.dmypy.json
|
|
129
|
+
dmypy.json
|
|
130
|
+
|
|
131
|
+
# Pyre type checker
|
|
132
|
+
.pyre/
|
|
133
|
+
|
|
134
|
+
# pytype static type analyzer
|
|
135
|
+
.pytype/
|
|
136
|
+
|
|
137
|
+
# Cython debug symbols
|
|
138
|
+
cython_debug/
|
|
139
|
+
|
|
140
|
+
# Ruff
|
|
141
|
+
.ruff_cache
|
|
142
|
+
|
|
143
|
+
# PyCharm
|
|
144
|
+
.idea
|
|
145
|
+
|
|
146
|
+
# VS Code
|
|
147
|
+
.vscode/*
|
|
148
|
+
!.vscode/settings.json
|
|
149
|
+
!.vscode/tasks.json
|
|
150
|
+
!.vscode/launch.json
|
|
151
|
+
!.vscode/extensions.json
|
|
152
|
+
!.vscode/*.code-snippets
|
|
153
|
+
|
|
154
|
+
# Inside /demos folder: ignore temporary logs, db's and local data files
|
|
155
|
+
demos/**/*.log
|
|
156
|
+
demos/**/*.db
|
|
157
|
+
demos/**/*.nc
|
|
158
|
+
|
|
159
|
+
# Cmake generated files
|
|
160
|
+
CMakeUserPresets.json
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
3
|
+
rev: v5.0.0
|
|
4
|
+
hooks:
|
|
5
|
+
- id: mixed-line-ending
|
|
6
|
+
args: [--fix=auto]
|
|
7
|
+
- id: trailing-whitespace
|
|
8
|
+
- id: check-yaml
|
|
9
|
+
- id: check-toml
|
|
10
|
+
- id: check-merge-conflict
|
|
11
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
12
|
+
rev: v0.9.2
|
|
13
|
+
hooks:
|
|
14
|
+
- id: ruff-format
|
|
15
|
+
- id: ruff
|
|
16
|
+
# - repo: https://github.com/pre-commit/mirrors-mypy
|
|
17
|
+
# rev: v1.14.1
|
|
18
|
+
# hooks:
|
|
19
|
+
# - id: mypy
|
|
20
|
+
exclude: '(.venv|.*_cache)/.*'
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Claas Rostock
|
|
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.
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: parent-lookup
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python package enabling a child object to dynamically lookup its parent at runtime.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ClaasRostock/parent-lookup
|
|
6
|
+
Project-URL: Documentation, https://ClaasRostock.github.io/parent-lookup/README.html
|
|
7
|
+
Project-URL: Repository, https://github.com/ClaasRostock/parent-lookup.git
|
|
8
|
+
Project-URL: Issues, https://github.com/ClaasRostock/parent-lookup/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/ClaasRostock/parent-lookup/blob/main/CHANGELOG.md
|
|
10
|
+
Author-email: Claas Rostock <claas.rostock@dnv.com>
|
|
11
|
+
Maintainer-email: Claas Rostock <claas.rostock@dnv.com>
|
|
12
|
+
License: MIT License
|
|
13
|
+
|
|
14
|
+
Copyright (c) 2025 Claas Rostock
|
|
15
|
+
|
|
16
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
17
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
18
|
+
in the Software without restriction, including without limitation the rights
|
|
19
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
20
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
21
|
+
furnished to do so, subject to the following conditions:
|
|
22
|
+
|
|
23
|
+
The above copyright notice and this permission notice shall be included in all
|
|
24
|
+
copies or substantial portions of the Software.
|
|
25
|
+
|
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
27
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
28
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
29
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
30
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
31
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
32
|
+
SOFTWARE.
|
|
33
|
+
License-File: LICENSE
|
|
34
|
+
Keywords: child,lookup,parent
|
|
35
|
+
Classifier: Development Status :: 3 - Alpha
|
|
36
|
+
Classifier: Environment :: Console
|
|
37
|
+
Classifier: Intended Audience :: Developers
|
|
38
|
+
Classifier: Intended Audience :: Science/Research
|
|
39
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
40
|
+
Classifier: Operating System :: MacOS
|
|
41
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
42
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
46
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
47
|
+
Classifier: Topic :: Scientific/Engineering
|
|
48
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
49
|
+
Requires-Python: <3.14,>=3.10
|
|
50
|
+
Requires-Dist: typing-inspect>=0.9.0
|
|
51
|
+
Description-Content-Type: text/markdown
|
|
52
|
+
|
|
53
|
+
[](https://pypi.python.org/pypi/parent-lookup)
|
|
54
|
+
[](https://pypi.python.org/pypi/parent-lookup)
|
|
55
|
+
[](https://github.com/ClaasRostock/parent-lookup/blob/main/LICENSE)
|
|
56
|
+

|
|
57
|
+
[][parent_lookup_docs]
|
|
58
|
+
|
|
59
|
+
# parent-lookup
|
|
60
|
+
parent-lookup is a utility Python package enabling a child object to dynamically lookup its parent at runtime. <br>
|
|
61
|
+
It comprises of a small set of decorators and methods allowing bidirectional parent-child relationships without hard-coded circular dependencies or circular imports.
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
pip install parent-lookup
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Usage Example
|
|
71
|
+
|
|
72
|
+
API:
|
|
73
|
+
|
|
74
|
+
```py
|
|
75
|
+
from parent_lookup import TParent, is_child_lookup, lookup_registry
|
|
76
|
+
|
|
77
|
+
class Child:
|
|
78
|
+
def __init__(self) -> None:
|
|
79
|
+
pass
|
|
80
|
+
|
|
81
|
+
@overload
|
|
82
|
+
def find_parent(self, parent_type: type[TParent]) -> TParent | None:
|
|
83
|
+
pass
|
|
84
|
+
|
|
85
|
+
@overload
|
|
86
|
+
def find_parent(self, parent_type: TParent) -> TParent | None:
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
def find_parent(self, parent_type: type[TParent] | TParent) -> TParent | None:
|
|
90
|
+
return lookup_registry.lookup_parent(self, parent_type)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class Parent:
|
|
94
|
+
def __init__(self) -> None:
|
|
95
|
+
self._childs: list[Child] = []
|
|
96
|
+
|
|
97
|
+
def __new__(
|
|
98
|
+
cls,
|
|
99
|
+
) -> Parent:
|
|
100
|
+
instance = super().__new__(cls)
|
|
101
|
+
lookup_registry.register_parent(instance)
|
|
102
|
+
return instance
|
|
103
|
+
|
|
104
|
+
def add_child(self, child: Child) -> None:
|
|
105
|
+
self._childs.append(child)
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
@is_child_lookup
|
|
109
|
+
def childs(self) -> list[Child]:
|
|
110
|
+
return self._childs
|
|
111
|
+
|
|
112
|
+
# Create two objects: A parent object and a child object
|
|
113
|
+
parent = Parent()
|
|
114
|
+
child = Child()
|
|
115
|
+
# Add child to parent
|
|
116
|
+
parent.add_child(child)
|
|
117
|
+
# Lookup parent from child
|
|
118
|
+
found_parent = child.find_parent(Parent)
|
|
119
|
+
assert found_parent is parent
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
_For more examples and usage, please refer to parent-lookup's [documentation][parent_lookup_docs]._
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## Development Setup
|
|
126
|
+
|
|
127
|
+
### 1. Install uv
|
|
128
|
+
This project uses `uv` as package manager.
|
|
129
|
+
If you haven't already, install [uv](https://docs.astral.sh/uv), preferably using it's ["Standalone installer"](https://docs.astral.sh/uv/getting-started/installation/#__tabbed_1_2) method: <br>
|
|
130
|
+
..on Windows:
|
|
131
|
+
```sh
|
|
132
|
+
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
|
|
133
|
+
```
|
|
134
|
+
..on MacOS and Linux:
|
|
135
|
+
```sh
|
|
136
|
+
curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
137
|
+
```
|
|
138
|
+
(see [docs.astral.sh/uv](https://docs.astral.sh/uv/getting-started/installation/) for all / alternative installation methods.)
|
|
139
|
+
|
|
140
|
+
Once installed, you can update `uv` to its latest version, anytime, by running:
|
|
141
|
+
```sh
|
|
142
|
+
uv self update
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 2. Install Python
|
|
146
|
+
This project requires Python 3.10 or later. <br>
|
|
147
|
+
If you don't already have a compatible version installed on your machine, the probably most comfortable way to install Python is through `uv`:
|
|
148
|
+
```sh
|
|
149
|
+
uv python install
|
|
150
|
+
```
|
|
151
|
+
This will install the latest stable version of Python into the uv Python directory, i.e. as a uv-managed version of Python.
|
|
152
|
+
|
|
153
|
+
Alternatively, and if you want a standalone version of Python on your machine, you can install Python either via `winget`:
|
|
154
|
+
```sh
|
|
155
|
+
winget install --id Python.Python
|
|
156
|
+
```
|
|
157
|
+
or you can download and install Python from the [python.org](https://www.python.org/downloads/) website.
|
|
158
|
+
|
|
159
|
+
### 3. Clone the repository
|
|
160
|
+
Clone the parent-lookup repository into your local development directory:
|
|
161
|
+
```sh
|
|
162
|
+
git clone https://github.com/ClaasRostock/parent-lookup path/to/your/dev/parent-lookup
|
|
163
|
+
```
|
|
164
|
+
Change into the project directory after cloning:
|
|
165
|
+
```sh
|
|
166
|
+
cd parent-lookup
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 4. Install dependencies
|
|
170
|
+
Run `uv sync` to create a virtual environment and install all project dependencies into it:
|
|
171
|
+
```sh
|
|
172
|
+
uv sync
|
|
173
|
+
```
|
|
174
|
+
> **Note**: Using `--no-dev` will omit installing development dependencies.
|
|
175
|
+
|
|
176
|
+
> **Note**: `uv` will create a new virtual environment called `.venv` in the project root directory when running
|
|
177
|
+
> `uv sync` the first time. Optionally, you can create your own virtual environment using e.g. `uv venv`, before running
|
|
178
|
+
> `uv sync`.
|
|
179
|
+
|
|
180
|
+
### 6. (Optional) Activate the virtual environment
|
|
181
|
+
When using `uv`, there is in almost all cases no longer a need to manually activate the virtual environment. <br>
|
|
182
|
+
`uv` will find the `.venv` virtual environment in the working directory or any parent directory, and activate it on the fly whenever you run a command via `uv` inside your project folder structure:
|
|
183
|
+
```sh
|
|
184
|
+
uv run <command>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
However, you still _can_ manually activate the virtual environment if needed.
|
|
188
|
+
When developing in an IDE, for instance, this can in some cases be necessary depending on your IDE settings.
|
|
189
|
+
To manually activate the virtual environment, run one of the "known" legacy commands: <br>
|
|
190
|
+
..on Windows:
|
|
191
|
+
```sh
|
|
192
|
+
.venv\Scripts\activate.bat
|
|
193
|
+
```
|
|
194
|
+
..on Linux:
|
|
195
|
+
```sh
|
|
196
|
+
source .venv/bin/activate
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### 7. Install pre-commit hooks
|
|
200
|
+
The `.pre-commit-config.yaml` file in the project root directory contains a configuration for pre-commit hooks.
|
|
201
|
+
To install the pre-commit hooks defined therein in your local git repository, run:
|
|
202
|
+
```sh
|
|
203
|
+
uv run pre-commit install
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
All pre-commit hooks configured in `.pre-commit-config.yaml` will now run each time you commit changes.
|
|
207
|
+
|
|
208
|
+
pre-commit can also manually be invoked, at anytime, using:
|
|
209
|
+
```sh
|
|
210
|
+
uv run pre-commit run --all-files
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
To skip the pre-commit validation on commits (e.g. when intentionally committing broken code), run:
|
|
214
|
+
```sh
|
|
215
|
+
uv run git commit -m <MSG> --no-verify
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
To update the hooks configured in `.pre-commit-config.yaml` to their newest versions, run:
|
|
219
|
+
```sh
|
|
220
|
+
uv run pre-commit autoupdate
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### 8. Test that the installation works
|
|
224
|
+
To test that the installation works, run pytest in the project root folder:
|
|
225
|
+
```sh
|
|
226
|
+
uv run pytest
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Meta
|
|
230
|
+
|
|
231
|
+
Copyright (c) 2025 [Claas Rostock](https://github.com/ClaasRostock). All rights reserved.
|
|
232
|
+
|
|
233
|
+
Claas Rostock - [@LinkedIn](https://www.linkedin.com/in/claasrostock/?locale=en_US) - claas.rostock@dnv.com
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
Distributed under the MIT license. See [LICENSE](LICENSE.md) for more information.
|
|
238
|
+
|
|
239
|
+
[https://github.com/ClaasRostock/parent-lookup](https://github.com/ClaasRostock/parent-lookup)
|
|
240
|
+
|
|
241
|
+
## Contributing
|
|
242
|
+
|
|
243
|
+
1. Fork it (<https://github.com/ClaasRostock/parent-lookup/fork>)
|
|
244
|
+
2. Create an issue in your GitHub repo
|
|
245
|
+
3. Create your branch based on the issue number and type (`git checkout -b issue-name`)
|
|
246
|
+
4. Evaluate and stage the changes you want to commit (`git add -i`)
|
|
247
|
+
5. Commit your changes (`git commit -am 'place a descriptive commit message here'`)
|
|
248
|
+
6. Push to the branch (`git push origin issue-name`)
|
|
249
|
+
7. Create a new Pull Request in GitHub
|
|
250
|
+
|
|
251
|
+
For your contribution, please make sure you follow the [STYLEGUIDE](STYLEGUIDE.md) before creating the Pull Request.
|
|
252
|
+
|
|
253
|
+
<!-- Markdown link & img dfn's -->
|
|
254
|
+
[parent_lookup_docs]: https://ClaasRostock.github.io/parent-lookup/README.html
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
[](https://pypi.python.org/pypi/parent-lookup)
|
|
2
|
+
[](https://pypi.python.org/pypi/parent-lookup)
|
|
3
|
+
[](https://github.com/ClaasRostock/parent-lookup/blob/main/LICENSE)
|
|
4
|
+

|
|
5
|
+
[][parent_lookup_docs]
|
|
6
|
+
|
|
7
|
+
# parent-lookup
|
|
8
|
+
parent-lookup is a utility Python package enabling a child object to dynamically lookup its parent at runtime. <br>
|
|
9
|
+
It comprises of a small set of decorators and methods allowing bidirectional parent-child relationships without hard-coded circular dependencies or circular imports.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pip install parent-lookup
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage Example
|
|
19
|
+
|
|
20
|
+
API:
|
|
21
|
+
|
|
22
|
+
```py
|
|
23
|
+
from parent_lookup import TParent, is_child_lookup, lookup_registry
|
|
24
|
+
|
|
25
|
+
class Child:
|
|
26
|
+
def __init__(self) -> None:
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
@overload
|
|
30
|
+
def find_parent(self, parent_type: type[TParent]) -> TParent | None:
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
@overload
|
|
34
|
+
def find_parent(self, parent_type: TParent) -> TParent | None:
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
def find_parent(self, parent_type: type[TParent] | TParent) -> TParent | None:
|
|
38
|
+
return lookup_registry.lookup_parent(self, parent_type)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Parent:
|
|
42
|
+
def __init__(self) -> None:
|
|
43
|
+
self._childs: list[Child] = []
|
|
44
|
+
|
|
45
|
+
def __new__(
|
|
46
|
+
cls,
|
|
47
|
+
) -> Parent:
|
|
48
|
+
instance = super().__new__(cls)
|
|
49
|
+
lookup_registry.register_parent(instance)
|
|
50
|
+
return instance
|
|
51
|
+
|
|
52
|
+
def add_child(self, child: Child) -> None:
|
|
53
|
+
self._childs.append(child)
|
|
54
|
+
|
|
55
|
+
@property
|
|
56
|
+
@is_child_lookup
|
|
57
|
+
def childs(self) -> list[Child]:
|
|
58
|
+
return self._childs
|
|
59
|
+
|
|
60
|
+
# Create two objects: A parent object and a child object
|
|
61
|
+
parent = Parent()
|
|
62
|
+
child = Child()
|
|
63
|
+
# Add child to parent
|
|
64
|
+
parent.add_child(child)
|
|
65
|
+
# Lookup parent from child
|
|
66
|
+
found_parent = child.find_parent(Parent)
|
|
67
|
+
assert found_parent is parent
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
_For more examples and usage, please refer to parent-lookup's [documentation][parent_lookup_docs]._
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
## Development Setup
|
|
74
|
+
|
|
75
|
+
### 1. Install uv
|
|
76
|
+
This project uses `uv` as package manager.
|
|
77
|
+
If you haven't already, install [uv](https://docs.astral.sh/uv), preferably using it's ["Standalone installer"](https://docs.astral.sh/uv/getting-started/installation/#__tabbed_1_2) method: <br>
|
|
78
|
+
..on Windows:
|
|
79
|
+
```sh
|
|
80
|
+
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
|
|
81
|
+
```
|
|
82
|
+
..on MacOS and Linux:
|
|
83
|
+
```sh
|
|
84
|
+
curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
85
|
+
```
|
|
86
|
+
(see [docs.astral.sh/uv](https://docs.astral.sh/uv/getting-started/installation/) for all / alternative installation methods.)
|
|
87
|
+
|
|
88
|
+
Once installed, you can update `uv` to its latest version, anytime, by running:
|
|
89
|
+
```sh
|
|
90
|
+
uv self update
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 2. Install Python
|
|
94
|
+
This project requires Python 3.10 or later. <br>
|
|
95
|
+
If you don't already have a compatible version installed on your machine, the probably most comfortable way to install Python is through `uv`:
|
|
96
|
+
```sh
|
|
97
|
+
uv python install
|
|
98
|
+
```
|
|
99
|
+
This will install the latest stable version of Python into the uv Python directory, i.e. as a uv-managed version of Python.
|
|
100
|
+
|
|
101
|
+
Alternatively, and if you want a standalone version of Python on your machine, you can install Python either via `winget`:
|
|
102
|
+
```sh
|
|
103
|
+
winget install --id Python.Python
|
|
104
|
+
```
|
|
105
|
+
or you can download and install Python from the [python.org](https://www.python.org/downloads/) website.
|
|
106
|
+
|
|
107
|
+
### 3. Clone the repository
|
|
108
|
+
Clone the parent-lookup repository into your local development directory:
|
|
109
|
+
```sh
|
|
110
|
+
git clone https://github.com/ClaasRostock/parent-lookup path/to/your/dev/parent-lookup
|
|
111
|
+
```
|
|
112
|
+
Change into the project directory after cloning:
|
|
113
|
+
```sh
|
|
114
|
+
cd parent-lookup
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 4. Install dependencies
|
|
118
|
+
Run `uv sync` to create a virtual environment and install all project dependencies into it:
|
|
119
|
+
```sh
|
|
120
|
+
uv sync
|
|
121
|
+
```
|
|
122
|
+
> **Note**: Using `--no-dev` will omit installing development dependencies.
|
|
123
|
+
|
|
124
|
+
> **Note**: `uv` will create a new virtual environment called `.venv` in the project root directory when running
|
|
125
|
+
> `uv sync` the first time. Optionally, you can create your own virtual environment using e.g. `uv venv`, before running
|
|
126
|
+
> `uv sync`.
|
|
127
|
+
|
|
128
|
+
### 6. (Optional) Activate the virtual environment
|
|
129
|
+
When using `uv`, there is in almost all cases no longer a need to manually activate the virtual environment. <br>
|
|
130
|
+
`uv` will find the `.venv` virtual environment in the working directory or any parent directory, and activate it on the fly whenever you run a command via `uv` inside your project folder structure:
|
|
131
|
+
```sh
|
|
132
|
+
uv run <command>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
However, you still _can_ manually activate the virtual environment if needed.
|
|
136
|
+
When developing in an IDE, for instance, this can in some cases be necessary depending on your IDE settings.
|
|
137
|
+
To manually activate the virtual environment, run one of the "known" legacy commands: <br>
|
|
138
|
+
..on Windows:
|
|
139
|
+
```sh
|
|
140
|
+
.venv\Scripts\activate.bat
|
|
141
|
+
```
|
|
142
|
+
..on Linux:
|
|
143
|
+
```sh
|
|
144
|
+
source .venv/bin/activate
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 7. Install pre-commit hooks
|
|
148
|
+
The `.pre-commit-config.yaml` file in the project root directory contains a configuration for pre-commit hooks.
|
|
149
|
+
To install the pre-commit hooks defined therein in your local git repository, run:
|
|
150
|
+
```sh
|
|
151
|
+
uv run pre-commit install
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
All pre-commit hooks configured in `.pre-commit-config.yaml` will now run each time you commit changes.
|
|
155
|
+
|
|
156
|
+
pre-commit can also manually be invoked, at anytime, using:
|
|
157
|
+
```sh
|
|
158
|
+
uv run pre-commit run --all-files
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
To skip the pre-commit validation on commits (e.g. when intentionally committing broken code), run:
|
|
162
|
+
```sh
|
|
163
|
+
uv run git commit -m <MSG> --no-verify
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
To update the hooks configured in `.pre-commit-config.yaml` to their newest versions, run:
|
|
167
|
+
```sh
|
|
168
|
+
uv run pre-commit autoupdate
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### 8. Test that the installation works
|
|
172
|
+
To test that the installation works, run pytest in the project root folder:
|
|
173
|
+
```sh
|
|
174
|
+
uv run pytest
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Meta
|
|
178
|
+
|
|
179
|
+
Copyright (c) 2025 [Claas Rostock](https://github.com/ClaasRostock). All rights reserved.
|
|
180
|
+
|
|
181
|
+
Claas Rostock - [@LinkedIn](https://www.linkedin.com/in/claasrostock/?locale=en_US) - claas.rostock@dnv.com
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
Distributed under the MIT license. See [LICENSE](LICENSE.md) for more information.
|
|
186
|
+
|
|
187
|
+
[https://github.com/ClaasRostock/parent-lookup](https://github.com/ClaasRostock/parent-lookup)
|
|
188
|
+
|
|
189
|
+
## Contributing
|
|
190
|
+
|
|
191
|
+
1. Fork it (<https://github.com/ClaasRostock/parent-lookup/fork>)
|
|
192
|
+
2. Create an issue in your GitHub repo
|
|
193
|
+
3. Create your branch based on the issue number and type (`git checkout -b issue-name`)
|
|
194
|
+
4. Evaluate and stage the changes you want to commit (`git add -i`)
|
|
195
|
+
5. Commit your changes (`git commit -am 'place a descriptive commit message here'`)
|
|
196
|
+
6. Push to the branch (`git push origin issue-name`)
|
|
197
|
+
7. Create a new Pull Request in GitHub
|
|
198
|
+
|
|
199
|
+
For your contribution, please make sure you follow the [STYLEGUIDE](STYLEGUIDE.md) before creating the Pull Request.
|
|
200
|
+
|
|
201
|
+
<!-- Markdown link & img dfn's -->
|
|
202
|
+
[parent_lookup_docs]: https://ClaasRostock.github.io/parent-lookup/README.html
|