<p align="center"></p>
<h2 align="center">Cyclic Classes</h2>
<p align="center">
<a href="https://github.com/rgryta/Cyclic-Classes/actions/workflows/main.yml"><img alt="Python package" src="https://github.com/rgryta/Cyclic-Classes/actions/workflows/main.yml/badge.svg?branch=main"></a>
<a href="https://pypi.org/project/cyclic-classes/"><img alt="PyPI" src="https://img.shields.io/pypi/v/cyclic-classes"></a>
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
<a href="https://github.com/PyCQA/pylint"><img alt="pylint" src="https://img.shields.io/badge/linting-pylint-yellowgreen"></a>
<a href="https://github.com/rgryta/NoPrint"><img alt="NoPrint" src="https://img.shields.io/badge/NoPrint-enabled-blueviolet"></a>
</p>
## About
Ever had a situation when you wanted to create a cyclic reference between two objects, but don't want to maintain a large singular file?
You're tired of seeing "cyclic-import" errors? This package is for you!
Simply "register" a class you want to share between different modules and you're pretty much good to go!
See the [documentation](https://github.com/rgryta/Cyclic-Classes/#Usage) for more information.
## Requirements
There's ***NONE!***
## Installation
Pull straight from this repo to install manually or just use pip: `pip install cyclic-classes` will do the trick.
## Usage
Consider a package `myk8s` with two modules, maybe one is to reference a k8s deployment and the other one is for pods within that deployment.
`deployment.py`
```python
from .pod import Pod
class Deployment:
def __init__(self, name: str):
self.name = name
@property
def pods(self):
return [Pod(f"{self.name}-pod-{i}") for i in range(3)]
```
Now, if you want to create a back-reference for Deployment within Pod, like this:
`pod.py`
```python
from .deployment import Deployment
class Pod:
def __init__(self, name: str):
self.name = name
@property
def deployment(self):
return Deployment(self.name.split("-")[0])
```
In the above example, you would get a cyclic-import error. To avoid this, you can use `cyclic_classes` to register the classes you want to share between modules.
Do so like this:
`deployment.py`
```python
from cyclic_classes import register, cyclic_import
with cyclic_import(): # This is required to avoid cyclic-import errors - you're actually importing a registered class underneath, but IDE will think it's your actual class
from .pod import Pod
@register # You have to register the class you want to access so that Pod will also be able to use it in the pod.py file
class Deployment:
def __init__(self, name: str):
self.name = name
@property
def pods(self):
return [Pod(f"{self.name}-pod-{i}") for i in range(3)]
```
Now, if you want to create a back-reference for Deployment within Pod, like this:
`pod.py`
```python
from cyclic_classes import register, cyclic_import
with cyclic_import():
from .deployment import Deployment
@register # Making Pod available to cyclic_import
class Pod:
def __init__(self, name: str):
self.name = name
@property
def deployment(self):
return Deployment(self.name.split("-")[0])
```
And that's it! You can now use the classes in your code without any issues.
### Note
Cyclic import supports multiple ways of imports, including relative imports, absolute imports, and even module imports.
As such, in the `cyclic_import` context you can use either of the following:
```python
import myk8s.deployment as depl # And later use depl.Deployment
from .deployment import Deployment
from myk8s.deployment import Deployment
# Not recommended, but also possible
# Reason: Such imports wouldn't work even if there was no cyclic import issue, but it works with cyclic import (you'll get a warning though) and some IDEs think it's correct
from deployment import Deployment
import deployment
```
This also works with aliases! So feel free to use `import ... as ...` or `from ... import ... as ...` as you wish.
## Development
### Installation
Install virtual environment and cyclic_classes package in editable mode with dev dependencies.
```bash
python -m venv venv
source venv/bin/activate
pip install -e .[dev]
```
### How to?
Automate as much as we can, see configuration in `pyproject.toml` file to see what are the flags used.
```bash
staging format # Reformat the code
staging lint # Check for linting issues
staging test # Run unit tests and coverage report
```
Raw data
{
"_id": null,
"home_page": null,
"name": "cyclic-classes",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": "python, cyclic, classes, circular, dependencies",
"author": null,
"author_email": "Radoslaw Gryta <radek.gryta@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/e2/68/89459fe522a06849429032bf849c9c078a9d7023d40c942ecf650aa9d290/cyclic_classes-0.0.2.tar.gz",
"platform": null,
"description": "<p align=\"center\"></p>\n<h2 align=\"center\">Cyclic Classes</h2>\n<p align=\"center\">\n<a href=\"https://github.com/rgryta/Cyclic-Classes/actions/workflows/main.yml\"><img alt=\"Python package\" src=\"https://github.com/rgryta/Cyclic-Classes/actions/workflows/main.yml/badge.svg?branch=main\"></a>\n<a href=\"https://pypi.org/project/cyclic-classes/\"><img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/cyclic-classes\"></a>\n<a href=\"https://github.com/psf/black\"><img alt=\"Code style: black\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"></a>\n<a href=\"https://github.com/PyCQA/pylint\"><img alt=\"pylint\" src=\"https://img.shields.io/badge/linting-pylint-yellowgreen\"></a>\n<a href=\"https://github.com/rgryta/NoPrint\"><img alt=\"NoPrint\" src=\"https://img.shields.io/badge/NoPrint-enabled-blueviolet\"></a>\n</p>\n\n## About\n\nEver had a situation when you wanted to create a cyclic reference between two objects, but don't want to maintain a large singular file?\nYou're tired of seeing \"cyclic-import\" errors? This package is for you!\nSimply \"register\" a class you want to share between different modules and you're pretty much good to go!\n\nSee the [documentation](https://github.com/rgryta/Cyclic-Classes/#Usage) for more information.\n\n## Requirements\n\nThere's ***NONE!***\n\n## Installation\n\nPull straight from this repo to install manually or just use pip: `pip install cyclic-classes` will do the trick.\n\n## Usage\n\nConsider a package `myk8s` with two modules, maybe one is to reference a k8s deployment and the other one is for pods within that deployment.\n\n\n`deployment.py`\n```python\nfrom .pod import Pod\n\nclass Deployment:\n def __init__(self, name: str):\n self.name = name\n \n @property\n def pods(self):\n return [Pod(f\"{self.name}-pod-{i}\") for i in range(3)]\n```\n\nNow, if you want to create a back-reference for Deployment within Pod, like this:\n`pod.py`\n```python\nfrom .deployment import Deployment\n\nclass Pod:\n def __init__(self, name: str):\n self.name = name\n \n @property\n def deployment(self):\n return Deployment(self.name.split(\"-\")[0])\n```\n\nIn the above example, you would get a cyclic-import error. To avoid this, you can use `cyclic_classes` to register the classes you want to share between modules.\nDo so like this:\n\n`deployment.py`\n```python\nfrom cyclic_classes import register, cyclic_import\n\nwith cyclic_import(): # This is required to avoid cyclic-import errors - you're actually importing a registered class underneath, but IDE will think it's your actual class\n from .pod import Pod\n\n@register # You have to register the class you want to access so that Pod will also be able to use it in the pod.py file\nclass Deployment:\n def __init__(self, name: str):\n self.name = name\n \n @property\n def pods(self):\n return [Pod(f\"{self.name}-pod-{i}\") for i in range(3)]\n```\n\nNow, if you want to create a back-reference for Deployment within Pod, like this:\n`pod.py`\n```python\nfrom cyclic_classes import register, cyclic_import\n\nwith cyclic_import():\n from .deployment import Deployment\n\n@register # Making Pod available to cyclic_import\nclass Pod:\n def __init__(self, name: str):\n self.name = name\n \n @property\n def deployment(self):\n return Deployment(self.name.split(\"-\")[0])\n```\n\nAnd that's it! You can now use the classes in your code without any issues.\n\n### Note\n\nCyclic import supports multiple ways of imports, including relative imports, absolute imports, and even module imports.\nAs such, in the `cyclic_import` context you can use either of the following:\n\n```python\nimport myk8s.deployment as depl # And later use depl.Deployment\nfrom .deployment import Deployment\nfrom myk8s.deployment import Deployment\n\n# Not recommended, but also possible\n# Reason: Such imports wouldn't work even if there was no cyclic import issue, but it works with cyclic import (you'll get a warning though) and some IDEs think it's correct \nfrom deployment import Deployment\nimport deployment\n```\n\nThis also works with aliases! So feel free to use `import ... as ...` or `from ... import ... as ...` as you wish.\n\n## Development\n\n### Installation\n\nInstall virtual environment and cyclic_classes package in editable mode with dev dependencies.\n\n```bash\npython -m venv venv\nsource venv/bin/activate\npip install -e .[dev]\n```\n\n### How to?\n\nAutomate as much as we can, see configuration in `pyproject.toml` file to see what are the flags used.\n\n```bash\nstaging format # Reformat the code\nstaging lint # Check for linting issues\nstaging test # Run unit tests and coverage report\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "Allow creation of circular module dependencies between classes",
"version": "0.0.2",
"project_urls": {
"Bug Tracker": "https://github.com/rgryta/Cyclic-Classes/issues",
"Homepage": "https://github.com/rgryta/Cyclic-Classes"
},
"split_keywords": [
"python",
" cyclic",
" classes",
" circular",
" dependencies"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b3b203ec5ab42a5ce25211cb8d9222d90eb7b1a4ed4d887b4843da62d134578a",
"md5": "9a4d147f2392e5c3e310d9d5ac0d60e8",
"sha256": "b05c8cf62107e884b15d751c6129f5f74ddbd5b52aedb05d5278860cdc4bd324"
},
"downloads": -1,
"filename": "cyclic_classes-0.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9a4d147f2392e5c3e310d9d5ac0d60e8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 11787,
"upload_time": "2024-05-13T10:11:03",
"upload_time_iso_8601": "2024-05-13T10:11:03.391017Z",
"url": "https://files.pythonhosted.org/packages/b3/b2/03ec5ab42a5ce25211cb8d9222d90eb7b1a4ed4d887b4843da62d134578a/cyclic_classes-0.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e26889459fe522a06849429032bf849c9c078a9d7023d40c942ecf650aa9d290",
"md5": "63cbf3cfca07fed87d311a8ece87dce4",
"sha256": "2deca6520172fff9fd94dd2335395ae437523d5bfd72bc533091e2937e586a6b"
},
"downloads": -1,
"filename": "cyclic_classes-0.0.2.tar.gz",
"has_sig": false,
"md5_digest": "63cbf3cfca07fed87d311a8ece87dce4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 12814,
"upload_time": "2024-05-13T10:11:04",
"upload_time_iso_8601": "2024-05-13T10:11:04.323622Z",
"url": "https://files.pythonhosted.org/packages/e2/68/89459fe522a06849429032bf849c9c078a9d7023d40c942ecf650aa9d290/cyclic_classes-0.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-13 10:11:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rgryta",
"github_project": "Cyclic-Classes",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "cyclic-classes"
}