[Python] I made my own library that can be imported dynamically

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!

What can you do with this library?

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!!")

What is delicious about this library?

** 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)

How do you use this library?

Please refer to this page for how to use it!

Why did you make such a library?

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!

One last word

Contribution is welcome as there are still some things that are not enough!

Recommended Posts

[Python] I made my own library that can be imported dynamically
I made my own Python library
I made a shuffle that can be reset (reverted) with Python
I made my own language. (1)
I made a familiar function that can be used in statistics with Python
I made my own language (2)
I made my own AML
I made my own Django Middleware so that I can access request information from anywhere
I published my own Python baseball library to Packaging & PyPI
I made a package that can compare morphological analyzers with Python
Python> I made a test code for my own external file
I made a library that adds docstring to a Python stub file.
[python] I made a class that can write a file tree quickly
I created a template for a Python project that can be used universally
[Python] I made a utility that can access dict type like a path
I made a simple timer that can be started from the terminal
I made a module PyNanaco that can charge nanaco credit with python
I made my own primitive static site generator
I tried to publish my own module so that I can pip install it
I want to create a priority queue that can be updated in Python (2.7)
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
I tried using the Python library "pykakasi" that can convert kanji to romaji.
I made a Docker image that can call FBX SDK Python from Node.js
How to install a Python library that can be used by pharmaceutical companies
I made my own parallel link robot (software version)
I made a VM that runs OpenCV for Python
Python knowledge notes that can be used with AtCoder
Can be used in competition pros! Python standard library
I made a python library to do rolling rank
I made my own parallel link robot (mechanical edition)
I made my goimports
I made a module that can be glitched easily, but I can't pass arguments from entry_points
[Atcoder] [C ++] I made a test automation tool that can be used during the contest
Until you can install your own Python library with pip
Scripts that can be used when using bottle in Python
I made my own OSS because I wanted to contribute to it
I investigated the pretreatment that can be done with PyCaret
I made a plug-in that can "Daruma-san fell" with Minecraft
I made blackjack with python!
I made a python text
I made blackjack with Python.
I made wordcloud with Python.
I made it because I want JSON data that can be used freely in demos and prototypes
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
[Python] I made a Line bot that randomly asks English words.
[Python] Make a graph that can be moved around with Plotly
I made a library to easily read config files with Python
[Python3] I made a decorator that declares undefined functions and methods.
I tried Python! ] Can I post to Kaggle on my iPad Pro?
Investigation of DC power supplies that can be controlled by Python
Created a library for python that can easily handle morpheme division
About psd-tools, a library that can process psd files in Python
Python standard input summary that can be used in competition pro
I made my own filter plugin for text parsing of Ansible
Python standard module that can be used on the command line
I made a Python wrapper library for docomo image recognition API.
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
[Python] I made a function that can also use regular expressions that replace character strings all at once.
I tried to deepen my understanding by making my own discriminator that can classify binary using logistic regression.
Can I be a data scientist?
I made a Line-bot using Python!