Python3: 关于预期列表的预期问题
Python3: pexpect issue about expect list
模板来了,在while循环中,变量"index"是一个列表,所以,我看不懂代码"if index == 0"的意思,index[0] = "suc", 索引[1]="fail" ?请尽量说清楚。
import pexpect
while True:
index = child.expect(["suc","fail",pexpect.TIMEOUT])
if index == 0:
break
elif index == 1:
return False
elif index == 2:
pass #continue to wait
expect() 方法returns 匹配模式的索引。索引不是列表。
根据 manual:
expect(pattern, timeout=-1, searchwindowsize=-1, async=False)
This seeks through the stream until a pattern is matched. The pattern is overloaded and may take several
types. The pattern can be a StringType, EOF, a compiled re, or a list of any of those types. Strings will be
compiled to re types. This returns the index into the pattern list. If the pattern was not a list this returns
index 0 on a successful match. This may raise exceptions for EOF or TIMEOUT. To avoid the EOF or
TIMEOUT exceptions add EOF or TIMEOUT to the pattern list. That will cause expect to match an EOF
or TIMEOUT condition instead of raising an exception.
If you pass a list of patterns and more than one matches, the first match in the stream is chosen. If more
than one pattern matches at that point, the leftmost in the pattern list is chosen.
模板来了,在while循环中,变量"index"是一个列表,所以,我看不懂代码"if index == 0"的意思,index[0] = "suc", 索引[1]="fail" ?请尽量说清楚。
import pexpect
while True:
index = child.expect(["suc","fail",pexpect.TIMEOUT])
if index == 0:
break
elif index == 1:
return False
elif index == 2:
pass #continue to wait
expect() 方法returns 匹配模式的索引。索引不是列表。
根据 manual:
expect(pattern, timeout=-1, searchwindowsize=-1, async=False)
This seeks through the stream until a pattern is matched. The pattern is overloaded and may take several types. The pattern can be a StringType, EOF, a compiled re, or a list of any of those types. Strings will be compiled to re types. This returns the index into the pattern list. If the pattern was not a list this returns index 0 on a successful match. This may raise exceptions for EOF or TIMEOUT. To avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern list. That will cause expect to match an EOF or TIMEOUT condition instead of raising an exception.
If you pass a list of patterns and more than one matches, the first match in the stream is chosen. If more than one pattern matches at that point, the leftmost in the pattern list is chosen.