Commit 2fa4fa91 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Merge branch 'optimade' into 'v0.8.1'

Optimade

See merge request !116
parents 9dcd63b1 55b75081
Pipeline #76282 passed with stages
in 21 minutes and 1 second
......@@ -29,6 +29,7 @@ import aniso8601
from datetime import datetime
import pytz
import docstring_parser
import jmespath
m_package: 'Package' = None
......@@ -1596,6 +1597,40 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
def __len__(self):
return len(self.m_def.all_properties)
def get(self, key):
return self.__dict__.get(key, None)
def values(self):
return {key: val for key, val in self.__dict__.items() if not key.startswith('m_')}.values()
def m_xpath(self, expression: str):
'''
Provides an interface to jmespath search functionality.
Arguments:
expression: A string compatible with the jmespath specs representing the
search. See https://jmespath.org/ for complete description.
.. code-block:: python
metainfo_section.m_xpath('code_name')
metainfo_section.m_xpath('systems[-1].system_type')
metainfo_section.m_xpath('sccs[0].system.atom_labels')
metainfo_section.m_xpath('systems[?system_type == `molecule`].atom_labels')
metainfo_section.m_xpath('sccs[?energy_total < `1.0E-23`].system')
'''
def to_dict(entries):
if not isinstance(entries, list):
try:
entries = entries.m_to_dict()
except Exception:
pass
return entries
else:
return [to_dict(entry) for entry in entries]
result = jmespath.search(expression, self)
return to_dict(result)
class MCategory(metaclass=MObjectMeta):
......
......@@ -20,6 +20,7 @@ aniso8601<=7
ase==3.19.0
python-keycloak
elasticsearch-dsl==6.4.0
jmespath
# [parsing]
pandas
......
......@@ -613,6 +613,22 @@ class TestM1:
assert section.float_quantity is None
assert section.bool_quantity is None
def test_xpath(self):
run = Run()
run.code_name = 'amazingX'
system = run.m_create(System)
system.atom_labels = ['H', 'O']
system.system_type = 'molecule'
calc = run.m_create(SCC)
calc.energy_total = -1.20E-23
calc.system = system
assert run.m_xpath('code_name') == 'amazingX'
assert run.m_xpath('systems[-1].system_type') == 'molecule'
assert run.m_xpath('sccs[0].system.atom_labels') == ['H', 'O']
assert run.m_xpath('systems[?system_type == `molecule`].atom_labels') == [['H', 'O']]
assert run.m_xpath('sccs[?energy_total < `1.0E-23`].system') == [{'atom_labels': ['H', 'O'], 'system_type': 'molecule'}]
class TestDatatypes:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment