Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addressing Molli Functionality of Arbitrary Property Assignment #54

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion molli/chem/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ class Atom:
# repr=lambda x: x.name,
)

mdata = attrs.field(
default=attrs.Factory(dict),
repr=False
)

def evolve(self, **changes):
return attrs.evolve(self, **changes)

Expand Down Expand Up @@ -443,6 +448,12 @@ def __eq__(self, other: AtomLike):
def __hash__(self) -> int:
return id(self)

def __getitem__(self, key):
return self.mdata[key]

def __setitem__(self, key, val):
self.mdata[key] = val

@property
def implicit_valence(self) -> int:
return IMPLICIT_VALENCE[self.element.group]
Expand Down Expand Up @@ -648,7 +659,7 @@ class Promolecule:
for API compatibility reasons.
"""

__slots__ = ("_atoms", "_atom_index_cache", "_name", "charge", "mult")
__slots__ = ("_atoms", "_atom_index_cache", "_name", "charge", "mult", "mdata")

def __init__(
self,
Expand All @@ -660,6 +671,7 @@ def __init__(
copy_atoms: bool = False,
charge: int = None,
mult: int = None,
mdata: dict = None,
**kwds, # mostly just for subclassing compatibility
):
"""
Expand All @@ -671,6 +683,7 @@ def __init__(
self.name = name
self.charge = charge or 0
self.mult = mult or 1
self.mdata = mdata

match other:
case None:
Expand Down
11 changes: 11 additions & 0 deletions molli/chem/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class Bond:
converter=float,
)

mdata: dict = attrs.field(
default=attrs.Factory(dict),
repr=False
)

def evolve(self, **changes):
return attrs.evolve(self, **changes)

Expand Down Expand Up @@ -170,6 +175,12 @@ def __mod__(self, a: Atom) -> Atom:
else:
raise ValueError("Atom is not a part of this bond")

def __getitem__(self, key):
return self.mdata[key]

def __setitem__(self, key, val):
self.mdata[key] = val

@property
def expected_length(self) -> float:
r1 = self.a1.cov_radius_1
Expand Down
33 changes: 32 additions & 1 deletion molli_test/test_chem_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,36 @@ def test_del_atom(self):
self.assertEqual(mol.n_bonds, nb - 3) # atom 0 is connected to 3 bonds
self.assertEqual(mol._atomic_charges.shape, (na - 1,))

def test_mdata_instance(self):


mol = ml.Molecule.load_mol2(ml.files.dendrobine_mol2)
a1 = mol.atoms[0]
b1 = mol.atoms[0]

a1['str'] = '5'
a1['int'] = 1
a1['float'] = 1.1
a1['tuple'] = (1,1)
a1['list'] = [2,3]
a1['dict'] = {2: 'a', 3: 'b'}

self.assertIsInstance(a1['str'], str)
self.assertIsInstance(a1['int'], int)
self.assertIsInstance(a1['float'], float)
self.assertIsInstance(a1['tuple'], tuple)
self.assertIsInstance(a1['list'], list)
self.assertIsInstance(a1['dict'], dict)

b1['str'] = '5'
b1['int'] = 1
b1['float'] = 1.1
b1['tuple'] = (1,1)
b1['list'] = [2,3]
b1['dict'] = {2: 'a', 3: 'b'}

self.assertIsInstance(b1['str'], str)
self.assertIsInstance(b1['int'], int)
self.assertIsInstance(b1['float'], float)
self.assertIsInstance(b1['tuple'], tuple)
self.assertIsInstance(b1['list'], list)
self.assertIsInstance(b1['dict'], dict)