openff-toolkit 0.18.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- devtools/scripts/build_cookiecutter_json.py +22 -0
- docs/conf.py +296 -0
- docs/conftest.py +23 -0
- examples/conformer_energies/conformer_energies.py +143 -0
- examples/deprecated/SMIRNOFF_comparison/compare_molecule_energies.py +48 -0
- examples/deprecated/SMIRNOFF_comparison/compare_set_energies.py +58 -0
- examples/deprecated/liquid_box/create_mol2_pdb/create_mol2_pdb.py +193 -0
- examples/deprecated/liquid_box/openff_simulation.py +72 -0
- examples/deprecated/mixedFF_structure/generate_mixedFF_complex.py +56 -0
- examples/deprecated/testing/test_impropers/tleap_tools.py +120 -0
- examples/examples_helper.py +61 -0
- openff/toolkit/__init__.py +101 -0
- openff/toolkit/_tests/__init__.py +0 -0
- openff/toolkit/_tests/_stale_tests.py +146 -0
- openff/toolkit/_tests/conftest.py +108 -0
- openff/toolkit/_tests/create_molecules.py +494 -0
- openff/toolkit/_tests/mocking.py +145 -0
- openff/toolkit/_tests/molecule/__init__.py +1 -0
- openff/toolkit/_tests/molecule/test_state_enumeration.py +302 -0
- openff/toolkit/_tests/test_energies.py +136 -0
- openff/toolkit/_tests/test_examples.py +109 -0
- openff/toolkit/_tests/test_forcefield.py +3864 -0
- openff/toolkit/_tests/test_interchange.py +19 -0
- openff/toolkit/_tests/test_io.py +19 -0
- openff/toolkit/_tests/test_links.py +73 -0
- openff/toolkit/_tests/test_mm_molecule.py +404 -0
- openff/toolkit/_tests/test_molecule.py +4246 -0
- openff/toolkit/_tests/test_nagl.py +169 -0
- openff/toolkit/_tests/test_parameter_plugins.py +89 -0
- openff/toolkit/_tests/test_parameters.py +2898 -0
- openff/toolkit/_tests/test_toolkit_io.py +1088 -0
- openff/toolkit/_tests/test_toolkits.py +4418 -0
- openff/toolkit/_tests/test_topology.py +2485 -0
- openff/toolkit/_tests/test_utils.py +101 -0
- openff/toolkit/_tests/test_utils_callback.py +178 -0
- openff/toolkit/_tests/test_utils_collections.py +263 -0
- openff/toolkit/_tests/test_utils_serialization.py +168 -0
- openff/toolkit/_tests/utils.py +1747 -0
- openff/toolkit/data/README.md +8 -0
- openff/toolkit/data/molecules/AlkEthOH_test_filt1_ff.mol2 +2026 -0
- openff/toolkit/data/molecules/AlkEthOH_test_filt1_tripos.mol2 +2026 -0
- openff/toolkit/data/molecules/AlkEthOH_tripos.tar.gz +0 -0
- openff/toolkit/data/molecules/CID20742535_anion.sdf +50 -0
- openff/toolkit/data/molecules/CID20742535_neutral.sdf +51 -0
- openff/toolkit/data/molecules/FreeSolv.tar.gz +0 -0
- openff/toolkit/data/molecules/MiniDrugBank.sdf +33988 -0
- openff/toolkit/data/molecules/MiniDrugBank_tripos.mol2 +35462 -0
- openff/toolkit/data/molecules/PT2385.sdf +90 -0
- openff/toolkit/data/molecules/QL8.sdf +401 -0
- openff/toolkit/data/molecules/README.md +21 -0
- openff/toolkit/data/molecules/benzene.crd +8 -0
- openff/toolkit/data/molecules/benzene.mol2 +34 -0
- openff/toolkit/data/molecules/benzene.top +166 -0
- openff/toolkit/data/molecules/butane_multi.sdf +231 -0
- openff/toolkit/data/molecules/cyclohexane.mol2 +44 -0
- openff/toolkit/data/molecules/ethanol.mol2 +25 -0
- openff/toolkit/data/molecules/ethanol.sdf +23 -0
- openff/toolkit/data/molecules/ethanol_partial_charges.sdf +26 -0
- openff/toolkit/data/molecules/ethanol_reordered.mol2 +25 -0
- openff/toolkit/data/molecules/ethanol_reordered.sdf +23 -0
- openff/toolkit/data/molecules/ethene_rdkit.sdf +17 -0
- openff/toolkit/data/molecules/laromustine_ff.mol2 +71 -0
- openff/toolkit/data/molecules/laromustine_tripos.mol2 +71 -0
- openff/toolkit/data/molecules/methane_multiconformer.sdf +32 -0
- openff/toolkit/data/molecules/methane_multiconformer_properties.sdf +45 -0
- openff/toolkit/data/molecules/nh4.pdb +7 -0
- openff/toolkit/data/molecules/po4_phenylphosphate.pdb +46 -0
- openff/toolkit/data/molecules/ruxolitinib_conformers.sdf +910 -0
- openff/toolkit/data/molecules/toluene.mol2 +38 -0
- openff/toolkit/data/molecules/toluene.mol2.gz +0 -0
- openff/toolkit/data/molecules/toluene.pdb +30 -0
- openff/toolkit/data/molecules/toluene.sdf +36 -0
- openff/toolkit/data/molecules/toluene_charged.mol2 +38 -0
- openff/toolkit/data/molecules/z_3_hydroxy_propenal.sdf +23 -0
- openff/toolkit/data/proteins/5tbm_complex_solv.pdb +1928 -0
- openff/toolkit/data/proteins/5tbm_complex_solv_box.pdb +19563 -0
- openff/toolkit/data/proteins/CTerminal_ALA.pdb +20 -0
- openff/toolkit/data/proteins/CTerminal_ALA.sdf +43 -0
- openff/toolkit/data/proteins/CTerminal_ALA_ALA.pdb +30 -0
- openff/toolkit/data/proteins/CTerminal_ALA_ALA_misnamedH.pdb +30 -0
- openff/toolkit/data/proteins/CTerminal_ARG.pdb +34 -0
- openff/toolkit/data/proteins/CTerminal_TRP.pdb +34 -0
- openff/toolkit/data/proteins/MainChain_ALA.pdb +25 -0
- openff/toolkit/data/proteins/MainChain_ALA.sdf +52 -0
- openff/toolkit/data/proteins/MainChain_ALA_ALA.pdb +35 -0
- openff/toolkit/data/proteins/MainChain_ALA_ALA.sdf +69 -0
- openff/toolkit/data/proteins/MainChain_ALA_ALA_icodes.pdb +35 -0
- openff/toolkit/data/proteins/MainChain_ALA_ALA_no_hydrogens.pdb +17 -0
- openff/toolkit/data/proteins/MainChain_ARG.pdb +39 -0
- openff/toolkit/data/proteins/MainChain_ARG.sdf +81 -0
- openff/toolkit/data/proteins/MainChain_CYM.pdb +25 -0
- openff/toolkit/data/proteins/MainChain_CYS.pdb +26 -0
- openff/toolkit/data/proteins/MainChain_CYX.pdb +48 -0
- openff/toolkit/data/proteins/MainChain_CYX.sdf +96 -0
- openff/toolkit/data/proteins/MainChain_GLH.sdf +64 -0
- openff/toolkit/data/proteins/MainChain_GLU.sdf +63 -0
- openff/toolkit/data/proteins/MainChain_HID.pdb +32 -0
- openff/toolkit/data/proteins/MainChain_HIE.pdb +32 -0
- openff/toolkit/data/proteins/MainChain_HIE.sdf +67 -0
- openff/toolkit/data/proteins/MainChain_HIP.pdb +33 -0
- openff/toolkit/data/proteins/MainChain_HIP.sdf +70 -0
- openff/toolkit/data/proteins/MainChain_PRO.pdb +29 -0
- openff/toolkit/data/proteins/MainChain_TRP.pdb +39 -0
- openff/toolkit/data/proteins/NTerminal_ALA.pdb +21 -0
- openff/toolkit/data/proteins/NTerminal_ALA.sdf +45 -0
- openff/toolkit/data/proteins/NTerminal_ALA_ALA.pdb +31 -0
- openff/toolkit/data/proteins/NTerminal_ARG.pdb +35 -0
- openff/toolkit/data/proteins/NTerminal_TRP.pdb +35 -0
- openff/toolkit/data/proteins/T4-protein-unprocessed-4w51.pdb +1892 -0
- openff/toolkit/data/proteins/T4-protein.mol2 +5461 -0
- openff/toolkit/data/proteins/T4-protein.pdb +2637 -0
- openff/toolkit/data/proteins/T4-protein.sdf +5318 -0
- openff/toolkit/data/proteins/T4_protein_waters.pdb +2965 -0
- openff/toolkit/data/proteins/TwoMol_SER_CYS.pdb +50 -0
- openff/toolkit/data/proteins/aa_residues_substructures_explicit_bond_orders_with_caps.json +6435 -0
- openff/toolkit/data/proteins/aa_residues_substructures_explicit_bond_orders_with_caps_explicit_connectivity.json +6675 -0
- openff/toolkit/data/proteins/aa_residues_substructures_with_caps.json +6297 -0
- openff/toolkit/data/proteins/ace-ZZZ-gly-nme.pdb +78 -0
- openff/toolkit/data/proteins/ace-ZZZ-gly-nme.sdf +98 -0
- openff/toolkit/data/proteins/ace-a10-nme.sdf +229 -0
- openff/toolkit/data/proteins/ace-ala-nh2.pdb +21 -0
- openff/toolkit/data/proteins/fluoresceine_dyed_helix_capped.pdb +338 -0
- openff/toolkit/data/proteins/neutral_arginine.pdb +31 -0
- openff/toolkit/data/proteins/split_chain.pdb +74 -0
- openff/toolkit/data/proteins/split_chain_reordered.pdb +74 -0
- openff/toolkit/data/reference_energies/README.md +6 -0
- openff/toolkit/data/reference_energies/reference_0.8.0.json +1 -0
- openff/toolkit/data/reference_energies/reference_0.8.0.json.txt +180 -0
- openff/toolkit/data/reference_energies/reference_after_1007.json +1 -0
- openff/toolkit/data/reference_energies/reference_after_1007.json.txt +350 -0
- openff/toolkit/data/reference_energies/reference_after_802.json +1 -0
- openff/toolkit/data/reference_energies/reference_after_802.json.txt +291 -0
- openff/toolkit/data/systems/README.md +3 -0
- openff/toolkit/data/systems/amber/cyclohexane_ethanol_0.4_0.6.inpcrd +1511 -0
- openff/toolkit/data/systems/amber/cyclohexane_ethanol_0.4_0.6.prmtop +13367 -0
- openff/toolkit/data/systems/monomers/butanol.mol2 +38 -0
- openff/toolkit/data/systems/monomers/butanol.sdf +88 -0
- openff/toolkit/data/systems/monomers/cyclohexane.mol2 +45 -0
- openff/toolkit/data/systems/monomers/cyclohexane.sdf +105 -0
- openff/toolkit/data/systems/monomers/ethanol.mol2 +26 -0
- openff/toolkit/data/systems/monomers/ethanol.sdf +58 -0
- openff/toolkit/data/systems/monomers/methane.mol2 +18 -0
- openff/toolkit/data/systems/monomers/methane.sdf +38 -0
- openff/toolkit/data/systems/monomers/propane.mol2 +30 -0
- openff/toolkit/data/systems/monomers/propane.sdf +68 -0
- openff/toolkit/data/systems/monomers/tip3p_water.mol2 +14 -0
- openff/toolkit/data/systems/monomers/water.mol2 +14 -0
- openff/toolkit/data/systems/monomers/water.sdf +28 -0
- openff/toolkit/data/systems/packmol_boxes/cyclohexane_ethanol_0.4_0.6.pdb +6272 -0
- openff/toolkit/data/systems/packmol_boxes/cyclohexane_water.pdb +5524 -0
- openff/toolkit/data/systems/packmol_boxes/ethanol_water.pdb +5174 -0
- openff/toolkit/data/systems/packmol_boxes/propane_methane_butanol_0.2_0.3_0.5.pdb +7959 -0
- openff/toolkit/data/systems/test_systems/1_cyclohexane_1_ethanol.pdb +61 -0
- openff/toolkit/data/systems/test_systems/1_ethanol.pdb +24 -0
- openff/toolkit/data/systems/test_systems/1_ethanol_no_conect.pdb +15 -0
- openff/toolkit/data/systems/test_systems/1_ethanol_reordered.pdb +24 -0
- openff/toolkit/data/systems/test_systems/PE.pdb +67 -0
- openff/toolkit/data/systems/test_systems/README.md +3 -0
- openff/toolkit/data/systems/test_systems/T4_lysozyme_water_ions.pdb +29240 -0
- openff/toolkit/data/test_forcefields/Frosst_AlkEthOH.offxml +61 -0
- openff/toolkit/data/test_forcefields/Frosst_AlkEthOH_GBSA.offxml +78 -0
- openff/toolkit/data/test_forcefields/Frosst_AlkEthOH_MDL.offxml +62 -0
- openff/toolkit/data/test_forcefields/Frosst_AlkEthOH_parmAtFrosst.offxml +64 -0
- openff/toolkit/data/test_forcefields/GBSA_HCT-1.0.offxml +21 -0
- openff/toolkit/data/test_forcefields/GBSA_OBC1-1.0.offxml +18 -0
- openff/toolkit/data/test_forcefields/GBSA_OBC2-1.0.offxml +18 -0
- openff/toolkit/data/test_forcefields/README.md +23 -0
- openff/toolkit/data/test_forcefields/ammonia_minimal.offxml +27 -0
- openff/toolkit/data/test_forcefields/benzene_minimal.offxml +29 -0
- openff/toolkit/data/test_forcefields/chargeincrement-test.offxml +16 -0
- openff/toolkit/data/test_forcefields/frcmod.Frosst_AlkEthOH_withIDs +85 -0
- openff/toolkit/data/test_forcefields/ion_charges.offxml +18 -0
- openff/toolkit/data/test_forcefields/mangled.offxml +9 -0
- openff/toolkit/data/test_forcefields/old/README.md +9 -0
- openff/toolkit/data/test_forcefields/old/hbonds.offxml +11 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_2_spec_0_1.offxml +332 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_2_spec_0_2.offxml +338 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_2_spec_0_3.offxml +329 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_fixed_spec_0_1.offxml +342 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_fixed_spec_0_2.offxml +348 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_fixed_spec_0_3.offxml +337 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_spec_0_1.offxml +340 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_spec_0_2.offxml +346 -0
- openff/toolkit/data/test_forcefields/old/test_ff_0_0_4_spec_0_3.offxml +337 -0
- openff/toolkit/data/test_forcefields/smirff99Frosst_reference_0_1_spec.offxml +349 -0
- openff/toolkit/data/test_forcefields/smirnoff99Frosst_experimental.offxml +343 -0
- openff/toolkit/data/test_forcefields/smirnoff99Frosst_reference_0_1_spec.offxml +349 -0
- openff/toolkit/data/test_forcefields/smirnoff99Frosst_reference_0_2_spec.offxml +356 -0
- openff/toolkit/data/test_forcefields/test_forcefield.offxml +346 -0
- openff/toolkit/data/test_forcefields/tip3p.offxml +22 -0
- openff/toolkit/data/test_forcefields/unsupported_smirnoff_version.offxml +9 -0
- openff/toolkit/topology/__init__.py +43 -0
- openff/toolkit/topology/_mm_molecule.py +663 -0
- openff/toolkit/topology/molecule.py +6102 -0
- openff/toolkit/topology/topology.py +2555 -0
- openff/toolkit/typing/__init__.py +1 -0
- openff/toolkit/typing/engines/__init__.py +1 -0
- openff/toolkit/typing/engines/smirnoff/__init__.py +31 -0
- openff/toolkit/typing/engines/smirnoff/forcefield.py +1440 -0
- openff/toolkit/typing/engines/smirnoff/io.py +234 -0
- openff/toolkit/typing/engines/smirnoff/parameters.py +3839 -0
- openff/toolkit/typing/engines/smirnoff/plugins.py +91 -0
- openff/toolkit/utils/__init__.py +57 -0
- openff/toolkit/utils/_viz.py +104 -0
- openff/toolkit/utils/ambertools_wrapper.py +507 -0
- openff/toolkit/utils/base_wrapper.py +268 -0
- openff/toolkit/utils/builtin_wrapper.py +136 -0
- openff/toolkit/utils/callback.py +187 -0
- openff/toolkit/utils/collections.py +247 -0
- openff/toolkit/utils/constants.py +18 -0
- openff/toolkit/utils/exceptions.py +766 -0
- openff/toolkit/utils/nagl_wrapper.py +164 -0
- openff/toolkit/utils/openeye_wrapper.py +2750 -0
- openff/toolkit/utils/rdkit_wrapper.py +3302 -0
- openff/toolkit/utils/serialization.py +466 -0
- openff/toolkit/utils/toolkit_registry.py +509 -0
- openff/toolkit/utils/toolkits.py +217 -0
- openff/toolkit/utils/utils.py +805 -0
- openff_toolkit-0.18.0.dist-info/METADATA +109 -0
- openff_toolkit-0.18.0.dist-info/RECORD +231 -0
- openff_toolkit-0.18.0.dist-info/WHEEL +5 -0
- openff_toolkit-0.18.0.dist-info/licenses/LICENSE +21 -0
- openff_toolkit-0.18.0.dist-info/top_level.txt +5 -0
- utilities/deprecated/convert_frosst/convert_frcmod_0.1.py +251 -0
- utilities/deprecated/convert_frosst/convert_frcmod_0.2.py +249 -0
- utilities/deprecated/convert_frosst/convert_frcmod_0.3.py +249 -0
- utilities/make_substructure_dict/_cif_to_substructure_dict.py +955 -0
- utilities/make_substructure_dict/_make_chemical_substructures.py +46 -0
- utilities/make_substructure_dict/_make_metadata_assignment_substructures.py +35 -0
- utilities/test_plugins/custom_plugins/handler_plugins.py +48 -0
- utilities/test_plugins/setup.py +19 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Custom collections classes.
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"ValidatedDict",
|
|
8
|
+
"ValidatedList",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
from collections import abc
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ValidatedList(list):
|
|
16
|
+
"""A list that runs custom converter and validators when new elements are added.
|
|
17
|
+
|
|
18
|
+
Multiple converters and validators can be assigned to the list. These
|
|
19
|
+
are executed in the given order with converters run before validators.
|
|
20
|
+
|
|
21
|
+
Validators must take the new element as the first argument and raise
|
|
22
|
+
an exception if validation fails.
|
|
23
|
+
|
|
24
|
+
validator(new_element) -> None
|
|
25
|
+
|
|
26
|
+
Converters must also take the new element as the first argument, but
|
|
27
|
+
they have to return the converted value.
|
|
28
|
+
|
|
29
|
+
converter(new_element) -> converted_value
|
|
30
|
+
|
|
31
|
+
Examples
|
|
32
|
+
--------
|
|
33
|
+
We can define validator and converter functions that are run on each
|
|
34
|
+
element of the list.
|
|
35
|
+
|
|
36
|
+
>>> def is_positive_validator(value):
|
|
37
|
+
... if value <= 0:
|
|
38
|
+
... raise TypeError('value must be positive')
|
|
39
|
+
...
|
|
40
|
+
>>> vl = ValidatedList([1, -1], validator=is_positive_validator)
|
|
41
|
+
Traceback (most recent call last):
|
|
42
|
+
...
|
|
43
|
+
TypeError: value must be positive
|
|
44
|
+
|
|
45
|
+
Multiple converters that are run before the validators can be specified.
|
|
46
|
+
|
|
47
|
+
>>> vl = ValidatedList([-1, '2', 3.0], converter=[float, abs],
|
|
48
|
+
... validator=is_positive_validator)
|
|
49
|
+
>>> vl
|
|
50
|
+
[1.0, 2.0, 3.0]
|
|
51
|
+
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(self, seq=(), converter=None, validator=None):
|
|
55
|
+
"""
|
|
56
|
+
Initialize the list.
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
seq
|
|
61
|
+
A sequence of elements.
|
|
62
|
+
converter
|
|
63
|
+
Functions that will be used to convert each new element of
|
|
64
|
+
the list.
|
|
65
|
+
validator
|
|
66
|
+
Functions that will be used to convert each new element of
|
|
67
|
+
the list.
|
|
68
|
+
|
|
69
|
+
"""
|
|
70
|
+
# Make sure converter and validator are always iterables.
|
|
71
|
+
if not (converter is None or isinstance(converter, abc.Iterable)):
|
|
72
|
+
converter = [converter]
|
|
73
|
+
if not (validator is None or isinstance(validator, abc.Iterable)):
|
|
74
|
+
validator = [validator]
|
|
75
|
+
self._converters = converter
|
|
76
|
+
self._validators = validator
|
|
77
|
+
|
|
78
|
+
# Validate and convert the whole sequence.
|
|
79
|
+
seq = self._convert_and_validate(seq)
|
|
80
|
+
super().__init__(seq)
|
|
81
|
+
|
|
82
|
+
def extend(self, iterable):
|
|
83
|
+
iterable = self._convert_and_validate(iterable)
|
|
84
|
+
super().extend(iterable)
|
|
85
|
+
|
|
86
|
+
def append(self, p_object):
|
|
87
|
+
p_object = self._convert_and_validate([p_object])[0]
|
|
88
|
+
super().append(p_object)
|
|
89
|
+
|
|
90
|
+
def insert(self, index, p_object):
|
|
91
|
+
p_object = self._convert_and_validate([p_object])[0]
|
|
92
|
+
super().insert(index, p_object)
|
|
93
|
+
|
|
94
|
+
def __iadd__(self, other): # type: ignore[misc]
|
|
95
|
+
other = self._convert_and_validate(other)
|
|
96
|
+
return super().__iadd__(other)
|
|
97
|
+
|
|
98
|
+
def __setitem__(self, key, value):
|
|
99
|
+
if isinstance(key, slice):
|
|
100
|
+
value = self._convert_and_validate(value)
|
|
101
|
+
else:
|
|
102
|
+
value = self._convert_and_validate([value])[0]
|
|
103
|
+
super().__setitem__(key, value)
|
|
104
|
+
|
|
105
|
+
def copy(self):
|
|
106
|
+
# Make sure a shallow copy still returns a ValidatedList.
|
|
107
|
+
return self.__class__(self)
|
|
108
|
+
|
|
109
|
+
def __getitem__(self, item):
|
|
110
|
+
# Make sure a slice returns a ValidatedList.
|
|
111
|
+
if isinstance(item, slice):
|
|
112
|
+
return self.__class__(super().__getitem__(item))
|
|
113
|
+
return super().__getitem__(item)
|
|
114
|
+
|
|
115
|
+
# This is needed for pickling. See https://github.com/openforcefield/openff-toolkit/issues/411
|
|
116
|
+
# for more details.
|
|
117
|
+
# TODO: Is there a cleaner way (getstate/setstate perhaps?) to allow FFs to be
|
|
118
|
+
# pickled?
|
|
119
|
+
def __reduce__(self):
|
|
120
|
+
return (self.__class__, (list(self),), self.__dict__)
|
|
121
|
+
|
|
122
|
+
def _convert_and_validate(self, seq):
|
|
123
|
+
"""Run all converters and the validator on the given sequence."""
|
|
124
|
+
# Run all element converters.
|
|
125
|
+
if self._converters is not None:
|
|
126
|
+
for converter in self._converters:
|
|
127
|
+
seq = [converter(element) for element in seq]
|
|
128
|
+
|
|
129
|
+
# Run all element validators.
|
|
130
|
+
if self._validators is not None:
|
|
131
|
+
for validator in self._validators:
|
|
132
|
+
for element in seq:
|
|
133
|
+
validator(element)
|
|
134
|
+
return seq
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class ValidatedDict(dict):
|
|
138
|
+
"""A dict that runs custom converter and validators when new
|
|
139
|
+
elements are added.
|
|
140
|
+
|
|
141
|
+
Multiple converters and validators can be assigned to the dict.
|
|
142
|
+
These are executed in the given order with converters run before
|
|
143
|
+
validators.
|
|
144
|
+
|
|
145
|
+
Validators must take the new element as the first argument and raise
|
|
146
|
+
an exception if validation fails.
|
|
147
|
+
|
|
148
|
+
validator(new_element) -> None
|
|
149
|
+
|
|
150
|
+
Converters must also take the new element as the first argument, but
|
|
151
|
+
they have to return the converted value.
|
|
152
|
+
|
|
153
|
+
converter(new_element) -> converted_value
|
|
154
|
+
|
|
155
|
+
Examples
|
|
156
|
+
--------
|
|
157
|
+
We can define validator and converter functions that are run on each
|
|
158
|
+
value of the dict.
|
|
159
|
+
|
|
160
|
+
>>> def is_positive_validator(value):
|
|
161
|
+
... if value <= 0:
|
|
162
|
+
... raise TypeError('value must be positive')
|
|
163
|
+
...
|
|
164
|
+
>>> vl = ValidatedDict({'a': 1, 'b': -1}, validator=is_positive_validator)
|
|
165
|
+
Traceback (most recent call last):
|
|
166
|
+
...
|
|
167
|
+
TypeError: value must be positive
|
|
168
|
+
|
|
169
|
+
Multiple converters that are run before the validators can be specified.
|
|
170
|
+
|
|
171
|
+
>>> vl = ValidatedDict({'c': -1, 'd': '2', 'e': 3.0}, converter=[float, abs],
|
|
172
|
+
... validator=is_positive_validator)
|
|
173
|
+
>>> vl
|
|
174
|
+
{'c': 1.0, 'd': 2.0, 'e': 3.0}
|
|
175
|
+
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
def __init__(self, mapping, converter=None, validator=None):
|
|
179
|
+
"""
|
|
180
|
+
Initialize the dict.
|
|
181
|
+
|
|
182
|
+
Parameters
|
|
183
|
+
----------
|
|
184
|
+
mapping
|
|
185
|
+
A mapping of elements, probably a dict.
|
|
186
|
+
converter
|
|
187
|
+
Functions that will be used to convert each new element of
|
|
188
|
+
the dict.
|
|
189
|
+
validator
|
|
190
|
+
Functions that will be used to convert each new element of
|
|
191
|
+
the dict.
|
|
192
|
+
|
|
193
|
+
"""
|
|
194
|
+
# Make sure converter and validator are always iterables.
|
|
195
|
+
if not (converter is None or isinstance(converter, abc.Iterable)):
|
|
196
|
+
converter = [converter]
|
|
197
|
+
if not (validator is None or isinstance(validator, abc.Iterable)):
|
|
198
|
+
validator = [validator]
|
|
199
|
+
self._converters = converter
|
|
200
|
+
self._validators = validator
|
|
201
|
+
|
|
202
|
+
# Validate and convert the whole mapping
|
|
203
|
+
mapping = self._convert_and_validate(mapping)
|
|
204
|
+
super().__init__(mapping)
|
|
205
|
+
|
|
206
|
+
def update(self, *args, **kwargs):
|
|
207
|
+
if len(args) != 1:
|
|
208
|
+
raise TypeError(f"ValidatedDict.update expected exactly one positional argument, got {len(args)} instead.")
|
|
209
|
+
if len(kwargs) != 0:
|
|
210
|
+
raise TypeError(f"ValidatedDict.update does not accept named arguments, got {kwargs} instead.")
|
|
211
|
+
other = self._convert_and_validate(dict(args[0]))
|
|
212
|
+
super().update(other)
|
|
213
|
+
|
|
214
|
+
def copy(self):
|
|
215
|
+
return self.__class__(self)
|
|
216
|
+
|
|
217
|
+
def __setitem__(self, key, value):
|
|
218
|
+
value = self._convert_and_validate({None: value})[None]
|
|
219
|
+
super().__setitem__(key, value)
|
|
220
|
+
|
|
221
|
+
# This is needed for pickling. See https://github.com/openforcefield/openff-toolkit/issues/411
|
|
222
|
+
# for more details.
|
|
223
|
+
# TODO: Is there a cleaner way (getstate/setstate perhaps?) to allow FFs to be
|
|
224
|
+
# pickled?
|
|
225
|
+
def __reduce__(self):
|
|
226
|
+
return (self.__class__, (dict(self),), self.__dict__)
|
|
227
|
+
|
|
228
|
+
def _convert_and_validate(self, mapping):
|
|
229
|
+
"""Run all converters and the validator on the given mapping."""
|
|
230
|
+
|
|
231
|
+
# Run all element converters.
|
|
232
|
+
if self._converters is not None:
|
|
233
|
+
for converter in self._converters:
|
|
234
|
+
mapping = {key: converter(value) for key, value in mapping.items()}
|
|
235
|
+
|
|
236
|
+
# Run all element validators.
|
|
237
|
+
if self._validators is not None:
|
|
238
|
+
for validator in self._validators:
|
|
239
|
+
for value in mapping.values():
|
|
240
|
+
validator(value)
|
|
241
|
+
return mapping
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
if __name__ == "__main__":
|
|
245
|
+
import doctest
|
|
246
|
+
|
|
247
|
+
doctest.run_docstring_examples(ValidatedList, globals())
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
__all__ = (
|
|
2
|
+
"ALLOWED_AROMATICITY_MODELS",
|
|
3
|
+
"ALLOWED_CHARGE_MODELS",
|
|
4
|
+
"ALLOWED_FRACTIONAL_BOND_ORDER_MODELS",
|
|
5
|
+
"DEFAULT_AROMATICITY_MODEL",
|
|
6
|
+
"DEFAULT_CHARGE_MODEL",
|
|
7
|
+
"DEFAULT_FRACTIONAL_BOND_ORDER_MODEL",
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
ALLOWED_AROMATICITY_MODELS = ["OEAroModel_MDL"]
|
|
12
|
+
DEFAULT_AROMATICITY_MODEL = ALLOWED_AROMATICITY_MODELS[0]
|
|
13
|
+
|
|
14
|
+
ALLOWED_FRACTIONAL_BOND_ORDER_MODELS = ["Wiberg"]
|
|
15
|
+
DEFAULT_FRACTIONAL_BOND_ORDER_MODEL = ALLOWED_FRACTIONAL_BOND_ORDER_MODELS[0]
|
|
16
|
+
|
|
17
|
+
ALLOWED_CHARGE_MODELS = ["AM1-BCC"]
|
|
18
|
+
DEFAULT_CHARGE_MODEL = ALLOWED_CHARGE_MODELS[0]
|