31 lines
1008 B
Python
31 lines
1008 B
Python
import csv
|
|
from typing import Dict, List
|
|
|
|
def _match_record(pattern: Dict[str, str], item: Dict[str, str]) -> float:
|
|
return all(k in item and item[k]==val for k, val in pattern.items())
|
|
|
|
def check_csv(result: str, rules: Dict[str, List[Dict[str, str]]]) -> float:
|
|
"""
|
|
Args:
|
|
result (str): path to csv file
|
|
rules (Dict[str, List[Dict[str, str]]]): dict like
|
|
{
|
|
"expect": [{key: value}]
|
|
"unexpect": [{key: value}]
|
|
}
|
|
|
|
Returns:
|
|
float
|
|
"""
|
|
|
|
expect_metrics = [False] * len(rules.get("expect", []))
|
|
unexpect_metric = True
|
|
with open(result) as f:
|
|
reader = csv.DictReader(f)
|
|
|
|
for rcd in reader:
|
|
for i, r in enumerate(rules.get("expect", [])):
|
|
expect_metrics[i] = expect_metrics[i] or _match_record(r, rcd)
|
|
unexpect_metric = unexpect_metric and all(_match_record(r, rcd) for r in rules.get("unexpect", []))
|
|
return float(all(expect_metrics) and unexpect_metric)
|