diff --git a/TCutility/molecule.py b/TCutility/molecule.py index d3312cc5..73a6f4ca 100644 --- a/TCutility/molecule.py +++ b/TCutility/molecule.py @@ -13,6 +13,9 @@ def parse_str(s: str): if not isinstance(s, str): return s + if ',' in s: + return [parse_str(part.strip()) for part in s.split(',')] + # to parse the string we use try/except method try: return int(s) @@ -86,7 +89,7 @@ def load(path) -> plams.Molecule: ''' def parse_flags(args): ret = result.Result() - ret.tags = [] + ret.tags = set() for arg in args: # flags are given as key=value pairs # tags are given as loose keys @@ -94,7 +97,7 @@ def parse_flags(args): key, value = arg.split('=') ret[key.strip()] = parse_str(value.strip()) else: - ret.tags.append(parse_str(arg.strip())) + ret.tags.add(parse_str(arg.strip())) return ret with open(path) as f: diff --git a/TCutility/results/result.py b/TCutility/results/result.py index 83b81167..a7d5bc81 100644 --- a/TCutility/results/result.py +++ b/TCutility/results/result.py @@ -53,6 +53,9 @@ def __hash__(self): ''' raise KeyError(f'Tried to hash {self.get_parent_tree()}, but it is empty') + def __reduce__(self): + return 'TCutility.results.result.Result' + def __bool__(self): '''Make sure that keys starting and ending in "__" are skipped''' return len([key for key in self.keys() if not (key.startswith('__') and key.endswith('__'))]) > 0 diff --git a/test/fixtures/xyz/pyr.xyz b/test/fixtures/xyz/pyr.xyz new file mode 100644 index 00000000..89793fe9 --- /dev/null +++ b/test/fixtures/xyz/pyr.xyz @@ -0,0 +1,18 @@ +12 + +C 0.00000000 1.14206764 0.90887116 +C 0.00000000 1.19134753 -0.47779047 +C 0.00000000 0.00000000 -1.18863009 +C 0.00000000 -1.19134753 -0.47779047 +C 0.00000000 -1.14206764 0.90887116 +N 0.00000000 0.00000000 1.58935329 +H 0.00000000 2.05493274 1.49740370 +H 0.00000000 2.14479192 -0.99199860 +H 0.00000000 0.00000000 -2.27369478 +H 0.00000000 -2.14479192 -0.99199860 +H 0.00000000 -2.05493274 1.49740370 +Xx 0.12739120 -0.00077609 2.50049043 + +conn = 6, 12 +test_list = 3.14, pi +active_atoms = 8 \ No newline at end of file diff --git a/test/test_molecules.py b/test/test_molecules.py index 98d7c9f8..c8c3e6d2 100644 --- a/test/test_molecules.py +++ b/test/test_molecules.py @@ -6,39 +6,63 @@ def test_atom_flags() -> None: - xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') mol = molecule.load(xyzfile) atom = mol[2] assert atom.flags.origin == 'substrate' def test_atom_tags() -> None: - xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') mol = molecule.load(xyzfile) atom = mol[3] assert 'R1' in atom.flags.tags def test_mol_reading() -> None: - xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') tcmol = molecule.load(xyzfile) plamsmol = plams.Molecule(xyzfile) assert all(tcatom.symbol == plamsatom.symbol and tcatom.coords == plamsatom.coords for tcatom, plamsatom in zip(tcmol.atoms, plamsmol.atoms)) def test_mol_flags() -> None: - xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') mol = molecule.load(xyzfile) assert mol.flags.task == 'TransitionStateSearch' def test_mol_tags() -> None: - xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') mol = molecule.load(xyzfile) assert 'vibrations' in mol.flags.tags def test_comment() -> None: - xyzfile = j('test', 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'transitionstate_radical_addition.xyz') mol = molecule.load(xyzfile) assert mol.comment == 'molecule used for testing the molecule module' + + +def test_list_value() -> None: + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'pyr.xyz') + mol = molecule.load(xyzfile) + assert mol.flags.conn == [6, 12] + + +def test_list_value2() -> None: + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'pyr.xyz') + mol = molecule.load(xyzfile) + assert mol.flags.test_list == [3.14, 'pi'] + + +def test_mol_copy() -> None: + xyzfile = j(os.path.split(__file__)[0], 'fixtures', 'xyz', 'pyr.xyz') + mol = molecule.load(xyzfile) + mol.copy() + + +if __name__ == '__main__': + import pytest + + pytest.main()