I released the Python library which is named autoload_module.
It will be more comfortable to your Python programming.
What is the library?
This library is enable to automatically import modules and get class objects. The example is below.
- Directory
project/
├ example.py
└ validator/
├ validator_a.py
├ validator_b.py
└ validator_c.py
- validator_a.py
class ValidatorA:
# b and c are same.
def valildate(self, input):
# process of validation
- example.py
from autoload.module_loader import ModuleLoader
input = "foo bar baz"
loader = ModuleLoader()
# Automatically import modules and return class objects
validator_classes = loader.load_classes("validator")
try:
# initialize and execute method
[clazz().validate(input) for clazz in validator_classes]
except:
print("input is invalid!!")
You can also get function objects.
# This is the example you defined not classes but functions like 'validate_A', 'validate_B' and 'validate_C'
validate_functions = loader.load_functions("validator")
[func(input) for func in validate_functions]
What is useful?
It will be useful to do something in bulk.
The following is assumed as a concrete usage example.
- validation
- pipeline
The example of pipeline is below.
- Directory
project/
├ example.py
└ pipelineA/
├ get_data_a.py
└ processing_data_a.py
└ pipelineB/
├ get_data_b.py
└ processing_data_b.py
- example.py
from autoload.module_loader import ModuleLoader
package_names = ("pipelineA", "pipelineB")
loader = ModuleLoader()
# You should use library like `concurrent.futures`
for package_name in package_names:
GetData, ProcessingData = loader.load_classes(package_name)
data = GetData().get()
processed_data = ProcessingData().process(data)
Hot to use
Please read this.
Conclusion
This library give automatically importing modules.
I welcome your contribution!
Top comments (0)