Hello! It's Nukosuke! python has a built-in library that dynamically imports a module called importlib, but I made my own library to make it easier to use!
By specifying the package name, ** You can dynamically import the modules under it and then get the class object defined in the module! ** ** Specifically, you can write the following Python code!
project/
├ example.py
└ validator/
├ validator_a.py
├ validator_b.py
└ validator_c.py
class ValidatorA:
#Same for b and c
def valildate(self, input):
#Validation process
from autoload.module_loader import ModuleLoader
input = "foo bar baz"
loader = ModuleLoader()
#Automatically loads modules under validator and returns a list of class objects such as ValidatorA
validator_classes = loader.load_classes("validator")
try:
#Instantiate a class object and execute a method
[clazz().validate(input) for clazz in validator_classes]
except:
print("input is invalid!!")
** It is very useful when you want to do something together! ** ** The following is assumed as a concrete usage scene example.
When implementing a pipeline, it looks like this: (It is an image of acquiring and processing data in parallel)
project/
├ example.py
└ pipelineA/
├ get_data_a.py
└ processing_data_a.py
└ pipelineB/
├ get_data_b.py
└ processing_data_b.py
from autoload.module_loader import ModuleLoader
package_names = ("pipelineA", "pipelineB")
loader = ModuleLoader()
#This is parallel processing
for package_name in package_names:
GetData, ProcessingData = loader.load_classes(package_name)
data = GetData().get()
processed_data = ProcessingData().process(data)
Please refer to this page for how to use it!
Originally, I personally created a site that scores and introduces the recommendation level of books, but I apply several patterns of validation to the book data. I needed to.
I googled variously thinking that I could make a module for each validation pattern and dynamically import it for a good feeling and execute validation, but it didn't seem to be so I made it myself!
Although I made it myself, I thought that it could be used for general purposes, so I released it as a library!
Contribution is welcome as there are still some things that are not enough!
Recommended Posts