<p>这里有一些严重的设计问题,但您要做的是简化代码。不要试图一次得到所有参数的组合,你应该一次得到一个完整的集合,并将它们添加到一个列表中,这样你就有了一个参数列表。你知道吗</p>
<p><strong>用户输入.py</strong></p>
<pre><code>import testSetup # imports go on top of a file
def get_param_set():
"""
Returns a list of parameters to be used for testing
"""
input_messages = [
"Please select:\n 1. Complete power cycle including both test PC and SSD\n 2. Partial power cycle only including SSD\n",
"Number of cycles: ",
"Rise time: ",
"Fall time: ",
"Overshoot voltage: ",
"Overshoot time: ",
"Input voltage: ",
"Off time between SSD power cycles: ",
"Off time between PC power cycles (if partial power cycle selected, enter none): ",
"Temperature: ",
"Stop test upon failure? Y/N\n"
]
return [raw_input(message) for message in input_messages]
</code></pre>
<p>或者,您可以使用字典,这将使跟踪您的参数更加容易:</p>
<pre><code>def get_param_set():
"""
Returns a list of parameters to be used for testing
"""
input_messages = {
'complete_power_cycle': "Please select:\n 1. Complete power cycle including both test PC and SSD\n 2. Partial power cycle only including SSD\n",
'cycles': "Number of cycles: ",
'rise_time': "Rise time: ",
'fall_time': "Fall time: ",
'overshoot_limit': "Overshoot voltage: ",
'overshoot_time': "Overshoot time: ",
'input_voltage': "Input voltage: ",
'ssd_off_Time': "Off time between SSD power cycles: ",
'pc_off_time': "Off time between PC power cycles (if partial power cycle selected, enter none): ",
'temperature': "Temperature: ",
'test_stop': "Stop test upon failure? Y/N\n"
}
return {key: raw_input(value) for key, value in input_messages.items()} # you may want to use int(raw_input()) since it looks like most of these will be numbers and 4 != '4'
</code></pre>
<p>现在,您将拥有一个如下所示的词典:</p>
<pre><code>{'cycles' '4', 'rise_time': '42', ...}
</code></pre>
<p>您只需知道参数名即可访问这些值,而不必知道它的索引,因为索引可能会变得混乱:</p>
<pre><code>>>> param_dict = get_param_set()
>>> param_dict.get('cycles', '0') # this will give you the value at the key 'cycles' if it's in the dictionary or '0' if it's not.
>>> param_dict['cycles'] # this will also give you the value at the key cycles but it will raise an exception if that key hasn't been set.
</code></pre>
<p><strong>测试设置.py</strong></p>
<pre><code>import userInput
def test_combos():
params = userInput.get_params()
print(params)
run_your_test_here(*params)
</code></pre>
<p>如果要收集多组参数:</p>
<pre><code>def test_combos(num_of_sets):
param_sets = [userInput.get_params() for _ in range(num_of_sets)]
print(params)
for param_set in param_sets:
run_your_test_here(*param_set) # the star unpacks the list into positional arguments
</code></pre>
<p>列表解包的快速示例:</p>
<pre><code>>>> def foo(a, b, c):
... print(a)
... print(b)
... print(c)
...
>>> lst = [1,2,3]
>>> foo(*lst)
1
2
3
</code></pre>
<p>字典解包:</p>
<pre><code>>>> def foo(a, b, c):
... print(a)
... print(b)
... print(c)
...
>>> bar = {'a': 1, 'c': 3, 'b': 2}
>>> foo(**bar)
1
2
3
</code></pre>
<p>请注意,字典的顺序并不重要(字典是无序的集合),只要字典映射中的键与参数名匹配,就可以像执行<code>foo(a=1, b=2, c=3)</code>一样使用它</p>
<p>因此,您可以将一个列表传递给具有多个位置参数的函数。只要<code>len(list)</code>等于位置参数的数量,并且它们的顺序相同,所有的事情都会像你一个接一个地传递它们一样工作。<a href="https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists" rel="nofollow">Read more here</a>。你知道吗</p>
<p>在函数内指定的变量在函数作用域外不可用,除非您将其返回并保存到其他变量中。它们可以作为类属性来使用。创建一个新类来运行测试无疑是实现目标的另一种方法。你知道吗</p>
<pre><code>class TestCombo(object):
def __init__(self):
# there are better ways to do this but this is simplest to read
self.complete_power_cycle = raw_input("...")
self.cycles = raw_input("Number of cycles: ")
self.rise_time = raw_input("Rise time: ")
self.fall_time = raw_input("Fall time: ")
self.overshoot_limit = raw_input("Overshoot voltage: ")
self.overshoot_time = raw_input("Overshoot time: ")
self.input_voltage = raw_input("Input voltage: ")
self.ssd_off_Time = raw_input("... ")
self.pc_off_time = raw_input("...")
self.temperature = raw_input("Temperature: ")
self.test_stop = raw_input("...")
def run_test_combo(self):
# access class attributes like so
foo = bar(self.cycles, self.rise_time, ...)
print(self.test_stop)
</code></pre>
<p>要使用类:</p>
<pre><code>>>> test_obj = TestCombo()
# your __init__ method will now run and collect all the inputs
>>> test_obj.test_stop
# will return whatever you set test_stop to be
>>> test_obj.run_test_combo()
# will run whatever code you put inside the method run_test_combo()
</code></pre>
<p>因为您似乎正在运行某种测试,所以可以使用内置的<a href="https://docs.python.org/2/library/unittest.html" rel="nofollow">^{<cd3>}</a>模块。你知道吗</p>