Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更好的可维护方式来组合来自多个选项的字符串_Python_Python 2.7_Dry - Fatal编程技术网

Python 更好的可维护方式来组合来自多个选项的字符串

Python 更好的可维护方式来组合来自多个选项的字符串,python,python-2.7,dry,Python,Python 2.7,Dry,我有一个场景,用户可以传入多个选项。对于传入的每个选项,我将获取文本,然后最终合并来自多个选项的文本并返回单个字符串。以下是我今天接受的三种选择的做法。代码看起来已经无法维护,随着我添加更多选项,逻辑将变得更糟: if (len(self.options.passedin.split(",")) > 0): #multiple options were passed in ops = self.options.passedin.split(",") for op in ops:

我有一个场景,用户可以传入多个选项。对于传入的每个选项,我将获取文本,然后最终合并来自多个选项的文本并返回单个字符串。以下是我今天接受的三种选择的做法。代码看起来已经无法维护,随着我添加更多选项,逻辑将变得更糟:

if (len(self.options.passedin.split(",")) > 0): #multiple options were passed in
  ops = self.options.passedin.split(",")
  for op in ops:
     if (op == "option1"):
         op1_text = get_text_for_option1()
     elif (op == "option2"):
         op2_text = get_text_for_option2()
     elif (op == "option3"):
         op3_text = get_text_for_option3()

   #all three were passed in
   if ("option1" in ops and "option2" in ops and "option3" in ops):
      op1_op2 = op1_text + " " + ' '.join(w for w in op1_text.split() if w not in op2_text.split())
      op3_op1_op2 = op1_op2 + " " + ' '.join(w for w in op1_op2.split() if w not in op3_text.split())
      return op3_op1_op2
   #option1 and option2 were passed in
   elif ("option1" in ops and "option2" in ops and "option3" not in ops):
      return op1_text + " " + ' '.join(w for w in op1_text.split() if w not in op2_text.split())
   #option1 and option3 were passed in
   elif ("option1" in ops and "option3" in ops and "option2" not in ops):
      return op1_text + " " + ' '.join(w for w in op1_text.split() if w not in op3_text.split())
   #option2 and option3 were passed in
   elif ("option2" in ops and "option3" in ops and "option1" not in ops):
      return op2_text + " " + ' '.join(w for w in op2_text.split() if w not in op3_text.split())

无法组合选项1的方法
get\u text\u
get\u text\u for\u option 2
get\u text\u for \u option 3

使用
dict
将选项名称映射到相应的函数,该函数返回选项文本,将它们连接在一起,然后使用唯一的单词,例如:

from collections import OrderedDict

options = {
    'option1': get_text_for_option1,
    'option2': get_text_for_option2,
    'option3': get_text_for_option3
}

input_text = 'option3,option1,option2'

all_text = ' '.join(options[opt]() for opt in input_text.split(','))
unique = ' '.join(OrderedDict.fromkeys(all_text.split()))

使用
dict
将选项名称映射到相应的函数,该函数返回选项文本,将它们连接在一起,然后使用唯一的单词,例如:

from collections import OrderedDict

options = {
    'option1': get_text_for_option1,
    'option2': get_text_for_option2,
    'option3': get_text_for_option3
}

input_text = 'option3,option1,option2'

all_text = ' '.join(options[opt]() for opt in input_text.split(','))
unique = ' '.join(OrderedDict.fromkeys(all_text.split()))

为什么需要将它们组合在一起?我在这里看到一些不必要的/不正确的东西(
len(something)>0
而不是
something
&
而不是
在编辑之前,
'option2'在ops
中是最后一个选项…)。我建议花更多的时间来重构和改进您首先能做的一切。如果我以后再添加两个选项,那么
If/elif
逻辑将变得越来越糟糕。您可以说“传入选项”。您是指程序的命令行参数吗?如果是,请考虑使用内置的<代码> AgPARSE> <代码>库。@ Jokabi我使用AgPARSE。其中一个选项可以是CSV。像
python myprogra.py--someoption“option1,option2,option3”
为什么需要将它们组合起来?我在这里看到一些不必要/不正确的东西(
len(某物)>0
而不是
某物
和&
而不是
在编辑前,
'option2'在ops中
option2不在ops中
用于最后一个选项…)。我建议花更多的时间来重构和改进您首先能做的一切。如果我以后再添加两个选项,那么
If/elif
逻辑将变得越来越糟糕。您可以说“传入选项”。您是指程序的命令行参数吗?如果是,请考虑使用内置的<代码> AgPARSE> <代码>库。@ Jokabi我使用AgPARSE。其中一个选项可以是CSV。像dict中的
python myprogra.py--someoption“option1,option2,option3”
这些字符串可以替换为函数调用吗?例如,将
这是选项一五二
替换为
获取选项1()的文本这是选项一五二
替换为
获取选项1()的文本
@Anthony已更改为使用函数