Python调度程序和特定于场景的函数和子函数

时间:2018-03-11 00:03:22

标签: python python-3.x

我正在尝试用Python编写几个函数(调度程序+特定于场景的函数和子函数),如下所示:

def dispatcher(event):
    if event['name'] == 'func_a':
        return func_a(event['data'])
    # several elif statements here
    elif event['name'] == 'func_n':
        return func_n(event['data'])


def func_a(data):
    """ multi-scenario function returning dict"""
    # Scenario A
    if data['certain_param'] == 'certain value':
        # do something
        # that takes several lines of code
        return {'result': 'scenario A'}

    # Scenario B
    elif data['certain_param'] == 'another value':
        # do something else
        # that takes several lines of code
        return {'result': 'scenario B'}

    # some other elif scenarios go here

    return {'result': 'Fail'}


# func_b(), func_c(), .... go here (similar to func_a())


def func_n(data):
    """ similar logic to funcs a, b, ...."""
    pass

我想将很长的 func_a()分成几个子函数(每个场景一个)。以下是两个选项:

# makes 2 calls to each scenario sub-function (not ideal)
def func_a(data):
    """ same func_a() above"""
    # Scenario A
    if func_a_scenario_a(data):
        return func_a_scenario_a(data)

    # other elifs

    # Scenario N
    elif func_a_scenario_n(data):
        return func_a_scenario_n(data)

编写 func_a()的另一种方法是:

# Not very Pythonic
def func_a(data):

    result = func_a_scenario_a(data)
    if result:
        return result

    result = func_a_scenario_b(data)
    if result:
        return result

    # repeat for every scenario

更好的方法是什么?也许使用eval('some_func()')?或者是其他东西?感谢。

1 个答案:

答案 0 :(得分:0)

您可以创建一个装饰器,然后可以确定传递给其包装函数的内容。装饰器可以存储一个以函数名作为键的字典,以及一个与该键具有相同名称的函数传递的关联值:

def dispatcher(f):
   registry = {'func_a':"arbitrary_data", 'func_n':"arbitrary_data_1"}
   def wrapper(data):
      return f(registry[f.__name__])
   return wrapper

@dispatcher
def func_a(data):
  #do something with data
  return 

@dispatcher
def func_n(data):
  #do something with data
  return 

func_a("somedata")
func_n("someotherdata") #functions called be called normally