python - Python中的递归等待keyPress
问题描述
我有一个 CAN 数据日志文件,我根据行号将其分成两个相等的部分。我使用 播放此文件canplayer
,观察模拟器上的行为,然后按y/n
,其中观察到 y 行为或未观察到 n 行为。
code logic:
1. split file into two parts based on line numbers or entries.
2. play file1, observe behavior
3. if behavior observed, take file1, split into two parts again and follow same procedure
(如果file1中没有观察到行为,请执行以下操作或忽略)
2*. play file2, observe behavior
3*. if behavior observed, take file2, split into two parts again and follow same procedure
我有以下代码:
def runParser(self):
#get the number of lines in file
num_lines = sum(1 for line in open(self.fileName))
print(num_lines)
if(num_lines % 2 == 0):
splitLen = num_lines/2
else:
splitLen = num_lines/2 + 1
self.splitFile(splitLen) # successfully splits the file and outputs two new files.
command = "canplayer -I {}".format(self.out1.name)
#print(command)
os.system(command)
print('Do you see the behaviour? press y/n')
keyboard.on_press(self.accessBehavior)
def accessBehavior(self,event):
if event.name == 'y':
self.fileName = self.out1.name
self.runParser()
if event.name == 'n':
command = "canplayer -I {}".format(self.out2.name)
print(command)
os.system(command)
self.fileName= self.out2.name
self.runParser()
def splitFile(self, len):
self.fin = open(self.fileName,'rb')
self.out1 = open('out{}.log'.format(self.fileCount),'wb')
self.fileCount+=1 # initialized to zero
self.out2 = open('out{}.log'.format(self.fileCount),'wb')
self.fileCount+=1
count=0
for line in self.fin:
count +=1
if count >= len:
#write to new file
self.out2.write(line)
else:
self.out1.write(line)
self.fin.close()
self.out1.close()
self.out2.close()
我面临的问题是 ,
keyboard.on_press(self.accessBehavior)
只等待第一次按下。y/n
我希望程序根据运行命令后看到的行为接受多次按下canplayer ..
因此,如果没有观察到该行为,我按下
n
,并且这些步骤不断重复,直到文件中的所有数据都用完并且文件中只存在一行数据,这会调用该行为。
以下是 CAN 数据文件外观示例:
(1584526791.145716) vcan0 13F#000000050000001F
(1584526791.145719) vcan0 164#0000C01AA8000031
(1584526791.145721) vcan0 17C#0000000010000012
(1584526791.145723) vcan0 18E#00005C
(1584526791.147489) vcan0 183#0000000D0000101D
(1584526791.147526) vcan0 143#6B6B00D1
(1584526791.149458) vcan0 095#800007F400000008
(1584526791.153293) vcan0 166#D0320018
(1584526791.153332) vcan0 158#0000000000000019
(1584526791.153335) vcan0 161#000005500108001C
(1584526791.153337) vcan0 191#010090A1410003
(1584526791.153360) vcan0 244#0000000142
(1584526791.154416) vcan0 133#00000000A7
(1584526791.154447) vcan0 136#000200000000002A
(1584526791.154450) vcan0 13A#0000000000000028
(1584526791.154452) vcan0 13F#000000050000002E
(1584526791.154454) vcan0 164#0000C01AA8000004
(1584526791.156373) vcan0 17C#0000000010000021
.....
.....
.....
解决方案
推荐阅读
- reactjs - React App 出现 MongoDB 错误“MustAuthenticateFirst”
- angular - Angular Material中带有菜单的完整背景图像
- android - Android 使用 Dagger2 和多个项目模块
- vue.js - 在 nuxt 中为默认和错误布局创建文件夹
- r - R 中现有数据的正态分布随机抽样
- javascript - React.Js not rendering in django project
- java - 如何从迷宫求解算法中的错误路径返回?(爪哇)
- java - 自定义 Reactor Netty 主机名验证器
- svg - SVG 径向渐变 - 移动焦点(中心)
- python - Python生成对角线条目为零的矩阵的所有可能组合