ver Jan23rdv2
added read_cell_value function to load the real value of an exact excel cell exactly by the coordinate
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
#import operator
|
||||
from numbers import Number
|
||||
from typing import Any, Union, cast, Callable
|
||||
from typing import Any, Union, cast, Callable, Iterable
|
||||
from typing import Dict, List, Tuple
|
||||
import os.path
|
||||
import itertools
|
||||
@@ -13,9 +13,11 @@ from openpyxl import Workbook
|
||||
from openpyxl.worksheet.worksheet import Worksheet
|
||||
#from openpyxl.worksheet.cell_range import MultiCellRange
|
||||
from openpyxl.worksheet.datavalidation import DataValidation
|
||||
from openpyxl.cell.cell import Cell
|
||||
#from openpyxl.utils import coordinate_to_tuple
|
||||
|
||||
from .utils import load_charts, load_sparklines, load_rows_or_cols, load_xlsx_styles
|
||||
from .utils import _match_value_to_rule
|
||||
from .utils import _match_value_to_rule, _read_cell_style, read_cell_value
|
||||
|
||||
logger = logging.getLogger("desktopenv.metric.table")
|
||||
|
||||
@@ -91,11 +93,11 @@ def compare_table(result: str, expected: str, **options) -> float:
|
||||
return 0.
|
||||
|
||||
xlworkbookr: Workbook = openpyxl.load_workbook(filename=result)
|
||||
pdworkbookr = pd.ExcelFile(xlworkbookr, engine="openpyxl")
|
||||
pdworkbookr = pd.ExcelFile(result)
|
||||
worksheetr_names: List[str] = pdworkbookr.sheet_names
|
||||
|
||||
xlworkbooke: Workbook = openpyxl.load_workbook(filename=expected)
|
||||
pdworkbooke = pd.ExcelFile(xlworkbooke, engine="openpyxl")
|
||||
pdworkbooke = pd.ExcelFile(expected)
|
||||
worksheete_names: List[str] = pdworkbooke.sheet_names
|
||||
|
||||
parse_idx: Callable[[Union[str, int], BOOK, BOOK], BOOK] =\
|
||||
@@ -165,7 +167,7 @@ def compare_table(result: str, expected: str, **options) -> float:
|
||||
# Compare Style (Also Conditional Formatiing) {{{ #
|
||||
# sheet_idx0: 0 == "RI0" == "RNSheet1" | "EI0" == "ENSheet1"
|
||||
# sheet_idx1: as sheet_idx0
|
||||
# props: list of str indicating concerned styles
|
||||
# props: list of str indicating concerned styles, see utils._read_cell_style
|
||||
|
||||
styles1: Dict[str, List[Any]] = load_xlsx_styles(*parse_idx(r["sheet_idx0"], xlworkbookr, xlworkbooke), **r)
|
||||
styles2: Dict[str, List[Any]] = load_xlsx_styles(*parse_idx(r["sheet_idx1"], xlworkbookr, xlworkbooke), **r)
|
||||
@@ -283,6 +285,31 @@ def compare_table(result: str, expected: str, **options) -> float:
|
||||
logger.debug("Assertion: %s[cols] == %s[cols] - %s", r["sheet_idx0"], r["sheet_idx1"], metric)
|
||||
# }}} Check Row Properties #
|
||||
|
||||
elif r["type"] == "check_cell":
|
||||
# Check Cell Properties {{{ #
|
||||
# sheet_idx: 0 == "RI0" == "RNSheet1" | "EI0" == "ENSheet1"
|
||||
# coordinate: str, "E3"
|
||||
# props: dict like {attribute: {"method": str, "ref": anything}}
|
||||
# supported attributes: value & those supported by utils._read_cell_style
|
||||
|
||||
sheet: Worksheet = _load_sheet(*parse_idx(r["sheet_idx"], xlworkbookr, xlworkbooke))
|
||||
#data_frame: pd.DataFrame = _load_sheet(*parse_idx(r["sheet_idx"], pdworkbookr, pdworkbooke))
|
||||
cell: Cell = sheet[r["coordinate"]]
|
||||
metric: bool = True
|
||||
for prpt, rule in r["props"].items():
|
||||
if prpt=="value":
|
||||
val = read_cell_value(*parse_idx(r["sheet_idx"], result, expected), r["coordinate"])
|
||||
else:
|
||||
val = _read_cell_style(prpt, cell)
|
||||
|
||||
metric = metric and _match_value_to_rule(val, rule)
|
||||
|
||||
logger.debug( "Assertion: %s[%s] :%s - %s"
|
||||
, r["sheet_idx"], r["coordinate"]
|
||||
, repr(r["props"]), metric
|
||||
)
|
||||
# }}} Check Cell Properties #
|
||||
|
||||
else:
|
||||
raise NotImplementedError("Unimplemented sheet check: {:}".format(r["type"]))
|
||||
|
||||
@@ -293,6 +320,24 @@ def compare_table(result: str, expected: str, **options) -> float:
|
||||
return float(passes)
|
||||
# }}} function compare_table #
|
||||
|
||||
def compare_csv(result: str, expected: str, **options) -> float:
|
||||
if result is None:
|
||||
return 0.
|
||||
|
||||
with open(result) as f:
|
||||
result_lines: Iterable[str] = f.read().splitlines()
|
||||
with open(expected) as f:
|
||||
expected_lines: Iterable[str] = f.read().splitlines()
|
||||
if not options.get("strict", True):
|
||||
result_lines = map(str.strip, result_lines)
|
||||
expected_lines = map(str.strip, expected_lines)
|
||||
if options.get("ignore_case", False):
|
||||
result_lines = map(str.lower, result_lines)
|
||||
expected_lines = map(str.lower, expected_lines)
|
||||
|
||||
metric: bool = list(result_lines)==list(expected_lines)
|
||||
return float(metric)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import datetime
|
||||
import sys
|
||||
@@ -326,16 +371,15 @@ if __name__ == '__main__':
|
||||
logger.addHandler(stdout_handler)
|
||||
logger.addHandler(sdebug_handler)
|
||||
|
||||
path1 = "../../任务数据/LibreOffice Calc/Calendar_Highlight_Weekend_Days.xlsx"
|
||||
path2 = "../../任务数据/LibreOffice Calc/Calendar_Highlight_Weekend_Days_gold.xlsx"
|
||||
rules = [ { "type": "sheet_data"
|
||||
, "sheet_idx0": 0
|
||||
, "sheet_idx1": "EI0"
|
||||
}
|
||||
, { "type": "style"
|
||||
, "sheet_idx0": 0
|
||||
, "sheet_idx1": "EI0"
|
||||
, "props": ["bgcolor"]
|
||||
path1 = "../../任务数据/LibreOffice Calc/Multiply_Time_Number.xlsx"
|
||||
path2 = "../../任务数据/LibreOffice Calc/Multiply_Time_Number_gold.xlsx"
|
||||
rules = [ { "type": "check_cell"
|
||||
, "sheet_idx": 0
|
||||
, "coordinate": "E3"
|
||||
, "props": { "value": { "method": "approx:0.001"
|
||||
, "ref": 191.6667
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
print( compare_table( path1, path2
|
||||
|
||||
Reference in New Issue
Block a user