1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

instrument/daq: Convert reading rows from all files to a generator

Instead of calling _read_next_rows() before the while and at the end,
it's simpler to read the rows in a for loop and have _read_rows() be a
generator.
This commit is contained in:
Javi Merino 2020-02-25 14:27:43 +00:00 committed by Marc Bonnici
parent dcab0b3718
commit 72ded188fa

View File

@ -16,7 +16,7 @@
import os import os
import shutil import shutil
import tempfile import tempfile
from itertools import chain from itertools import chain, zip_longest
from devlib.instrument import Instrument, MeasurementsCsv, CONTINUOUS from devlib.instrument import Instrument, MeasurementsCsv, CONTINUOUS
from devlib.exception import HostError from devlib.exception import HostError
@ -127,23 +127,18 @@ class DaqInstrument(Instrument):
channel_order.extend(['{}_{}'.format(site, kind) channel_order.extend(['{}_{}'.format(site, kind)
for kind in next(reader)]) for kind in next(reader)])
def _read_next_rows(): def _read_rows():
parts = [] row_iter = zip_longest(*site_readers.values(), fillvalue=(None, None))
for reader in site_readers.values(): for raw_row in row_iter:
try: raw_row = list(chain.from_iterable(raw_row))
parts.extend(next(reader)) yield raw_row
except StopIteration:
parts.extend([None, None])
return list(chain(parts))
with csvwriter(outfile) as writer: with csvwriter(outfile) as writer:
field_names = [c.label for c in self.active_channels] field_names = [c.label for c in self.active_channels]
writer.writerow(field_names) writer.writerow(field_names)
raw_row = _read_next_rows() for raw_row in _read_rows():
while any(raw_row):
row = [raw_row[channel_order.index(f)] for f in field_names] row = [raw_row[channel_order.index(f)] for f in field_names]
writer.writerow(row) writer.writerow(row)
raw_row = _read_next_rows()
return MeasurementsCsv(outfile, self.active_channels, self.sample_rate_hz) return MeasurementsCsv(outfile, self.active_channels, self.sample_rate_hz)
finally: finally: