避免 if 情况 python

Avoid if cases python

我有以下代码:

  1. 检查调度策略
  2. 根据调度策略
  3. 检查关联的调度周期

目前我只有两个调度策略,因此我的代码非常简单:

def test_scheduling_parameters(schedulingStrategy: str, schedulingPeriod: str):
    """
    :param schedulingStrategy:Could be CRON_DRIVEN or TIMER_DRIVEN
    :param schedulingPeriod: If CRON would be in the form of '* * * * * ?', else XX sec (or min)
    :return:
    """
    sche_strat = ['TIMER_DRIVEN', 'CRON_DRIVEN']
    if schedulingStrategy in sche_strat:
        if schedulingStrategy == 'TIMER_DRIVEN' and re.match('\d+\s(sec|min)$', schedulingPeriod):
            return True
        elif schedulingStrategy == "CRON_DRIVEN" and croniter.is_valid(schedulingPeriod[:-2]):
            return True
        else:
            return "Error: Wrong scheduling period"
    else:
        return "Error: Wrong scheduling strategy"

我想知道如果我有更多的调度策略而不乘以 if cases 怎么办?

我想有一种字典,其中包含策略名称和验证函数,这是最好的方法吗?

类似下面的内容

def test_scheduling_parameters(schedulingStrategy: str, schedulingPeriod: str):
    """
    :param schedulingStrategy:Could be CRON_DRIVEN or TIMER_DRIVEN
    :param schedulingPeriod: If CRON would be in the form of '* * * * * ?', else XX sec (or min)
    :return:
    """
    sche_strat = ['TIMER_DRIVEN', 'CRON_DRIVEN']
    if schedulingStrategy == 'TIMER_DRIVEN' and re.match('\d+\s(sec|min)$', schedulingPeriod):
        return True, ''
    elif schedulingStrategy == 'CRON_DRIVEN' and croniter.is_valid(schedulingPeriod[:-2]):
        return True, ''
    else:
        return False, "Wrong scheduling period" if schedulingStrategy in sche_strat else 'Wrong scheduling strategy'

使用函数字典:

import sys
import re
import croniter

def checkstrat_timerdriven(schedulingPeriod):
  return re.match('\d+\s(sec|min)$', schedulingPeriod)

def checkstrat_crondriven(schedulingPeriod):
  return croniter.is_valid(schedulingPeriod[:-2])

def checkstrat_wrongstrat(schedulingPeriod):
  print("Error: Wrong scheduling strategy", file=sys.stderr)
  return False

def test_scheduling_parameters(schedulingStrategy: str, schedulingPeriod: str):
    """
    :param schedulingStrategy:Could be CRON_DRIVEN or TIMER_DRIVEN
    :param schedulingPeriod: If CRON would be in the form of '* * * * * ?', else XX sec (or min)
    :return:
    """
    checkstrat_dict = {'TIMER_DRIVEN': checkstrat_timerdriven, 'CRON_DRIVEN': checkstrat_crondriven}
    checkstrat = checkstrat_dict.get(schedulingStrategy, checkstrat_wrongstrat)
    return checkstrat(schedulingPeriod)

如果行数是您唯一关心的问题,请使用预定义的字典并将您的键保留为 strategy_name,并将值保留为 validator,如下所示,

def test_scheduling_parameters(schedulingStrategy: str, schedulingPeriod: str):
    """
    :param schedulingStrategy:Could be CRON_DRIVEN or TIMER_DRIVEN
    :param schedulingPeriod: If CRON would be in the form of '* * * * * ?', else XX sec (or min)
    :return:
    """
    scheduling_validation = {
                                   'TIMER_DRIVEN': re.match('\d+\s(sec|min)$', schedulingPeriod),
                                   'CRON_DRIVEN': croniter.is_valid(schedulingPeriod[:-2])
                            }
    if schedulingStrategy in scheduling_validation .keys():
        if scheduling_validation[schedulingStrategy]:
            return True, ''
        else:
            return False, 'Wrong Scheduling Period'
    else:
        return False, 'Wrong Scheduling Strategy'