How to use the pycel.excelcompiler.ExcelCompiler.from_file function in pycel

To help you get started, we’ve selected a few pycel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
ws['B2'] = '2'
    ws['B3'] = '3'
    ws['B4'] = '4'
    ws['B5'] = '5'
    ws['C1'] = '=COUNTIFS(B:B,">3")'
    ws['C2'] = '=SUMIFS(A:A,B:B,">3")'
    excel_compiler = ExcelCompiler(filename='test_unbounded_countifs', excel=wb)

    output_addrs = 'Sheet!C1', 'Sheet!C2'
    assert (2, 9) == excel_compiler.evaluate(output_addrs)
    excel_compiler.recalculate()
    assert (2, 9) == excel_compiler.evaluate(output_addrs)

    # read the spreadsheet from pickle
    excel_compiler.to_file(file_types=('pickle', ))
    excel_compiler = ExcelCompiler.from_file(excel_compiler.filename)

    # test evaluation
    assert (2, 9) == excel_compiler.evaluate(output_addrs)
    excel_compiler.recalculate()
    assert (2, 9) == excel_compiler.evaluate(output_addrs)
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_round_trip_through_json_yaml_and_pickle(
        excel_compiler, fixture_xls_path):
    excel_compiler.evaluate('Sheet1!D1')
    excel_compiler.extra_data = {1: 3}
    excel_compiler.to_file(file_types=('pickle', ))
    excel_compiler.to_file(file_types=('yml', ))
    excel_compiler.to_file(file_types=('json', ))

    # read the spreadsheet from json, yaml and pickle
    excel_compiler_json = ExcelCompiler.from_file(
        excel_compiler.filename + '.json')
    excel_compiler_yaml = ExcelCompiler.from_file(
        excel_compiler.filename + '.yml')
    excel_compiler = ExcelCompiler.from_file(excel_compiler.filename)

    # test evaluation
    assert -0.02286 == round(excel_compiler_json.evaluate('Sheet1!D1'), 5)
    assert -0.02286 == round(excel_compiler_yaml.evaluate('Sheet1!D1'), 5)
    assert -0.02286 == round(excel_compiler.evaluate('Sheet1!D1'), 5)

    excel_compiler_json.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler_json.evaluate('Sheet1!D1'), 5)

    excel_compiler_yaml.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler_yaml.evaluate('Sheet1!D1'), 5)

    excel_compiler.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler.evaluate('Sheet1!D1'), 5)
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_evaluate_empty_intersection(fixture_dir):
    excel_compiler = ExcelCompiler.from_file(
        os.path.join(fixture_dir, 'fixture.xlsx.yml'))

    address = AddressCell('s!A1')
    excel_compiler.cell_map[str(address)] = _Cell(
        address, None, '=_R_(str(_REF_("s!A1:A2") & _REF_("s!B1:B2")))',
        excel_compiler.excel
    )
    assert excel_compiler.evaluate(address) == NULL_ERROR
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_compile_error_message_line_number(excel_compiler):
    input_addrs = ['trim-range!D5']
    output_addrs = ['trim-range!B2']

    excel_compiler.trim_graph(input_addrs, output_addrs)

    filename = excel_compiler.filename + '.pickle'
    excel_compiler.to_file(filename)

    excel_compiler = ExcelCompiler.from_file(filename)
    formula = excel_compiler.cell_map[output_addrs[0]].formula
    formula._python_code = '(x)'
    formula.lineno = 3000
    formula.filename = 'a_file'
    with pytest.raises(UnknownFunction, match='File "a_file", line 3000'):
        excel_compiler.evaluate(output_addrs[0])
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_unknown_functions(fixture_dir, msg, formula):
    excel_compiler = ExcelCompiler.from_file(
        os.path.join(fixture_dir, 'fixture.xlsx.yml'))

    address = AddressCell('s!A1')
    excel_compiler.cell_map[str(address)] = _Cell(
        address, None, formula, excel_compiler.excel
    )
    with pytest.raises(UnknownFunction, match=msg):
        excel_compiler.evaluate(address)

    result = excel_compiler.validate_calcs([address])
    assert 'not-implemented' in result
    assert len(result['not-implemented']) == 1
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_round_trip_through_json_yaml_and_pickle(
        excel_compiler, fixture_xls_path):
    excel_compiler.evaluate('Sheet1!D1')
    excel_compiler.extra_data = {1: 3}
    excel_compiler.to_file(file_types=('pickle', ))
    excel_compiler.to_file(file_types=('yml', ))
    excel_compiler.to_file(file_types=('json', ))

    # read the spreadsheet from json, yaml and pickle
    excel_compiler_json = ExcelCompiler.from_file(
        excel_compiler.filename + '.json')
    excel_compiler_yaml = ExcelCompiler.from_file(
        excel_compiler.filename + '.yml')
    excel_compiler = ExcelCompiler.from_file(excel_compiler.filename)

    # test evaluation
    assert -0.02286 == round(excel_compiler_json.evaluate('Sheet1!D1'), 5)
    assert -0.02286 == round(excel_compiler_yaml.evaluate('Sheet1!D1'), 5)
    assert -0.02286 == round(excel_compiler.evaluate('Sheet1!D1'), 5)

    excel_compiler_json.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler_json.evaluate('Sheet1!D1'), 5)

    excel_compiler_yaml.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler_yaml.evaluate('Sheet1!D1'), 5)

    excel_compiler.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler.evaluate('Sheet1!D1'), 5)
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_round_trip_through_json_yaml_and_pickle(
        excel_compiler, fixture_xls_path):
    excel_compiler.evaluate('Sheet1!D1')
    excel_compiler.extra_data = {1: 3}
    excel_compiler.to_file(file_types=('pickle', ))
    excel_compiler.to_file(file_types=('yml', ))
    excel_compiler.to_file(file_types=('json', ))

    # read the spreadsheet from json, yaml and pickle
    excel_compiler_json = ExcelCompiler.from_file(
        excel_compiler.filename + '.json')
    excel_compiler_yaml = ExcelCompiler.from_file(
        excel_compiler.filename + '.yml')
    excel_compiler = ExcelCompiler.from_file(excel_compiler.filename)

    # test evaluation
    assert -0.02286 == round(excel_compiler_json.evaluate('Sheet1!D1'), 5)
    assert -0.02286 == round(excel_compiler_yaml.evaluate('Sheet1!D1'), 5)
    assert -0.02286 == round(excel_compiler.evaluate('Sheet1!D1'), 5)

    excel_compiler_json.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler_json.evaluate('Sheet1!D1'), 5)

    excel_compiler_yaml.set_value('Sheet1!A1', 200)
    assert -0.00331 == round(excel_compiler_yaml.evaluate('Sheet1!D1'), 5)
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_evaluate_exceptions(fixture_dir):
    excel_compiler = ExcelCompiler.from_file(
        os.path.join(fixture_dir, 'fixture.xlsx.yml'))

    address = AddressCell('s!A1')
    excel_compiler.cell_map[str(address)] = _Cell(
        address, None, '=__REF__("s!A2")', excel_compiler.excel
    )
    address = AddressCell('s!A2')
    excel_compiler.cell_map[str(address)] = _Cell(
        address, None, '=$', excel_compiler.excel
    )

    with pytest.raises(FormulaParserError):
        excel_compiler.evaluate(address)

    result = excel_compiler.validate_calcs(address)
    assert 'exceptions' in result
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
assert b6 == b6_expect
    assert b8 == b8_expect

    circular_ws.set_value('Sheet1!B3', 0)
    b6, b8 = circular_ws.evaluate(
        ['Sheet1!B6', 'Sheet1!B8'], iterations=5000, tolerance=1e-20)
    assert (b6, b8) == (50, -50)
    circular_ws.set_value('Sheet1!B3', 100)
    b8, b6 = circular_ws.evaluate(
        ['Sheet1!B8', 'Sheet1!B6'], iterations=5000, tolerance=0.01)
    assert b6 == b6_expect
    assert b8 == b8_expect

    # round trip cycle params through text file
    circular_ws.to_file(file_types='yml')
    excel_compiler = ExcelCompiler.from_file(circular_ws.filename)
    excel_compiler.set_value('Sheet1!B3', 0)
    b6, b8 = excel_compiler.evaluate(
        ['Sheet1!B6', 'Sheet1!B8'], iterations=5000, tolerance=1e-20)
    assert (b6, b8) == (50, -50)
    excel_compiler.set_value('Sheet1!B3', 100)
    b8, b6 = excel_compiler.evaluate(
        ['Sheet1!B8', 'Sheet1!B6'], iterations=5000, tolerance=0.01)
    assert b6 == b6_expect
    assert b8 == b8_expect
github dgorissen / pycel / tests / test_excelcompiler.py View on Github external
def test_evaluate_conditional_formatting(cond_format_ws):
    cells_addrs = [
        AddressCell('B2'),
        AddressCell('Sheet1!B3'),
        AddressRange('Sheet1!B4:B6'),
    ]
    formats = cond_format_ws.eval_conditional_formats(cells_addrs)
    formats2 = cond_format_ws.eval_conditional_formats((a for a in cells_addrs))
    assert formats == list(formats2)
    assert len(formats) == 3
    assert len(formats[2]) == 3

    # read the spreadsheet from yaml
    cond_format_ws.to_file(file_types=('yml', ))
    cond_format_ws_yaml = ExcelCompiler.from_file(
        cond_format_ws.filename + '.yml')
    cells_addrs[0] = AddressCell('Sheet1!B2')
    formats3 = cond_format_ws_yaml.eval_conditional_formats(tuple(cells_addrs))
    assert formats2 == formats3

    # read the spreadsheet from pickle
    cond_format_ws.to_file(file_types=('pkl', ))
    cond_format_ws_pkl = ExcelCompiler.from_file(
        cond_format_ws.filename + '.pkl')
    cells_addrs[0] = AddressCell('Sheet1!B2')
    formats4 = cond_format_ws_pkl.eval_conditional_formats(tuple(cells_addrs))
    assert formats2 == formats4

    formats.append(formats[2][0][0])
    formats.append(formats[2][1][0])
    formats.append(formats[2][2][0])