Home > Mobile >  Python create dynamic class and set multi bases from imported module
Python create dynamic class and set multi bases from imported module

Time:01-13

I found several example here, but is not what exactly looking for, will try to explain here from this answer tried to achieve my result but is not what looking for How can I dynamically create derived classes from a base class

i have a module that holds many classes

importing the module

import importlib

# are awalable many classes here
forms = importlib.import_module('my_forms')

Now, based on forms i need to create a new class and add bases to my new class all classes that are availabe in forms

this what i tried, but can not find a way to assign the bases

import inspect

def create_DynamicClass():
    class DynamicClass(BaseClass):
        pass

    for form_name, class_name in inspect.getmembers(forms):
        for i in class_name():
            # here the code to added all bases to DynamicClass
            
    return DynamicClass()

example how my_forms module looks

class MyClass1(BaseClass):
    
    attr1 = 1
    attr2 = 2
    
    @coroutine
    def prepare(self):
        # some code for each class

class MyClass2(BaseClass):
    
    attr3 = 3
    attr4 = 4
    
    @coroutine
    def prepare(self):
        # some code for each class

class MyClass3(BaseClass):
    
    attr5 = 5
    attr6 = 6
    
    @coroutine
    def prepare(self):
        # some code for each class

The result that i want to achieve is the following, will make a static class to show desired result but need to be dynamic

I need to create my class dynamic because in my_forms module can be any amount of classes

# inherits all classes from my_forms module
class MyResultClass(MyClass1, MyClass2, MyClass3):

    # here get all available attributes from all classes
    

    @coroutine
    def prepare(self):
        # as well need each prepare function for each class as well
        yield MyClass1().prepare()
        yield MyClass2().prepare()
        yield MyClass3().prepare()

CodePudding user response:

Simply declare the dynamic class with all of your base classes. To do so, put all of your base classes in a list, and unpack the list in the class definition statement with the * operator like this:

def createClass(baseClasess):
    class NewClass(*baseClasses):
       pass
    
    return NewClass

DynamicClass = createClass([class1, class2, ...])

CodePudding user response:

i have managed to find a solution, will post here, if any recommendation to make it better will appreciate

forms = importlib.import_module('my_forms')

class Form(BaseForm):

    @coroutine
    def prepare(self):
        for form_name, class_name in inspect.getmembers(forms, inspect.isclass):
            try:
                yield class_name().prepare()
            except TypeError:
                continue


def createClass(meta):
    for form_name, class_name in inspect.getmembers(forms, inspect.isclass):
        try:
            Form.__bases__  = (class_name, )
            for field in class_name():
                field_type = fl.SelectField() if hasattr(field, 'choices') else fl.StringField()
                setattr(Form, field.name, field_type)
        except TypeError:
            continue
    return Form(meta=meta)
  • Related