def test_excel_model_cycles(self): xl_model = ExcelModel().loads(self.filename_circular).finish(circular=1) xl_model.calculate() books = xl_model.books books = {k: _book2dict(v[BOOK]) for k, v in xl_model.write(books).items()} self._compare(books, self.results_circular)
def test_excel_model_compile(self): xl_model = ExcelModel() xl_model.loads(self.filename_compile) xl_model.finish() inputs = ["A%d" % i for i in range(2, 5)] outputs = ["C%d" % i for i in range(2, 5)] func = xl_model.compile( ["'[EXCEL.XLSX]DATA'!%s" % i for i in inputs], ["'[EXCEL.XLSX]DATA'!%s" % i for i in outputs] ) i = sh.selector(inputs, self.results_compile, output_type='list') res = sh.selector(outputs, self.results_compile, output_type='list') self.assertEqual([x.value[0, 0] for x in func(*i)], res)
def test_excel_model_compile(self): xl_model = ExcelModel().loads(self.filename_compile).finish() inputs = ["A%d" % i for i in range(2, 5)] outputs = ["C%d" % i for i in range(2, 5)] func = xl_model.compile(["'[excel.xlsx]DATA'!%s" % i for i in inputs], ["'[excel.xlsx]DATA'!%s" % i for i in outputs]) i = sh.selector(inputs, self.results_compile, output_type='list') res = sh.selector(outputs, self.results_compile, output_type='list') self.assertEqual([x.value[0, 0] for x in func(*i)], res) self.assertIsNot(xl_model, copy.deepcopy(xl_model)) self.assertIsNot(func, copy.deepcopy(func)) xl_model = ExcelModel().loads( self.filename_circular).finish(circular=1) func = xl_model.compile(["'[circular.xlsx]DATA'!A10"], ["'[circular.xlsx]DATA'!E10"]) self.assertEqual(func(False).value[0, 0], 2.0) self.assertIs(func(True).value[0, 0], ERR_CIRCULAR) self.assertIsNot(xl_model, copy.deepcopy(xl_model)) self.assertIsNot(func, copy.deepcopy(func))
def test_excel_model(self): start = time.time() _msg = '[info] test_excel_model: ' xl_mdl = ExcelModel() print('\n%sLoading excel-model.' % _msg) s = time.time() xl_mdl.loads(self.filename) xl_mdl.add_book(self.link_filename) msg = '%sLoaded excel-model in %.2fs.\n%sFinishing excel-model.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() xl_mdl.finish() print('%sFinished excel-model in %.2fs.' % (_msg, time.time() - s)) n_test = 0 for i in range(3): print('%sCalculate excel-model.' % _msg) s = time.time() xl_mdl.calculate() msg = '%sCalculated excel-model in %.2fs.\n%s' \ 'Comparing overwritten results.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() books = _res2books(xl_mdl.write(xl_mdl.books)) n_test += self._compare(books, self.results) msg = '%sCompared overwritten results in %.2fs.\n' \ '%sComparing fresh written results.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() n_test += self._compare(_res2books(xl_mdl.write()), self.results) msg = '%sCompared fresh written results in %.2fs.' print(msg % (_msg, time.time() - s)) if i == 0: print('%sSaving excel-model dill.' % _msg) s = time.time() xl_copy = dill.dumps(xl_mdl) msg = '%sSaved excel-model dill in %.2fs.\n' \ '%sLoading excel-model dill.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() xl_mdl = dill.loads(xl_copy) del xl_copy msg = '%sLoaded excel-model dill in %.2fs.' print(msg % (_msg, time.time() - s)) elif i == 1: print('%sDeep-copying excel-model.' % _msg) s = time.time() xl_mdl = copy.deepcopy(xl_mdl) msg = '%sDeep-copied excel-model in %.2fs.' print(msg % (_msg, time.time() - s)) print('%sSaving excel-model xlsx.' % _msg) s = time.time() dirpath = osp.join(mydir, 'tmp') xl_mdl.write(dirpath=dirpath) msg = '%sSaved excel-model exls in %.2fs.\n%sComparing saved results.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() n_test += self._compare( _file2books(*(osp.join(dirpath, fp) for fp in xl_mdl.books)), self.results) msg = '%sCompared saved results in %.2fs.\n%sRan %d tests in %.2fs' print(msg % (_msg, time.time() - s, _msg, n_test, time.time() - start))
def test_excel_model(self): start = time.time() _msg = '[info] test_excel_model: ' xl_mdl = ExcelModel() print('\n%sLoading excel-model.' % _msg) s = time.time() xl_mdl.loads(self.filename) xl_mdl.add_book(self.link_filename) msg = '%sLoaded excel-model in %.2fs.\n%sFinishing excel-model.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() xl_mdl.finish() print('%sFinished excel-model in %.2fs.' % (_msg, time.time() - s)) n_test, calculate = 0, True for i in range(4): if calculate: print('%sCalculate excel-model.' % _msg) s = time.time() xl_mdl.calculate({"'[EXTRA.XLSX]EXTRA'!A1:B1": [[1, 1]]}) msg = '%sCalculated excel-model in %.2fs.\n%s' \ 'Comparing overwritten results.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() books = _res2books(xl_mdl.write(xl_mdl.books)) n_test += self._compare(books, self.results) msg = '%sCompared overwritten results in %.2fs.\n' \ '%sComparing fresh written results.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() n_test += self._compare(_res2books(xl_mdl.write()), self.results) msg = '%sCompared fresh written results in %.2fs.' print(msg % (_msg, time.time() - s)) calculate = False if i == 2 and platform.python_version() >= '3.8': print('%sSaving excel-model dill.' % _msg) s = time.time() xl_copy = dill.dumps(xl_mdl) msg = '%sSaved excel-model dill in %.2fs.\n' \ '%sLoading excel-model dill.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() xl_mdl = dill.loads(xl_copy) del xl_copy msg = '%sLoaded excel-model dill in %.2fs.' print(msg % (_msg, time.time() - s)) calculate = True elif i == 1: print('%sDeep-copying excel-model.' % _msg) s = time.time() xl_mdl = copy.deepcopy(xl_mdl) msg = '%sDeep-copied excel-model in %.2fs.' print(msg % (_msg, time.time() - s)) calculate = True elif i == 0: print('%sSaving JSON excel-model.' % _msg) s = time.time() xl_json = json.dumps(xl_mdl.to_dict()) msg = '%sSaved JSON excel-model in %.2fs.\n' \ '%sLoading JSON excel-model.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() xl_mdl = ExcelModel().from_dict(json.loads(xl_json)) del xl_json msg = '%sLoaded JSON excel-model in %.2fs.' print(msg % (_msg, time.time() - s)) calculate = True print('%sSaving excel-model xlsx.' % _msg) s = time.time() dirpath = osp.join(mydir, 'tmp') xl_mdl.write(dirpath=dirpath) msg = '%sSaved excel-model xlsx in %.2fs.\n%sComparing saved results.' print(msg % (_msg, time.time() - s, _msg)) s = time.time() n_test += self._compare( _file2books(*(osp.join(dirpath, fp) for fp in xl_mdl.books)), self.results) msg = '%sCompared saved results in %.2fs.\n%sRan %d tests in %.2fs' print(msg % (_msg, time.time() - s, _msg, n_test, time.time() - start))
def test_excel_model(self): xl_model = ExcelModel() xl_model.loads(self.filename) xl_model.add_book(self.link_filename) xl_model.finish() xl_model.calculate() books = xl_model.books books = { k: _book2dict(v[BOOK]) for k, v in xl_model.write(books).items() } self._compare(books, self.results) books = {k: _book2dict(v[BOOK]) for k, v in xl_model.write().items()} res = {} for k, v in sh.stack_nested_keys(self.results, depth=2): sh.get_nested_dicts(res, *map(str.upper, k), default=lambda: v) self._compare(books, res)