python 任务编排

1... 目的,一坨坨的函数选择执行...

2...code

# file m.py
from obj.tsk import Task
from obj.acc import Acc

map_name_cls = {"task": Task, "acc": Acc}

fconf = [
    {"task_type": "task",
     "task_list": ["task", "pre_task", "lf"]},
    {"task_type": "acc",
     "task_list": ["task", "pre_task", "lf"]}
]

if __name__ == '__main__':
    for k in fconf:
        tsk_type = k["task_type"]
        tsk_list = k["task_list"]
        cls = map_name_cls[tsk_type](tsk_list)
        for ts in tsk_list:
            run_flag = True
            try:
                func = getattr(cls, ts)
            except:
                run_flag = False
            if run_flag:
                func()

___

# file obj/tsk.py
class Task:
    def __init__(self, *args):
        pass

    @staticmethod
    def pre_task():
        print("running task pre_task")

    @staticmethod
    def task():
        print("running tsk task")

    @staticmethod
    def post_task():
        print("running tsk post_task")

__

file obj/acc.py 和tsk.py类同

3... output

 

class Base:
    res_all={}
    pass

class A1050(Base):
    def __init__(self):
        self.res_a={}
        print("A1050 init")

    def ma1(self, *args):
        print("ma1 excute")
        self.res_all['key2'] = "key value2"

    def ma2(self, *args):
        print("ma2 excute")
        self.res_all['key1']="key value1"

    def ma3(self, *args):
        print("ma3 excute")
    def __del__(self):
        print("A1050 del excute")

    pass


class B1050(Base):
    def __init__(self):
        self.res_b={}
        print("B1050 init")

    def mb1(self, *args):
        print("mb1 excute")
        self.res_all['key3']="key value3"

    def mb2(self, *args):
        print("mb2 excute")

    def mb3(self, *args):
        print("mb3 excute")
    def __del__(self):
        print("B1050 del excute")

    pass


if __name__ == '__main__':
    # tsk_map = {"A1050": A1050, "B1050": B1050}
    # lst = [{"tsk_name":"A1050","nm_lst" :["ma2"]}, {"tsk_name":"B1050", "nm_lst":["mb1", "mb2"]},{"tsk_name":"A1050","nm_lst":["ma1"]}]
    #
    #
    # for t in lst:
    #     print(t)
    #     cls=tsk_map[t['tsk_name']]
    #     c=cls()
    #
    #     for method in t['nm_lst']:
    #         print(method)
    #         m=c.__getattribute__(method)
    #         m()
    #         print(c.res_all)
    #     del c

    tsk_map = {"A1050": A1050, "B1050": B1050}
    lst = [{"tsk_name":A1050,"nm_lst" :["ma2"]}, {"tsk_name":B1050, "nm_lst":["mb1", "mb2"]},{"tsk_name":A1050,"nm_lst":["ma1"]}]


    for t in lst:
        print(t)
        cls=t['tsk_name']
        c=cls()
        for method in t['nm_lst']:
            print(method)
            m=c.__getattribute__(method)
            m()
            print(c.res_all)
        del c


    pass

  

posted @ 2021-12-15 11:34  voh99800  阅读(201)  评论(0编辑  收藏  举报