Python 我可以根据传递给类的数据触发一组动态实例方法吗?

Python 我可以根据传递给类的数据触发一组动态实例方法吗?,python,oop,methods,analytics,pipeline,Python,Oop,Methods,Analytics,Pipeline,我正在尝试用python构建一个动态分析管道 目标是能够将一组指令传递到管道(使用JSON),并循环通过这些指令来触发类中的方法 我有办法做到这一点,但我想知道这是一个糟糕的解决方案,还是因为我解决这个问题的方式,我可能会遇到规模/并行化/内存问题 """ Class with methods we want to execute using data passed by API. """ class prediction_node:

我正在尝试用python构建一个动态分析管道

目标是能够将一组指令传递到管道(使用JSON),并循环通过这些指令来触发类中的方法

我有办法做到这一点,但我想知道这是一个糟糕的解决方案,还是因为我解决这个问题的方式,我可能会遇到规模/并行化/内存问题

"""
Class with methods we want to execute using data passed by API.
"""

class prediction_node:
    def __init__(self, name):
        self.name = name
        self.history = None
        self.forecast = None

    def set_history(self, **kwargs):
        """
        A note on this. In the current formualtion, all methods need kwargs because our pipeline passes a key word dictionary, regardless of if the method needs it.
        the key words are then unpacked in the method. fine for now?
        """
        d = kwargs["input"]["history"]
        self.history = d

    def generate_prediction(self, **kwargs):
        self.forecast = [1,2,3,4,5,6]


"""
A set fo method templates that define what method we want to call as a string, and the kwargs it can take
"""
set_history_template = {"method":"set_history",
"method_args":{"history":None, "something_else":None}}

set_prediction_template = {"method":"generate_prediction",
"method_args":{}}

"""
This needs to be a templated, because if we pass parameters, we can't call the method more than once in a pipeline with different args.
"""
"""
combine the instruction template into a list to execute
One method with no args, one with two, just to make sure that works.
"""

instriction_instance_1 = [
[set_history_template, {"history":[1,2,3,4,5,6,7,8,9], "something_else": "xyz"}],
[set_prediction_template,{}]
]

"""
Get an instance of above class
"""
y = prediction_node("test_prediction")

"""
Fill kwarg values into templates to get final list of methods to call with data.
"""
def compile_instruction_set(instruction_instance):
    compiled_instructions = []
    for template, fill in instruction_instance:
        # print("template", template, "fill", fill)
        template_filled = template
        for f in fill.keys():
            # print(f)
            template_filled["method_args"][f] = fill[f]
            # print(template_filled)

        compiled_instructions = compiled_instructions + [template_filled]
    return(compiled_instructions)


"""
Compile to get instructions instance
"""
instriction_compiled_1 = compile_instruction_set(instriction_instance_1)

"""
Pipeline that loops over instructions and triggers methods in class using getattr
"""
def excecute_pipeline(c,Instructions):
    """
    The above assumes that the instruction set is an attibute of the class. this may not be the case, so here is one where you can pass any instruction set.
    """
    for I in Instructions:
        """
        Passing any params as kwags and triggering method using name in template.
        """
        getattr(c, "%s" % I["method"])(input = I["method_args"])


"""
Run the pipeline
"""
excecute_pipeline(y,instriction_compiled_1)

"""
Check Results.
"""
print(y.name)
print(y.history)
print(y.forecast)
我疯了吗?我是否应该使用某种类方法来修改一个动态触发这些的管道方法,如果是,如何修改?这会回来咬我吗

抱歉发了这么长的帖子,提前谢谢