merged from zdy
This commit is contained in:
@@ -33,16 +33,25 @@ def _parse_sheet_idx(sheet_idx: Union[int, str]
|
||||
) -> Tuple[BOOK, str]:
|
||||
# function _parse_sheet_idx {{{ #
|
||||
if isinstance(sheet_idx, int):
|
||||
index: str = result_sheet_names[sheet_idx]
|
||||
try:
|
||||
index: str = result_sheet_names[sheet_idx]
|
||||
except:
|
||||
index = ""
|
||||
book: BOOK = result
|
||||
elif sheet_idx.startswith("RI"):
|
||||
index: str = result_sheet_names[int(sheet_idx[2:])]
|
||||
try:
|
||||
index: str = result_sheet_names[int(sheet_idx[2:])]
|
||||
except:
|
||||
index = ""
|
||||
book: BOOK = result
|
||||
elif sheet_idx.startswith("RN"):
|
||||
index: str = sheet_idx[2:]
|
||||
book: BOOK = result
|
||||
elif sheet_idx.startswith("EI"):
|
||||
index: str = expected_sheet_names[int(sheet_idx[2:])]
|
||||
try:
|
||||
index: str = expected_sheet_names[int(sheet_idx[2:])]
|
||||
except:
|
||||
index = ""
|
||||
book: BOOK = expected
|
||||
elif sheet_idx.startswith("EN"):
|
||||
index: str = sheet_idx[2:]
|
||||
@@ -59,24 +68,29 @@ SHEET = Union[pd.DataFrame, Worksheet, List[str]]
|
||||
|
||||
def _load_sheet(book: BOOK, index: str) -> SHEET:
|
||||
# function _load_sheet {{{ #
|
||||
if isinstance(book, str):
|
||||
book: str = cast(str, book)
|
||||
csv_name: str = "{:}-{:}.csv".format(os.path.splitext(book)[0], index)
|
||||
try:
|
||||
if isinstance(book, str):
|
||||
book: str = cast(str, book)
|
||||
csv_name: str = "{:}-{:}.csv".format(os.path.splitext(book)[0], index)
|
||||
|
||||
with open(csv_name) as f:
|
||||
csv_lines: List[str] = list(itertools.dropwhile(lambda l: len(l) == 0
|
||||
, map(lambda l: l.strip()
|
||||
, reversed(f.read().splitlines())
|
||||
)
|
||||
)
|
||||
)
|
||||
return csv_lines
|
||||
if isinstance(book, pd.ExcelFile):
|
||||
return pd.read_excel(book, index)
|
||||
if isinstance(book, Workbook):
|
||||
return book[index]
|
||||
logger.error("Not supported workbook format")
|
||||
raise NotImplementedError("Not supported workbook format")
|
||||
with open(csv_name) as f:
|
||||
csv_lines: List[str] = list(itertools.dropwhile(lambda l: len(l) == 0
|
||||
, map(lambda l: l.strip()
|
||||
, reversed(f.read().splitlines())
|
||||
)
|
||||
)
|
||||
)
|
||||
return csv_lines
|
||||
if isinstance(book, pd.ExcelFile):
|
||||
return pd.read_excel(book, index)
|
||||
if isinstance(book, Workbook):
|
||||
return book[index]
|
||||
logger.error("Not supported workbook format")
|
||||
raise NotImplementedError("Not supported workbook format")
|
||||
except NotImplementedError as e:
|
||||
raise e
|
||||
except:
|
||||
return None
|
||||
# }}} function _load_sheet #
|
||||
|
||||
|
||||
@@ -140,6 +154,8 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
|
||||
error_limit: int = r.get("precision", 4)
|
||||
sheet1: pd.DataFrame = _load_sheet(*parse_idx(r["sheet_idx0"], pdworkbookr, pdworkbooke)).round(error_limit)
|
||||
if sheet1 is None:
|
||||
return 0.
|
||||
sheet2: pd.DataFrame = _load_sheet(*parse_idx(r["sheet_idx1"], pdworkbookr, pdworkbooke)).round(error_limit)
|
||||
metric: bool = sheet1.equals(sheet2)
|
||||
logger.debug("Sheet1: \n%s", str(sheet1))
|
||||
@@ -158,6 +174,8 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
# ignore_case: optional, defaults to False
|
||||
|
||||
sheet1: List[str] = _load_sheet(*parse_idx(r["sheet_idx0"], result, expected))
|
||||
if sheet1 is None:
|
||||
return 0.
|
||||
sheet2: List[str] = _load_sheet(*parse_idx(r["sheet_idx1"], result, expected))
|
||||
if r.get("ignore_case", False):
|
||||
sheet1 = [l.lower() for l in sheet1]
|
||||
@@ -195,11 +213,11 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
# sheet_idx1: as sheet_idx0
|
||||
# props: list of str indicating concerned styles, see utils._read_cell_style
|
||||
|
||||
sheet_idx1: Tuple[Book, str] = parse_idx(r["sheet_idx0"], xlworkbookr, xlworkbooke)
|
||||
sheet_idx1: Tuple[BOOK, str] = parse_idx(r["sheet_idx0"], xlworkbookr, xlworkbooke)
|
||||
book_name1: str = parse_idx(r["sheet_idx0"], result, expected)[0]
|
||||
styles1: Dict[str, List[Any]] = load_xlsx_styles(*sheet_idx1, book_name1, **r)
|
||||
|
||||
sheet_idx2: Tuple[Book, str] = parse_idx(r["sheet_idx1"], xlworkbookr, xlworkbooke)
|
||||
sheet_idx2: Tuple[BOOK, str] = parse_idx(r["sheet_idx1"], xlworkbookr, xlworkbooke)
|
||||
book_name2: str = parse_idx(r["sheet_idx1"], result, expected)[0]
|
||||
styles2: Dict[str, List[Any]] = load_xlsx_styles(*sheet_idx2, book_name2, **r)
|
||||
# number_formats1: List[str] = [c.number_format.lower() for col in sheet1.iter_cols() for c in col if c.value is not None and c.data_type=="n"]
|
||||
@@ -214,6 +232,8 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
# sheet_idx1: as sheet_idx0
|
||||
|
||||
sheet1: Worksheet = _load_sheet(*parse_idx(r["sheet_idx0"], xlworkbookr, xlworkbooke))
|
||||
if sheet1 is None:
|
||||
return 0.
|
||||
sheet2: Worksheet = _load_sheet(*parse_idx(r["sheet_idx1"], xlworkbookr, xlworkbooke))
|
||||
metric: bool = sheet1.freeze_panes == sheet2.freeze_panes
|
||||
logger.debug("Assertion: %s.freeze(%s) == %s.freeze(%s) - %s"
|
||||
@@ -230,6 +250,8 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
# ref: value
|
||||
|
||||
sheet: Worksheet = _load_sheet(*parse_idx(r["sheet_idx"], xlworkbookr, xlworkbooke))
|
||||
if sheet is None:
|
||||
return 0.
|
||||
zoom_scale: Number = sheet.sheet_view.zoomScale or 100.
|
||||
metric: bool = _match_value_to_rule(zoom_scale, r)
|
||||
logger.debug("Assertion: %s.zoom(%.1f) %s %.1f - %s", r["sheet_idx"], zoom_scale, r["method"], r["ref"],
|
||||
@@ -258,6 +280,8 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
# * imeMode
|
||||
|
||||
sheet: Worksheet = _load_sheet(*parse_idx(r["sheet_idx"], xlworkbookr, xlworkbooke))
|
||||
if sheet is None:
|
||||
return 0.
|
||||
data_validators: List[DataValidation] = sheet.data_validations.dataValidation
|
||||
|
||||
total_metric = len(data_validators) >= len(r["dv_props"])
|
||||
@@ -348,6 +372,8 @@ def compare_table(result: str, expected: str = None, **options) -> float:
|
||||
# supported attributes: value & those supported by utils._read_cell_style
|
||||
|
||||
sheet: Worksheet = _load_sheet(*parse_idx(r["sheet_idx"], xlworkbookr, xlworkbooke))
|
||||
if sheet is None:
|
||||
return 0.
|
||||
# data_frame: pd.DataFrame = _load_sheet(*parse_idx(r["sheet_idx"], pdworkbookr, pdworkbooke))
|
||||
cell: Cell = sheet[r["coordinate"]]
|
||||
metric: bool = True
|
||||
|
||||
Reference in New Issue
Block a user