def test_load_functions(): modules = ( importlib.import_module('pycel.excellib'), importlib.import_module('pycel.lib.date_time'), importlib.import_module('pycel.lib.logical'), importlib.import_module('math'), ) namespace = locals() names = 'degrees if_ junk'.split() missing = load_functions(names, namespace, modules) assert missing == {'junk'} assert 'degrees' in namespace assert 'if_' in namespace names = 'radians if_ junk'.split() missing = load_functions(names, namespace, modules) assert missing == {'junk'} assert 'radians' in namespace assert namespace['radians'](180) == math.pi assert namespace['radians'](((180, 360), )) == ((math.pi, 2 * math.pi), ) assert namespace['if_'](0, 'Y', 'N') == 'N' assert namespace['if_'](((0, 1), ), 'Y', 'N') == (('N', 'Y'), ) missing = load_functions(['log'], namespace, modules) assert not missing assert namespace['log'](DIV0) == DIV0
def test_load_functions(): modules = ( importlib.import_module('pycel.excellib'), importlib.import_module('pycel.lib.logical'), importlib.import_module('math'), ) namespace = locals() names = 'degrees x_if junk'.split() missing = load_functions(names, namespace, modules) assert missing == {'junk'} assert 'degrees' in namespace assert 'x_if' in namespace names = 'radians x_if junk'.split() missing = load_functions(names, namespace, modules) assert missing == {'junk'} assert 'radians' in namespace assert namespace['radians'](180) == math.pi assert namespace['radians'](((180, 360),)) == ((math.pi, 2 * math.pi),) assert namespace['x_if'](0, 'Y', 'N') == 'N' assert namespace['x_if'](((0, 1),), 'Y', 'N') == (('N', 'Y'),) missing = load_functions(['log'], namespace, modules) assert not missing assert namespace['log'](DIV0) == DIV0
def load_function(excel_formula, name_space): """exec the code into our address space""" # the compiled expressions can call these functions if # referencing other cells or a range of cells name_space['_C_'] = evaluate name_space['_R_'] = evaluate_range name_space['_REF_'] = AddressRange.create name_space['pi'] = math.pi # function to fixup the operands name_space['excel_operator_operand_fixup'] = \ build_operator_operand_fixup(capture_error_state) # hook for the execed code to save the resulting lambda name_space['lambdas'] = lambdas = [] # get the compiled code and needed names compiled, names = excel_formula.compiled_python # load the needed names not_found = load_functions(names, name_space, modules) # exec the code to define the lambda exec(compiled, name_space, name_space) excel_formula.compiled_lambda = lambdas[0] del name_space['lambdas'] return not_found