Пример #1
0
 def test_formula_parse_with_wide_count(self):
     f = Formula.parse('C6H10O2')
     self.assertEqual(f, Formula({
         Atom('C'): 6,
         Atom('H'): 10,
         Atom('O'): 2
     }))
Пример #2
0
 def test_formula_parse_with_implicit_digit(self):
     f = Formula.parse('C2H5NO2')
     self.assertEqual(
         f, Formula({
             Atom('C'): 2,
             Atom('H'): 5,
             Atom('N'): 1,
             Atom('O'): 2
         }))
Пример #3
0
 def test_formula_balance_subgroups_cancel_out(self):
     f1, f2 = Formula.balance(Formula.parse('H2(CH2)n'),
                              Formula.parse('CH3O(CH2)n'))
     self.assertEqual(f1, Formula({
         Atom('C'): 1,
         Atom('H'): 1,
         Atom('O'): 1
     }))
     self.assertEqual(f2, Formula())
Пример #4
0
 def test_formula_parse_with_numbered_radical(self):
     f = Formula.parse('C2H4NO2(R1)')
     self.assertEqual(
         f,
         Formula({
             Atom('C'): 2,
             Atom('H'): 4,
             Atom('N'): 1,
             Atom('O'): 2,
             Radical('R1'): 1
         }))
Пример #5
0
 def test_formula_parse_with_implicitly_counted_subgroup(self):
     f = Formula.parse('C2H6O2(CH)')
     self.assertEqual(
         f,
         Formula({
             Atom('C'): 2,
             Atom('H'): 6,
             Atom('O'): 2,
             Formula({
                 Atom('C'): 1,
                 Atom('H'): 1
             }): 1
         }))
Пример #6
0
 def test_formula_parse_with_two_identical_counted_subgroups(self):
     f = Formula.parse('C2H6O2(CH)2(CH)2')
     self.assertEqual(
         f,
         Formula({
             Atom('C'): 2,
             Atom('H'): 6,
             Atom('O'): 2,
             Formula({
                 Atom('C'): 1,
                 Atom('H'): 1
             }): 4
         }))
Пример #7
0
 def test_formula_parse_with_wide_counted_subgroup(self):
     f = Formula.parse('C2(CH)10NO2')
     self.assertEqual(
         f,
         Formula({
             Atom('C'): 2,
             Atom('N'): 1,
             Atom('O'): 2,
             Formula({
                 Atom('C'): 1,
                 Atom('H'): 1
             }): 10
         }))
Пример #8
0
 def calc_formula(f, charge, neutral=False):
     formula = {}
     for e, value in iteritems(f):
         if e != Atom('H') or charge is None or not neutral:
             formula[e] = value
         else:
             formula[e] = value - charge  # No. of H in neutral stat
     return formula
Пример #9
0
 def test_formula_items(self):
     f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6})
     self.assertEqual(dict(f.items()), {
         Atom('C'): 6,
         Atom('H'): 12,
         Atom('O'): 6
     })
Пример #10
0
 def test_formula_merge_same_formulas_with_same_atoms(self):
     f = Formula({
         Atom('H'): 2,
         Atom('O'): 1
     }) | Formula({
         Atom('N'): 1,
         Atom('O'): 2
     })
     self.assertEqual(f, Formula({
         Atom('H'): 2,
         Atom('N'): 1,
         Atom('O'): 3
     }))
Пример #11
0
 def test_formula_to_string_with_group(self):
     f = Formula({
         Atom('C'):
         1,
         Atom('H'):
         3,
         Formula({
             Atom('C'): 1,
             Atom('H'): 2
         }):
         14,
         Formula({
             Atom('C'): 1,
             Atom('O'): 1,
             Formula({
                 Atom('H'): 1,
                 Atom('O'): 1
             }): 1
         }):
         1
     })
Пример #12
0
 def test_atom_equals(self):
     a1 = Atom('H')
     a2 = Atom('Zn')
     self.assertEqual(a1, a1)
     self.assertNotEqual(a1, a2)
Пример #13
0
 def test_formula_not_equals_other_with_different_number(self):
     f = Formula({Atom('Au'): 1})
     self.assertNotEqual(f, Formula({Atom('Au'): 2}))
Пример #14
0
 def test_atom_to_string(self):
     a = Atom('C')
     self.assertEqual(str(a), 'C')
Пример #15
0
 def test_atom_symbol_wide(self):
     a = Atom('Zn')
     self.assertEqual(a.symbol, 'Zn')
Пример #16
0
 def test_formula_parse_with_implicit_final_digit(self):
     f = Formula.parse('H2O')
     self.assertEqual(f, Formula({Atom('H'): 2, Atom('O'): 1}))
Пример #17
0
 def test_formula_contains(self):
     f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6})
     self.assertIn(Atom('C'), f)
     self.assertNotIn(Atom('Ag'), f)
Пример #18
0
 def test_formula_balance_missing_on_both_sides(self):
     f1, f2 = Formula.balance(Formula.parse('C3H6OH'),
                              Formula.parse('CH6O2'))
     self.assertEqual(f1, Formula({Atom('O'): 1}))
     self.assertEqual(f2, Formula({Atom('C'): 2, Atom('H'): 1}))
Пример #19
0
 def test_formula_balance_missing_on_one_side(self):
     f1, f2 = Formula.balance(Formula.parse('H2O'), Formula.parse('OH'))
     self.assertEqual(f1, Formula())
     self.assertEqual(f2, Formula({Atom('H'): 1}))
Пример #20
0
 def test_formula_flattened(self):
     f = Formula({
         Atom('C'):
         1,
         Atom('H'):
         3,
         Formula({
             Atom('C'): 1,
             Atom('H'): 2
         }):
         14,
         Formula({
             Atom('C'): 1,
             Atom('O'): 1,
             Formula({
                 Atom('H'): 1,
                 Atom('O'): 1
             }): 1
         }):
         1
     })
     self.assertEqual(f.flattened(),
                      Formula({
                          Atom('C'): 16,
                          Atom('H'): 32,
                          Atom('O'): 2
                      }))
Пример #21
0
 def test_formula_merge_formulas_that_cancel_out(self):
     f = Formula({Atom('H'): 3}) | Formula({Atom('H'): -3})
     self.assertEqual(f, Formula())
Пример #22
0
 def test_formula_parse_with_wide_element(self):
     f = Formula.parse('ZnO')
     self.assertEqual(f, Formula({Atom('Zn'): 1, Atom('O'): 1}))
Пример #23
0
 def test_atom_symbol(self):
     a = Atom('H')
     self.assertEqual(a.symbol, 'H')
Пример #24
0
 def test_atom_equals(self):
     a1 = Atom('H')
     self.assertEqual(a1, Atom.H)
     self.assertNotEqual(a1, Atom.Zn)
Пример #25
0
 def test_atom_symbol_non_standard(self):
     a = Atom('X')
     self.assertEqual(a.symbol, 'X')
Пример #26
0
 def test_formula_not_equals_other_with_distinct_elements(self):
     f = Formula({Atom('Au'): 1})
     self.assertNotEqual(f, Formula({Atom('Ag'): 1}))
Пример #27
0
 def test_atom_repr(self):
     a = Atom('Si')
     self.assertEqual(repr(a), "Atom('Si')")
Пример #28
0
 def test_formula_to_string(self):
     f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6})
     self.assertEqual(str(f), 'C6H12O6')
Пример #29
0
 def test_atom_ordered(self):
     a1 = Atom('H')
     a2 = Atom('C')
     a3 = Atom('Zn')
     self.assertGreater(a1, a2)
     self.assertLess(a1, a3)
Пример #30
0
 def test_formula_equals_other_formula(self):
     f = Formula({Atom('H'): 2, Atom('O'): 1})
     self.assertEqual(f, Formula({Atom('O'): 1, Atom('H'): 2}))