SCRUM-98 file reader #13
@@ -24,14 +24,18 @@ class FileReader:
|
||||
self.z_idx = file_header.index('Z')
|
||||
|
||||
def getNextValue(self):
|
||||
|
|
||||
while True:
|
||||
row = next(self.file_reader, None)
|
||||
if row is None:
|
||||
self._rewind_file()
|
||||
row = next(self.file_reader)
|
||||
continue
|
||||
try:
|
||||
x = int(row[self.x_idx])
|
||||
y = int(row[self.y_idx])
|
||||
z = int(row[self.z_idx])
|
||||
return Accelerometer(x=x, y=y, z=z)
|
||||
except Exception as e:
|
||||
continue
|
||||
|
||||
def _rewind_file(self):
|
||||
self.file.seek(0)
|
||||
|
||||
Reference in New Issue
Block a user
This function is prone to crashing on unexpected file lines. Instead of trying to adapt to a specific file format, it might be better to use try/catch and deciding what to do based on that. The code may look like so:
This way, in case the file is structured differently, has gaps or other mistakes, the function will silently skip over them and return the next valid group of values.
As an improvement, it may be way better to create a custom reader rather than relying on the csv.reader to make the script resilient against any possible .csv file failures
Ok, I'll try it