def test_complex_cast_fail(): cc = CalculatorComplex.from_pair("a", "b") with pytest.raises(ValueError): assert complex(cc)
def test_from_pair(start_value): cf = CalculatorComplex.from_pair(*start_value) assert cf.real == CalculatorFloat(start_value[0]) assert cf.imag == CalculatorFloat(start_value[1])
def test_complex_abs(initial): aabs = abs(CalculatorComplex.from_pair(*initial[0])) assert aabs.isclose(initial[1])
def test_complex_arg(initial): arg = CalculatorComplex.from_pair(*initial[0]).arg() assert arg.isclose(initial[1])
def test_complex_conj(initial): t = CalculatorComplex.from_pair(*initial[0]).conj() assert t == CalculatorComplex.from_pair(*initial[1])
def test_complex_isclose(initial): t = CalculatorComplex.from_pair(initial[0][0], initial[0][1]).isclose( CalculatorComplex.from_pair(initial[1][0], initial[1][1])) assert t == initial[2]
def from_dict(cls, config_dictionary: dict) -> 'Qonfig[T]': r"""Load a (partial) Qonfig from a dictionary. Args: config_dictionary: Dictionary version of the config Returns: Qonfig[T] Raises: ImportError: Could not find class. Try importing corresponding module """ # Process qonfig_name to import class of Qonfig and raise error # when import fails spl = config_dictionary['qonfig_name'].rsplit('.', 1) if len(spl) == 1: class_type = globals().get(spl[0], None) if class_type is None: error_msg = ( 'Could not find class {}. Try importing corresponding module' .format(spl[0])) raise ImportError(error_msg) elif len(spl) == 2: temporary_import = importlib.import_module(spl[0]) class_type = getattr(temporary_import, spl[1], None) if class_type is None: error_msg = 'Could not import {}.'.format(spl) raise ImportError(error_msg) # Create new Qonfig for class return_config = cls(class_type) # Setting items in dict as items in Qonfig for key in [ key for key in return_config.keys() if key in config_dictionary.keys() ]: # Code path when the value of config_dictionary[key] defines a Qonfig if (isinstance(config_dictionary[key], dict) and 'qonfig_name' in config_dictionary[key].keys()): try: return_config[key] = Qonfig.from_dict( config_dictionary[key]) except NotQonfigurableError: return_config[key] = config_dictionary[key] elif (isinstance(config_dictionary[key], dict) and config_dictionary[key].get('is_calculator_complex', False)): return_config[key] = CalculatorComplex.from_pair( config_dictionary[key]['real'], config_dictionary[key]['imag']) # Code path for list recursion if item is a list containing dicts # defining a Qonfig, create the Qonfigs in the list elif (isinstance(config_dictionary[key], list) and any([(isinstance(d, dict) and ('qonfig_name' in d.keys())) for d in config_dictionary[key]])): config_list: List[Any] = list() for d in config_dictionary[key]: if isinstance(d, dict) and ('qonfig_name' in d.keys()): try: config_list.append(Qonfig.from_dict(d)) except NotQonfigurableError: config_list.append(d) elif isinstance(d, dict) and d.get('is_calculator_complex', False): config_list.append( CalculatorComplex.from_pair(d['real'], d['imag'])) else: config_list.append(d) return_config[key] = config_list elif config_dictionary[key] == "<empty 'Empty'>": return_config[key] = empty else: return_config[key] = config_dictionary[key] return return_config