Skip to content

Commit

Permalink
dont waste first csv reading
Browse files Browse the repository at this point in the history
  • Loading branch information
avaldebe committed Jul 7, 2021
1 parent dfc77f3 commit ac9c7de
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/pms/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions tests/captured_data/MCU680.csv
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/captured_data/MHZ19B.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
time, CO2
1625419056, 636
1625419066, 636
1625419076, 636
1625419086, 636
Expand Down
1 change: 1 addition & 0 deletions tests/captured_data/PMS3003.csv
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/captured_data/PMS5003T.csv
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/captured_data/PMSx003.csv
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/captured_data/SDS01x.csv
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/captured_data/SDS198.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
time, pm100
1599656310, 16.0
1599656320, 19.0
1599656330, 22.0
1599656340, 26.0
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ac9c7de

Please sign in to comment.