Commit 1ad8d841 authored by Alvin Noe Ladines's avatar Alvin Noe Ladines
Browse files

Implemented metainfo m_xpath using jmespath

parent 918e47ad
Pipeline #75389 failed with stages
in 28 minutes and 3 seconds
......@@ -29,6 +29,7 @@ import aniso8601
from datetime import datetime
import pytz
import docstring_parser
import jmespath
m_package: 'Package' = None
......@@ -1596,6 +1597,26 @@ 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):
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):
......
......@@ -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