From ac9c7de0d2c0faea939996a1053c3a3a4e763cd9 Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Wed, 7 Jul 2021 14:32:47 +0200 Subject: [PATCH] dont waste first csv reading --- src/pms/cli.py | 14 ++++++++------ tests/captured_data/MCU680.csv | 1 + tests/captured_data/MHZ19B.csv | 1 + tests/captured_data/PMS3003.csv | 1 + tests/captured_data/PMS5003T.csv | 1 + tests/captured_data/PMSx003.csv | 1 + tests/captured_data/SDS01x.csv | 1 + tests/captured_data/SDS198.csv | 1 + tests/conftest.py | 2 +- 9 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/pms/cli.py b/src/pms/cli.py index 2328352..44e80ce 100644 --- a/src/pms/cli.py +++ b/src/pms/cli.py @@ -94,10 +94,11 @@ def serial( for n, raw in enumerate(reader(raw=True)): echo(raw.hexdump(n)) elif format: - if format == "csv": - obs = next(reader()) - echo(f"{obs:header}") + print_header = format == "csv" for obs in reader(): + if print_header: + echo(f"{obs:header}") + print_header = False echo(f"{obs:{format}}") else: # pragma: no cover for obs in reader(): @@ -121,10 +122,11 @@ def csv( if not capture: logger.debug(f"capture {sensor_name} observations to {path}") # add header to new files - if path.stat().st_size == 0: - obs = next(reader()) - csv.write(f"{obs:header}\n") + print_header = path.stat().st_size == 0 for obs in reader(): + if print_header: + csv.write(f"{obs:header}\n") + print_header = False csv.write(f"{obs:csv}\n") else: logger.debug(f"capture {sensor_name} messages to {path}") diff --git a/tests/captured_data/MCU680.csv b/tests/captured_data/MCU680.csv index c0199e2..76d5662 100644 --- a/tests/captured_data/MCU680.csv +++ b/tests/captured_data/MCU680.csv @@ -1,4 +1,5 @@ time, temp, rhum, pres, IAQ_acc, IAQ, gas, alt +1601220052, 24.3, 27.6, 1012.57, 1, 25, 40.7, 5 1601220062, 24.2, 27.6, 1012.53, 1, 25, 42.1, 5 1601220072, 24.2, 27.6, 1012.53, 1, 25, 43.8, 5 1601220082, 24.2, 27.6, 1012.55, 1, 25, 45.1, 5 diff --git a/tests/captured_data/MHZ19B.csv b/tests/captured_data/MHZ19B.csv index 6113530..67b685c 100644 --- a/tests/captured_data/MHZ19B.csv +++ b/tests/captured_data/MHZ19B.csv @@ -1,4 +1,5 @@ time, CO2 +1625419056, 636 1625419066, 636 1625419076, 636 1625419086, 636 diff --git a/tests/captured_data/PMS3003.csv b/tests/captured_data/PMS3003.csv index e096c4c..6250edd 100644 --- a/tests/captured_data/PMS3003.csv +++ b/tests/captured_data/PMS3003.csv @@ -1,4 +1,5 @@ time, raw01, raw25, raw10, pm01, pm25, pm10 +1601219770, 0, 0, 0, 0.0, 0.0, 0.0 1601219780, 0, 0, 0, 0.0, 0.0, 0.0 1601219790, 0, 0, 0, 0.0, 0.0, 0.0 1601219800, 0, 0, 0, 0.0, 0.0, 0.0 diff --git a/tests/captured_data/PMS5003T.csv b/tests/captured_data/PMS5003T.csv index cb9cf77..dee5112 100644 --- a/tests/captured_data/PMS5003T.csv +++ b/tests/captured_data/PMS5003T.csv @@ -1,4 +1,5 @@ time, raw01, raw25, raw10, pm01, pm25, pm10, n0_3, n0_5, n1_0, n2_5, temp, rhum +1612247277, 23, 39, 41, 22.0, 35.0, 41.0, 41.04, 11.93, 2.78, 0.12, 21.2, 22.4 1612247287, 21, 36, 36, 20.0, 33.0, 36.0, 35.28, 10.55, 2.28, 0.14, 21.2, 22.4 1612247297, 21, 37, 38, 20.0, 34.0, 38.0, 38.40, 11.26, 2.50, 0.22, 21.2, 22.4 1612247307, 20, 35, 42, 19.0, 32.0, 42.0, 37.50, 10.79, 2.62, 0.30, 21.2, 22.3 diff --git a/tests/captured_data/PMSx003.csv b/tests/captured_data/PMSx003.csv index 2982a00..4e531a1 100644 --- a/tests/captured_data/PMSx003.csv +++ b/tests/captured_data/PMSx003.csv @@ -1,4 +1,5 @@ time, raw01, raw25, raw10, pm01, pm25, pm10, n0_3, n0_5, n1_0, n2_5, n5_0, n10_0 +1601219770, 0, 8, 8, 0.0, 8.0, 8.0, 2.10, 0.70, 0.45, 0.30, 0.00, 0.00 1601219780, 0, 7, 7, 0.0, 7.0, 7.0, 2.10, 0.70, 0.45, 0.30, 0.00, 0.00 1601219790, 0, 7, 7, 0.0, 7.0, 7.0, 1.89, 0.63, 0.42, 0.27, 0.00, 0.00 1601219800, 0, 7, 7, 0.0, 7.0, 7.0, 1.80, 0.60, 0.39, 0.24, 0.00, 0.00 diff --git a/tests/captured_data/SDS01x.csv b/tests/captured_data/SDS01x.csv index 5709bce..709323b 100644 --- a/tests/captured_data/SDS01x.csv +++ b/tests/captured_data/SDS01x.csv @@ -1,4 +1,5 @@ time, pm25, pm10 +1601230280, 0.6, 0.6 1601230290, 0.9, 0.9 1601230300, 0.9, 0.9 1601230310, 0.9, 0.9 diff --git a/tests/captured_data/SDS198.csv b/tests/captured_data/SDS198.csv index d6486ef..8363ba8 100644 --- a/tests/captured_data/SDS198.csv +++ b/tests/captured_data/SDS198.csv @@ -1,4 +1,5 @@ time, pm100 +1599656310, 16.0 1599656320, 19.0 1599656330, 22.0 1599656340, 26.0 diff --git a/tests/conftest.py b/tests/conftest.py index f3ee5e2..dba909c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -51,7 +51,7 @@ def obs(self) -> Generator[ObsData, None, None]: def options(self, command: str) -> List[str]: samples = len(self.value) - if "csv" in command or command == "mqtt": + if command == "mqtt": samples -= 1 capture = f"-m {self.name} -n {samples} -i 0" cmd = dict(