Read the function name from the DB and execute it dynamically

Folder structure

project
  |
 ├ __init__.py
 ├ apps.py
 ├ models.py
 ├ routes.py
 ├ views.py

Create a Model

First, specify the class that represents the function list in models.py.

from django.db import models
from django.db.models.signals import post_save

class FuncList:

    func_id = models.CharField(max_length=50)
    callback = models.CharField(max_length=50)

    def get_func_id(self):
        return self.func_id

    def set_func_id(self, func_id):
        self.func_id = func_id
        self.save()

    def get_callback(self):
        return self.callback

    def set_callback(self, callback):
        self.callback = callback
        self.save()

Save / read to DB and dynamically execute

# Save to DB
def save_function(**kwargs):

    func_list = FuncList()

    func_list.set_func_id(kwargs['func_id'])
    func_list.set_callback(kwargs['callback'])

    return

# Dynamic execution
def dynamic_execution(arg1, arg2):

 # Function that executes the function read from DB
    def execution_handler(func, **kwargs):
        return func(**kwargs)

 #Get function list from DB
    func_list = FuncList.objects.all()

    for i in range(len(func_list)):
 # Execute the acquired functions in order
        result = execution_handler(func=eval(func_list[i].callback),
            arg1=arg1, arg2=arg2)

    return result

Afterword

I didn't have enough words, but thank you for coming to see it. Any questions in this article can be supplemented at the following sites. ~~ ・ ・ ・ Maybe ~~

Recommended Posts

Read the function name from the DB and execute it dynamically
Read the csv file and display it in the browser
[Python] I installed the game from pip and played it
If you want to put an argument in the closure function and execute it later
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
DB read of Sqlite3 from Excel (without ODBC) and suggestion search
Run Pylint and read the results
Read and use Python files from Python
Gender is determined from the name.
Output the report as PDF from DB with Python and automatically attach it to an email and send it
I want to pass an argument to a python function and execute it from PHP on a web server
Clogged when getting data from DB and making it a return value
Find out the name of the method that called it from the method that is python
[python] Send the image captured from the webcam to the server and save it
Find it in the procession and edit it
Get the address from latitude and longitude
[Introduction to Pandas] Read a csv file without a column name and give it a column name
Python --Read data from a numeric data file and find the multiple regression line.
Extract the product name and price from the product list in the Yodobashi.com purchase statement email.
Fourier transform the wav file read by Python, reverse transform it, and write it again.
[Free] Hit the Clash Royale API from lambda and send it to LINE
[Python] I tried to get the type name as a string from the type function
I want to get the file name, line number, and function name in Python 3.4