Skip to content

Commit

Permalink
Merge pull request #89 from molssi-seamm/dev
Browse files Browse the repository at this point in the history
Significant internal enhancement to property handling.
  • Loading branch information
seamm authored Dec 7, 2024
2 parents f33ebfa + bc32f76 commit 2c47cf0
Show file tree
Hide file tree
Showing 10 changed files with 550 additions and 349 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
=======
History
=======
2024.12.7 -- Significant internal enhancement to property handling.
* An internal change, allowing listing and getting properties with wildcards,
working with multiple values at once. This is a significant change, but should
not affect the user interface. Also consolidated the property handling code for
configurations vs systems.

2024.11.27.1 -- Added support for charge in chemical formulae
* Added support for charge in chemical formulae, e.g. [H2 O]+.

Expand Down
84 changes: 52 additions & 32 deletions molsystem/configuration_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,37 @@ def exists(self, name):
"""A thin wrapper of the _Properties method."""
return self.properties.exists(name)

def get(self, _property="all", include_system_properties=False):
"""Get the given property value for this configuration.
def get(
self,
pattern="*",
match="glob",
include_system_properties=False,
types=["float", "int", "str", "json"],
):
"""Get the property value(s)
Parameters
----------
_property : int or str
The id or name of the property.
pattern : str = "*"
The pattern of the property.
match : str="glob"
Whether to use exact, glob, or 'like' matching.
include_system_properties : bool=False
Whether to include properties that are on the system, not any configuration
For a configuration, whether to include properties that are on the system.
types : [str] = ["float", "int", "str", "json"]
The type of results to return.
Returns
-------
int, float, or str
The value of the property.
{str: {str: value}}
The matching property values.
"""
return self.properties.get(
self._cid,
_property,
pattern=pattern,
match=match,
include_system_properties=include_system_properties,
types=types,
)

def id(self, name):
Expand All @@ -98,35 +110,43 @@ def known_properties(self):
"""List the known properties."""
return self.properties.known_properties()

def list(self):
"""Return all of the property names for the current configuration.
Returns
-------
[str]
The names of the properties for the configuration.
"""
sql = "SELECT name FROM property WHERE id IN ("
sql += "SELECT property FROM float_data WHERE configuration = ?"
sql += "UNION SELECT property FROM int_data WHERE configuration = ?"
sql += "UNION SELECT property FROM str_data WHERE configuration = ?"
sql += ")"
def list(
self,
pattern="*",
match="glob",
include_system_properties=False,
as_ids=False,
types=["float", "int", "str", "json"],
):
"""Get the list of matching properties for the configuration.
self.properties.cursor.execute(sql, (self._cid, self._cid, self._cid))
return [row[0] for row in self.properties.cursor]
Parameters
----------
pattern : str = "*"
The pattern of the property.
match : str="glob"
Whether to use exact, glob, or 'like' matching.
include_system_properties : bool=False
Whether to include properties that are on the system, not any configuration
as_ids : bool=False
Whether to return the ids rather than names
types : [str] = ["float", "int", "str", "json"]
The type of results to return.
def list_ids(self):
"""Return all of the property ids for the current configuration.
Returns
-------
[int]
The names of the properties for the configuration.
[str] or [int]
The matching properties.
"""
sql = "SELECT property FROM float_data WHERE configuration = ?"
sql += "UNION SELECT property FROM int_data WHERE configuration = ?"
sql += "UNION SELECT property FROM str_data WHERE configuration = ?"

self.properties.cursor.execute(sql, (self._cid, self._cid, self._cid))
return [row[0] for row in self.properties.cursor]
return self.properties.list(
self._cid,
is_system=False,
pattern=pattern,
match=match,
include_system_properties=include_system_properties,
as_ids=as_ids,
types=types,
)

def metadata(self, _property):
"""The metadata for a property
Expand Down
6 changes: 3 additions & 3 deletions molsystem/openbabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def to_OBMol(self, properties=None):
pair.SetValue(str(self.spin_multiplicity))
ob_mol.CloneData(pair)

if properties == "all":
data = self.properties.get("all", include_system_properties=True)
if properties is not None:
data = self.properties.get(properties, include_system_properties=True)
for key, value in data.items():
pair.SetAttribute(key)
pair.SetValue(str(value))
Expand Down Expand Up @@ -318,7 +318,7 @@ def to_sdf_text(self):
"""
obConversion = ob.OBConversion()
obConversion.SetOutFormat("sdf")
obMol = self.to_OBMol(properties="all")
obMol = self.to_OBMol(properties="*")
title = f"SEAMM={self.system.name}/{self.name}"
obMol.SetTitle(title)
text = obConversion.WriteString(obMol)
Expand Down
Loading

0 comments on commit 2c47cf0

Please sign in to comment.