I want to create a plug-in type implementation

When creating a script that makes a judgment according to the rules

I wanted to be able to add rules, so I created it

Made it possible to execute by creating a class that inherits Person in classes

.
├── Main.py
└── classes
    ├── Person.py
    ├── Taro.py
    └── Tom.py

Main.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from os.path import join, relpath, splitext
from os import path
from glob import glob
import re
import sys

def main():
    """ Run from Here. """
    dir = "classes"
    exception_file = ["__pycache__", "Person.py"]
    extention = ".py"
    reg = re.compile('.+\\' +extention)

    sys.path.append(dir) #Add path
    files = [path.relpath(x, dir) for x in glob(path.join(dir, '*'))]
    files = list(set(files) - set(exception_file))

    objs = []
    for x in files:
        if reg.match(x):
            name = x.replace(".py", "")
            #Load class here
            m = __import__(name, globals(), locals(), [name], 0 )
            o = eval("m."+name+"()")
            objs.append(o)

    for obj in objs:
        obj.say()

if __name__=="__main__":
    main()

Person.py


from abc import ABCMeta
from abc import abstractmethod

class Person(metaclass=ABCMeta):

    @abstractmethod
    def say(self):
        raise NotImplementedError

Taro.py


from Person import Person

class Taro(Person):
    def say(self):
        print("Hello, this is Taro.")

Tom.py


from Person import Person

class Tom(Person):
    def say(self):
        print("Hello, this is Tom.")

out


$ python Main.py 
Hello, this is Taro.
Hello, this is Tom.

What I don't understand

Is it dangerous to implement a script that can be added without rewriting the program?

Is there a smarter way to call?

Please tell me

Recommended Posts

I want to create a plug-in type implementation
I tried to create a linebot (implementation)
I want to easily create a Noise Model
I want to create a window in Python
I want to manually create a legend with matplotlib
I want to create a Dockerfile for the time being.
I want to print in a comprehension
I tried to create a linebot (preparation)
I want to build a Python environment
numpy: I want to convert a single type ndarray to a structured array
Qiskit: I want to create a circuit that creates arbitrary states! !!
I want to create a system to prevent forgetting to tighten the key 1
I want to create a pipfile and reflect it in docker
I want to create a machine learning service without programming! WebAPI
I want to make matplotlib a dark theme
I want to INSERT a DataFrame into MSSQL
I want to make a game with Python
If you want to create a Word Cloud.
I don't want to take a coding test
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
I tried to create a plug-in with HULFT IoT Edge Streaming [Execution] (3/3)
I wrote a script to upload a WordPress plugin
I want to easily find a delicious restaurant
I tried to create a plug-in with HULFT IoT Edge Streaming [Setup] (1/3)
I want to write to a file with Python
I want to create a machine learning service without programming! Text classification
I want to upload a Django app to heroku
I want to create a web application that uses League of Legends data ①
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I tried to create a table only with Django
100 image processing knocks !! (021-030) I want to take a break ...
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
How to create a submenu with the [Blender] plugin
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
I want to find a popular package on PyPi
I want to easily build a model-based development environment
I want to work with a robot in python.
I want to split a character string with hiragana
I want to install a package of Php Redis
[Python] I want to make a nested list a tuple
I want to send a business start email automatically
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want a mox generator
Create a Wox plugin (Python)
I want to solve Sudoku (Sudoku)
I want a mox generator (2)
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to make a click macro with pyautogui (desire)
I want to automatically generate a modern metal band name
NikuGan ~ I want to see a lot of delicious meat! !!
I made a tool to create a word cloud from wikipedia
I want to make a click macro with pyautogui (outlook)