# autoload_module
[![PyPI version](https://badge.fury.io/py/autoload-module.svg)](https://badge.fury.io/py/autoload-module)
[![Test](https://github.com/hiroki0525/autoload_module/actions/workflows/test.yml/badge.svg)](https://github.com/hiroki0525/autoload_module/actions/workflows/test.yml)
[![Downloads](https://pepy.tech/badge/autoload-module)](https://pepy.tech/project/autoload-module)
![MIT License image](<%5Bimage_url%5D(https://img.shields.io/badge/license-MIT-blue.svg?style=flat)>)
Get classes and functions from modules simply and efficiently.
The following is a plain example.
▼ Directory
```text
project/
├ main.py
└ pipelines/
├ pipeline_a.py
├ pipeline_b.py
└ pipeline_c.py
```
▼ main.py
```py
from autoload import autoload
from functools import reduce
# Automatically import modules and return function objects
pipelines = autoload("pipelines", "function")
initial_value = 1
# Execute functions sequentially
final_result = reduce(lambda acc, func: func(acc), pipelines, initial_value)
```
## Install
```bash
pip install autoload-module
```
## Quick Start
There are only 2 steps. Imagine the following directory structure.
```text
project/
├ main.py
└ functions.py
```
### 1. Set `@loadable` to the class or function you want to import
```py
# functions.py
from autoload import loadable
@loadable
def increment(number: int) -> int:
return number + 1
@loadable
def decrement(number: int) -> int:
return number - 1
```
### 2. Set package or module name to `autoload`
```py
# main.py
from autoload import autoload
def main() -> None:
functions = autoload("pipelines", "function")
# If you want to get class
# pipelines = autoload("pipelines", "class")
# call increment function
print(functions[0](1))
# => 2
# call decrement function
print(functions[1](1))
# => 0
if __name__ == "__main__":
main()
```
## FAQ
### Can I import classes?
Set `"class"` to `autoload` .
▼ Directory
```text
project/
├ main.py
└ pipelines.py
```
▼ Code
```py
# pipelines.py
from autoload import loadable
@loadable
class PipelineA:
pass
@loadable
class PipelineB:
pass
# main.py
from autoload import autoload
classes = autoload("pipelines", "class")
```
### Can nested packages be loaded?
Set `"recursive=True"` to `autoload` .
▼ Directory
```text
project/
├ main.py
└ main_package/
├ module_a.py
└ sub_package/
├ module_b.py
└ module_c.py
```
▼ Code
```py
# module_a.py
@loadable
def module_a_function() -> None:
pass
# module_b.py
@loadable
def module_b_function() -> None:
pass
# module_c.py
@loadable
def module_c_function() -> None:
pass
# main.py
# only import module_a_function
functions = autoload("main_package", "function")
# import not only module_a_function but also module_b_function and module_c_function
functions = autoload("main_package", "function", recursive=True)
```
### Can the order of loading be controlled?
Set `order` to `@loadable` .
```py
@loadable(order=3)
def function_3() -> None:
pass
@loadable(order=1)
def function_1() -> None:
pass
@loadable(order=2)
def function_2() -> None:
pass
# main.py
# The order of `functions` is function_1, function_2 and function_3.
functions = autoload("package", "function")
```
### Can relative path be specified?
Set `base` to `autoload` .
```py
functions = autoload("..module_a", "function", base="main_package.sub_package")
```
## License
Released under the MIT license.
Raw data
{
"_id": null,
"home_page": "https://github.com/hiroki0525/autoload_module",
"name": "autoload-module",
"maintainer": "Hiroki Miyaji",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "nukoprogramming@gmail.com",
"keywords": "python, import, autoload, autoload_module, dynamic import",
"author": "Hiroki Miyaji",
"author_email": "nukoprogramming@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3e/29/c78a78a712200c6db32c2c26dec223b9f87a4d52f452be8c747fce2b09d7/autoload_module-3.0.0.tar.gz",
"platform": null,
"description": "# autoload_module\n\n[![PyPI version](https://badge.fury.io/py/autoload-module.svg)](https://badge.fury.io/py/autoload-module)\n[![Test](https://github.com/hiroki0525/autoload_module/actions/workflows/test.yml/badge.svg)](https://github.com/hiroki0525/autoload_module/actions/workflows/test.yml)\n[![Downloads](https://pepy.tech/badge/autoload-module)](https://pepy.tech/project/autoload-module)\n![MIT License image](<%5Bimage_url%5D(https://img.shields.io/badge/license-MIT-blue.svg?style=flat)>)\n\nGet classes and functions from modules simply and efficiently.\nThe following is a plain example.\n\n\u25bc Directory\n\n```text\nproject/\n \u251c main.py\n \u2514 pipelines/\n \u251c pipeline_a.py\n \u251c pipeline_b.py\n \u2514 pipeline_c.py\n```\n\n\u25bc main.py\n\n```py\nfrom autoload import autoload\nfrom functools import reduce\n\n# Automatically import modules and return function objects\npipelines = autoload(\"pipelines\", \"function\")\n\ninitial_value = 1\n\n# Execute functions sequentially\nfinal_result = reduce(lambda acc, func: func(acc), pipelines, initial_value)\n```\n\n## Install\n\n```bash\npip install autoload-module\n```\n\n## Quick Start\n\nThere are only 2 steps. Imagine the following directory structure.\n\n```text\nproject/\n \u251c main.py\n \u2514 functions.py\n```\n\n### 1. Set `@loadable` to the class or function you want to import\n\n```py\n# functions.py\n\nfrom autoload import loadable\n\n\n@loadable\ndef increment(number: int) -> int:\n return number + 1\n\n\n@loadable\ndef decrement(number: int) -> int:\n return number - 1\n```\n\n### 2. Set package or module name to `autoload`\n\n```py\n# main.py\n\nfrom autoload import autoload\n\n\ndef main() -> None:\n functions = autoload(\"pipelines\", \"function\")\n # If you want to get class\n # pipelines = autoload(\"pipelines\", \"class\")\n\n # call increment function\n print(functions[0](1))\n # => 2\n\n # call decrement function\n print(functions[1](1))\n # => 0\n\n\nif __name__ == \"__main__\":\n main()\n```\n\n## FAQ\n\n### Can I import classes?\n\nSet `\"class\"` to `autoload` .\n\n\u25bc Directory\n\n```text\nproject/\n \u251c main.py\n \u2514 pipelines.py\n```\n\n\u25bc Code\n\n```py\n# pipelines.py\n\nfrom autoload import loadable\n\n\n@loadable\nclass PipelineA:\n pass\n\n\n@loadable\nclass PipelineB:\n pass\n\n\n# main.py\nfrom autoload import autoload\n\nclasses = autoload(\"pipelines\", \"class\")\n```\n\n### Can nested packages be loaded?\n\nSet `\"recursive=True\"` to `autoload` .\n\n\u25bc Directory\n\n```text\nproject/\n \u251c main.py\n \u2514 main_package/\n \u251c module_a.py\n \u2514 sub_package/\n \u251c module_b.py\n \u2514 module_c.py\n```\n\n\u25bc Code\n\n```py\n# module_a.py\n@loadable\ndef module_a_function() -> None:\n pass\n\n\n# module_b.py\n@loadable\ndef module_b_function() -> None:\n pass\n\n\n# module_c.py\n@loadable\ndef module_c_function() -> None:\n pass\n\n\n# main.py\n# only import module_a_function\nfunctions = autoload(\"main_package\", \"function\")\n\n# import not only module_a_function but also module_b_function and module_c_function\nfunctions = autoload(\"main_package\", \"function\", recursive=True)\n```\n\n### Can the order of loading be controlled?\n\nSet `order` to `@loadable` .\n\n```py\n@loadable(order=3)\ndef function_3() -> None:\n pass\n\n\n@loadable(order=1)\ndef function_1() -> None:\n pass\n\n\n@loadable(order=2)\ndef function_2() -> None:\n pass\n\n\n# main.py\n# The order of `functions` is function_1, function_2 and function_3.\nfunctions = autoload(\"package\", \"function\")\n```\n\n### Can relative path be specified?\n\nSet `base` to `autoload` .\n\n```py\nfunctions = autoload(\"..module_a\", \"function\", base=\"main_package.sub_package\")\n```\n\n## License\n\nReleased under the MIT license.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python Autoload Module",
"version": "3.0.0",
"project_urls": {
"Documentation": "https://github.com/hiroki0525/autoload_module",
"Homepage": "https://github.com/hiroki0525/autoload_module",
"Repository": "https://github.com/hiroki0525/autoload_module"
},
"split_keywords": [
"python",
" import",
" autoload",
" autoload_module",
" dynamic import"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c771da48b4db80a7c5df4e884f6966f7655f74fcde20ec13dcdabddde5b03b9b",
"md5": "bc6fd5a238c3ebb647f97929c4c9f0d9",
"sha256": "446f2e47a57d9e0df95dd2f3678f2baf8b75f954b392cfa4bfae66721db6f23b"
},
"downloads": -1,
"filename": "autoload_module-3.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bc6fd5a238c3ebb647f97929c4c9f0d9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5901,
"upload_time": "2024-12-08T08:20:09",
"upload_time_iso_8601": "2024-12-08T08:20:09.942581Z",
"url": "https://files.pythonhosted.org/packages/c7/71/da48b4db80a7c5df4e884f6966f7655f74fcde20ec13dcdabddde5b03b9b/autoload_module-3.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3e29c78a78a712200c6db32c2c26dec223b9f87a4d52f452be8c747fce2b09d7",
"md5": "f5031f7ffdaabdf962a25f0372a19258",
"sha256": "72d54bdce30da9bd88c7723a63bf813551dc311d60807fb73fa9f97ad17836ed"
},
"downloads": -1,
"filename": "autoload_module-3.0.0.tar.gz",
"has_sig": false,
"md5_digest": "f5031f7ffdaabdf962a25f0372a19258",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 4807,
"upload_time": "2024-12-08T08:20:11",
"upload_time_iso_8601": "2024-12-08T08:20:11.938000Z",
"url": "https://files.pythonhosted.org/packages/3e/29/c78a78a712200c6db32c2c26dec223b9f87a4d52f452be8c747fce2b09d7/autoload_module-3.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-08 08:20:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hiroki0525",
"github_project": "autoload_module",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "autoload-module"
}