DEV Community

nuko_suke
nuko_suke

Posted on • Edited on

Convenient Python Library for importing modules

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
Enter fullscreen mode Exit fullscreen mode
  • validator_a.py
class ValidatorA:
    # b and c are same.
    def valildate(self, input):
        # process of validation
Enter fullscreen mode Exit fullscreen mode
  • 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!!")
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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)
Enter fullscreen mode Exit fullscreen mode

Hot to use

Please read this.

Conclusion

This library give automatically importing modules.
I welcome your contribution!

Top comments (0)