Python : 检测测试数据中的EOF
Python : Detect EOF in test data
我在 python 中通过自动化测试时遇到了问题。而且我认为原因是我的程序没有在测试数据结束时结束。
此程序使用如下命令在两个 IP 地址之间建立网络:
B 100.100.100.1 100.100.100.2
表示:在IP1和IP2之间建立双向连接
还有第二个命令来测试连接是否存在。
T 100.100.100.1 100.100.100.3
如果可能,程序打印 'T',否则打印 'N'。
假设我们得到了如下条目数据:
B 100.100.100.1 100.100.100.2
B 100.100.100.1 100.100.100.3
B 100.100.100.10 100.100.100.11
T 100.100.100.1 100.100.100.3
T 100.100.100.10 100.100.100.2
T 100.100.100.10 100.100.100.11
B 100.100.100.11 100.100.100.2
T 100.100.100.10 100.100.100.3
T 100.100.100.100 100.100.100.103
那么预期的输出是:
T
N
T
T
N
我的程序打印出正确的值,但如果测试数据结束并工作直到检测到输入中的空行,则不会依赖。但它需要在命令行中再输入 1 'Enter' 才能发送此行,而自动测试不会发送它。
我想我需要一次读取所有输入数据,但我不知道如何忽略每行末尾的 '\n' 并在行尾仅检测 1 'enter'输入。
希望问题描述清楚。这是代码:
from collections import defaultdict
class Graph:
def __init__(self,vertices):
self.V= vertices
self.graph = defaultdict(list)
def addVerticle(self):
self.V+=1
def addEdge(self,u,v):
self.graph[u].append(v)
def graph_keys(self):
return self.graph.keys()
def isReachable(self, s, d):
visited =[False]*(self.V)
queue=[]
queue.append(s)
visited[s] = True
while queue:
n = queue.pop(0)
if n == d:
return True
for i in self.graph[n]:
if visited[i] == False:
queue.append(i)
visited[i] = True
return False
ip_list={}
counter=0
g = Graph(0)
while True:
u_in = [x for x in input().split()]
if not u_in:
break
action = u_in[0]
start = u_in[1]
end = u_in[2]
if action == 'B':
if start not in ip_list.keys():
ip_list[start]=counter
counter+=1
if end not in ip_list.keys():
ip_list[end]=counter
counter+=1
start = ip_list[start]
end = ip_list[end]
if start not in g.graph_keys():
g.addVerticle()
if end not in g.graph_keys():
g.addVerticle()
g.addEdge(start,end)
g.addEdge(end,start)
elif action =='T':
if start not in ip_list.keys() or end not in ip_list.keys():
print('N')
continue
if g.isReachable(ip_list[start],ip_list[end]):
print('T')
else:
print('N')
下方(假设输入来自文件)
with open('input.txt') as f:
lines = [l.strip() for l in f.readlines()]
for line in lines:
print(line)
# TODO - your logic goes here
input.txt
B 100.100.100.1 100.100.100.2
B 100.100.100.1 100.100.100.3
B 100.100.100.10 100.100.100.11
T 100.100.100.1 100.100.100.3
T 100.100.100.10 100.100.100.2
T 100.100.100.10 100.100.100.11
B 100.100.100.11 100.100.100.2
T 100.100.100.10 100.100.100.3
T 100.100.100.100 100.100.100.103
我在 python 中通过自动化测试时遇到了问题。而且我认为原因是我的程序没有在测试数据结束时结束。
此程序使用如下命令在两个 IP 地址之间建立网络:
B 100.100.100.1 100.100.100.2
表示:在IP1和IP2之间建立双向连接
还有第二个命令来测试连接是否存在。
T 100.100.100.1 100.100.100.3
如果可能,程序打印 'T',否则打印 'N'。
假设我们得到了如下条目数据:
B 100.100.100.1 100.100.100.2
B 100.100.100.1 100.100.100.3
B 100.100.100.10 100.100.100.11
T 100.100.100.1 100.100.100.3
T 100.100.100.10 100.100.100.2
T 100.100.100.10 100.100.100.11
B 100.100.100.11 100.100.100.2
T 100.100.100.10 100.100.100.3
T 100.100.100.100 100.100.100.103
那么预期的输出是:
T
N
T
T
N
我的程序打印出正确的值,但如果测试数据结束并工作直到检测到输入中的空行,则不会依赖。但它需要在命令行中再输入 1 'Enter' 才能发送此行,而自动测试不会发送它。
我想我需要一次读取所有输入数据,但我不知道如何忽略每行末尾的 '\n' 并在行尾仅检测 1 'enter'输入。
希望问题描述清楚。这是代码:
from collections import defaultdict
class Graph:
def __init__(self,vertices):
self.V= vertices
self.graph = defaultdict(list)
def addVerticle(self):
self.V+=1
def addEdge(self,u,v):
self.graph[u].append(v)
def graph_keys(self):
return self.graph.keys()
def isReachable(self, s, d):
visited =[False]*(self.V)
queue=[]
queue.append(s)
visited[s] = True
while queue:
n = queue.pop(0)
if n == d:
return True
for i in self.graph[n]:
if visited[i] == False:
queue.append(i)
visited[i] = True
return False
ip_list={}
counter=0
g = Graph(0)
while True:
u_in = [x for x in input().split()]
if not u_in:
break
action = u_in[0]
start = u_in[1]
end = u_in[2]
if action == 'B':
if start not in ip_list.keys():
ip_list[start]=counter
counter+=1
if end not in ip_list.keys():
ip_list[end]=counter
counter+=1
start = ip_list[start]
end = ip_list[end]
if start not in g.graph_keys():
g.addVerticle()
if end not in g.graph_keys():
g.addVerticle()
g.addEdge(start,end)
g.addEdge(end,start)
elif action =='T':
if start not in ip_list.keys() or end not in ip_list.keys():
print('N')
continue
if g.isReachable(ip_list[start],ip_list[end]):
print('T')
else:
print('N')
下方(假设输入来自文件)
with open('input.txt') as f:
lines = [l.strip() for l in f.readlines()]
for line in lines:
print(line)
# TODO - your logic goes here
input.txt
B 100.100.100.1 100.100.100.2
B 100.100.100.1 100.100.100.3
B 100.100.100.10 100.100.100.11
T 100.100.100.1 100.100.100.3
T 100.100.100.10 100.100.100.2
T 100.100.100.10 100.100.100.11
B 100.100.100.11 100.100.100.2
T 100.100.100.10 100.100.100.3
T 100.100.100.100 100.100.100.103