SCRUM-98 file reader #13

Merged
VladiusVostokus merged 11 commits from lab5/kovalenko-SCRUM-98-FileReader into dev 2026-03-08 17:48:13 +02:00
3 changed files with 1479 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,7 @@
from dataclasses import dataclass
@dataclass
class Accelerometer:
x: int
y: int
z: int

50
MapView/fileReader.py Normal file
View File

@@ -0,0 +1,50 @@
from csv import reader
import config
from domain.accelerometer import Accelerometer
class FileReader:
def __init__(
self, data_filename: str,
) -> None:
self.file_path = data_filename
pass
def read(self):
return self.getNextValue()
def startReading(self, *args, **kwargs):
self.file = open(self.file_path, newline='')
self.file_reader = reader(self.file, skipinitialspace=True)
file_header = next(self.file_reader)
self.x_idx = file_header.index('X')
self.y_idx = file_header.index('Y')
self.z_idx = file_header.index('Z')
def getNextValue(self):
hasslesstech commented 2026-03-03 17:41:56 +02:00 (Migrated from github.com)
Review

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:

while True:
    row = next(self.file_reader, None)

    if row is None:
            self._rewind_file()
            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)
    exception Exception as e:
        continue

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

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: ``` while True: row = next(self.file_reader, None) if row is None: self._rewind_file() 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) exception Exception as e: continue ``` 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
VladiusVostokus commented 2026-03-03 18:52:57 +02:00 (Migrated from github.com)
Review

Ok, I'll try it

Ok, I'll try it
while True:
row = next(self.file_reader, None)
if row is None:
self._rewind_file()
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)
self.file_reader = reader(self.file)
next(self.file_reader)
def stopReading(self, *args, **kwargs):
if self.file:
self.file.close()
self.file_reader = None