def validate(self, input, pos): state, input, pos = super().validate(input, pos) if state == QtGui.QValidator.Invalid: return state, input, pos try: from_formula(input) except: return QtGui.QValidator.Intermediate, input, pos return QtGui.QValidator.Acceptable, input, pos
def from_formula(cls, formula, density_kg_per_m3=None, color=None): """ Returns the material for the specified formula. :arg formula: formula of a molecule (e.g. ``Al2O3``) :type formula: :class:`str` """ composition = from_formula(formula) return cls(formula, composition, density_kg_per_m3, color=color)
def testfrom_formula(self): weightFractionAl = 0.21358626371988801 weightFractionNa = 0.27298103136883051 weightFractionB = 0.51343270491128157 comp = from_formula('Al2Na3B12') self.assertAlmostEqual(weightFractionAl, comp[13], 4) self.assertAlmostEqual(weightFractionNa, comp[11], 4) self.assertAlmostEqual(weightFractionB, comp[5], 4) comp = from_formula('Al 2 Na 3 B 12') self.assertAlmostEqual(weightFractionAl, comp[13], 4) self.assertAlmostEqual(weightFractionNa, comp[11], 4) self.assertAlmostEqual(weightFractionB, comp[5], 4) comp = from_formula('Al2 Na3 B12') self.assertAlmostEqual(weightFractionAl, comp[13], 4) self.assertAlmostEqual(weightFractionNa, comp[11], 4) self.assertAlmostEqual(weightFractionB, comp[5], 4) self.assertRaises(Exception, from_formula, 'Aq2 Na3 B12') comp = from_formula('Al2') self.assertAlmostEqual(1.0, comp[13], 4)
def test_composition_from_formula_normalize(): comp = from_formula("Al2") assert comp[13] == pytest.approx(1.0, abs=1e-4)
def test_composition_from_formula_invalid_atomicnumber(): with pytest.raises(Exception): from_formula("Aq2 Na3 B12")
def test_composition_from_formula(formula): comp = from_formula(formula) assert comp[13] == pytest.approx(0.21358626371988801, abs=1e-4) assert comp[11] == pytest.approx(0.27298103136883051, abs=1e-4) assert comp[5] == pytest.approx(0.51343270491128157, abs=1e-4)
def _on_formula_changed(self): try: composition = from_formula(self.field_formula.formula()) except: composition = {} self.field_density.setComposition(composition)