Python Autoload Module
Project description
autoload_module
Get classes and functions from modules simply and efficiently. The following is a plain example.
▼ Directory
project/
├ main.py
└ pipelines/
├ pipeline_a.py
├ pipeline_b.py
└ pipeline_c.py
▼ main.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
pip install autoload-module
Quick Start
There are only 2 steps. Imagine the following directory structure.
project/
├ main.py
└ functions.py
1. Set @loadable
to the class or function you want to import
# 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
# 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
project/
├ main.py
└ pipelines.py
▼ Code
# 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
project/
├ main.py
└ main_package/
├ module_a.py
└ sub_package/
├ module_b.py
└ module_c.py
▼ Code
# 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
.
@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
.
functions = autoload("..module_a", "function", base="main_package.sub_package")
License
Released under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file autoload_module-3.0.0.tar.gz
.
File metadata
- Download URL: autoload_module-3.0.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
72d54bdce30da9bd88c7723a63bf813551dc311d60807fb73fa9f97ad17836ed
|
|
MD5 |
f5031f7ffdaabdf962a25f0372a19258
|
|
BLAKE2b-256 |
3e29c78a78a712200c6db32c2c26dec223b9f87a4d52f452be8c747fce2b09d7
|
Provenance
The following attestation bundles were made for autoload_module-3.0.0.tar.gz
:
Publisher:
publish-prod.yml
on hiroki0525/autoload_module
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
autoload_module-3.0.0.tar.gz
-
Subject digest:
72d54bdce30da9bd88c7723a63bf813551dc311d60807fb73fa9f97ad17836ed
- Sigstore transparency entry: 154036535
- Sigstore integration time:
-
Permalink:
hiroki0525/autoload_module@f018fea929469e71635b2ac139c69d0a6e734534
-
Branch / Tag:
refs/heads/master
- Owner: https://github.com/hiroki0525
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish-prod.yml@f018fea929469e71635b2ac139c69d0a6e734534
-
Trigger Event:
push
-
Statement type:
File details
Details for the file autoload_module-3.0.0-py3-none-any.whl
.
File metadata
- Download URL: autoload_module-3.0.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
446f2e47a57d9e0df95dd2f3678f2baf8b75f954b392cfa4bfae66721db6f23b
|
|
MD5 |
bc6fd5a238c3ebb647f97929c4c9f0d9
|
|
BLAKE2b-256 |
c771da48b4db80a7c5df4e884f6966f7655f74fcde20ec13dcdabddde5b03b9b
|
Provenance
The following attestation bundles were made for autoload_module-3.0.0-py3-none-any.whl
:
Publisher:
publish-prod.yml
on hiroki0525/autoload_module
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
autoload_module-3.0.0-py3-none-any.whl
-
Subject digest:
446f2e47a57d9e0df95dd2f3678f2baf8b75f954b392cfa4bfae66721db6f23b
- Sigstore transparency entry: 154036536
- Sigstore integration time:
-
Permalink:
hiroki0525/autoload_module@f018fea929469e71635b2ac139c69d0a6e734534
-
Branch / Tag:
refs/heads/master
- Owner: https://github.com/hiroki0525
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish-prod.yml@f018fea929469e71635b2ac139c69d0a6e734534
-
Trigger Event:
push
-
Statement type: