[validation] Small refactor for readability.
@@ -61,7 +61,7 @@ import csv
import json
import re
import sys
-from typing import Iterator
+from typing import Iterable
import numpy as np
from absl import app, flags
@@ -105,7 +105,7 @@ flags.DEFINE_string(
FLAGS = flags.FLAGS
-def read_states(in_file) -> Iterator[CompilerEnvState]:
+def read_states(in_file) -> Iterable[CompilerEnvState]:
"""Read the CSV states from stdin."""
data = in_file.readlines()
for line in csv.DictReader(data):
@@ -118,6 +118,16 @@ def read_states(in_file) -> Iterator[CompilerEnvState]:
sys.exit(1)
+def read_states_from_paths(paths: Iterable[str]) -> Iterable[CompilerEnvState]:
+ """Read the states from the given input paths."""
+ for path in paths:
+ if path == "-":
+ yield from read_states(sys.stdin)
+ else:
+ with open(path) as f:
+ yield from read_states(f)
+
+
def state_name(state: CompilerEnvState) -> str:
"""Get the string name for a state."""
return re.sub(r"^benchmark://", "", state.benchmark)
@@ -151,14 +161,7 @@ def stdev(values):
def main(argv):
"""Main entry point."""
# Parse the input states from the user.
- states = []
- for path in argv[1:]:
- if path == "-":
- states += list(read_states(sys.stdin))
- else:
- with open(path) as f:
- states += list(read_states(f))
-
+ states = list(read_states_from_paths(argv[1:]))
if not states:
print(
"No inputs to validate. Pass a CSV file path as an argument, or "
