def test_non_existing_element_returns0(self): args = [ CompositionNode(FormulaWrapper("C"), 0.7), CompositionNode(FormulaWrapper("H"), 0.043) ] fuel = Composition(*args) self.assertEqual(0, fuel["O"])
def test_init_args_as_list(self): args = [ CompositionNode(FormulaWrapper("C"), 0.7), CompositionNode(FormulaWrapper("H"), 0.043) ] fuel = Composition(*args) self.assertEqual(0.7, fuel["C"])
def test_complex_formula_in_init(self): args = [ CompositionNode(FormulaWrapper("C2H4"), 0.7), CompositionNode(FormulaWrapper("H"), 0.043) ] fuel = Composition(*args) with self.assertRaises(KeyError): c = fuel["C2H4"]
class CompositionNode: def __init__(self, *args): self.formula = FormulaWrapper("") self.proportion = 0.0 if len(args) is 1: if not isinstance(args[0], (tuple, list)): raise ValueError("Can create only from list or tuple.") self.formula = args[0][0] self.proportion = args[0][1] else: self.formula = args[0] self.proportion = args[1] if isinstance(self.formula, str): self.formula = FormulaWrapper(self.formula) elif not isinstance(self.formula, FormulaWrapper): raise ValueError("Formula is not an instance of " + FormulaWrapper.__name__ + "nor a string.") if 0 > self.proportion or self.proportion > 1: raise ValueError("Proportion should be a number in range <0,1>") def mass_percentage(self, iso_element_name): return self.formula.percentage_mass(iso_element_name) * self.proportion def __getitem__(self, iso_element_name): return self.mass_percentage(iso_element_name)
def __init__(self, c, h): if c < 1 or h < 1: raise ValueError("C and H need to be positive numbers") self.carbon = c self.hydrogen = h self.type = self.get_type(c, h) self.name = self.get_name(c, h) self.formula = FormulaWrapper(self.name)
def __init__(self, *args): self.formula = FormulaWrapper("") self.proportion = 0.0 if len(args) is 1: if not isinstance(args[0], (tuple, list)): raise ValueError("Can create only from list or tuple.") self.formula = args[0][0] self.proportion = args[0][1] else: self.formula = args[0] self.proportion = args[1] if isinstance(self.formula, str): self.formula = FormulaWrapper(self.formula) elif not isinstance(self.formula, FormulaWrapper): raise ValueError("Formula is not an instance of " + FormulaWrapper.__name__ + "nor a string.") if 0 > self.proportion or self.proportion > 1: raise ValueError("Proportion should be a number in range <0,1>")
def test_init_args(self): fuel = Composition( CompositionNode(FormulaWrapper("C"), 0.7), CompositionNode(FormulaWrapper("H"), 0.043) ) self.assertEqual(0.7, fuel["C"])
def test_get_mass_percentage(self): f = FormulaWrapper("H2O") self.assertEqual(0.11189834407236524, f.percentage_mass("H"))
def test_get_item(self): f = FormulaWrapper("HHHH") self.assertEqual(4, f["H"])
def test_amount_of_not_existing_element_returns0(self): proportion = 0.5 cn = CompositionNode(FormulaWrapper("H2O"), proportion) self.assertEqual(0, cn["C"])
def test_amount_of_h_mass_in_watter_with05_proportion(self): proportion = 0.5 cn = CompositionNode(FormulaWrapper("H2O"), proportion) self.assertEqual(proportion * (ELEMENTS["H"].mass * 2)/(ELEMENTS["H"].mass *2 + ELEMENTS["O"].mass), cn["H"])
def test_init_raises_value_error_when_proportion_is_more_than_1(self): with self.assertRaises(ValueError): CompositionNode(FormulaWrapper("H2O"), 1.1)
def test_init_raises_value_error_when_proportion_is_negative(self): with self.assertRaises(ValueError): CompositionNode(FormulaWrapper("H2O"), -1)