Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nomad-lab
nomad-FAIR
Commits
410d8d97
Commit
410d8d97
authored
Jan 12, 2021
by
Markus Scheidgen
Browse files
Added include_derived option to m_to_dict in metainfo.
parent
8ec6944e
Pipeline
#90925
canceled with stages
in 7 minutes and 58 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
nomad/metainfo/metainfo.py
View file @
410d8d97
...
@@ -1152,7 +1152,9 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
...
@@ -1152,7 +1152,9 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
return
self
.
m_def
==
definition
or
definition
in
self
.
m_def
.
all_base_sections
return
self
.
m_def
==
definition
or
definition
in
self
.
m_def
.
all_base_sections
def
m_to_dict
(
def
m_to_dict
(
self
,
with_meta
:
bool
=
False
,
include_defaults
:
bool
=
False
,
self
,
with_meta
:
bool
=
False
,
include_defaults
:
bool
=
False
,
include_derived
:
bool
=
False
,
categories
:
List
[
Union
[
'Category'
,
Type
[
'MCategory'
]]]
=
None
,
categories
:
List
[
Union
[
'Category'
,
Type
[
'MCategory'
]]]
=
None
,
partial
:
TypingCallable
[[
'Definition'
,
'MSection'
],
bool
]
=
None
)
->
Dict
[
str
,
Any
]:
partial
:
TypingCallable
[[
'Definition'
,
'MSection'
],
bool
]
=
None
)
->
Dict
[
str
,
Any
]:
'''
'''
...
@@ -1162,6 +1164,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
...
@@ -1162,6 +1164,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
with_meta: Include information about the section definition and the sections
with_meta: Include information about the section definition and the sections
position in its parent.
position in its parent.
include_defaults: Include default values of unset quantities.
include_defaults: Include default values of unset quantities.
include_derived: Include values of derived quantities.
categories: A list of category classes or category definitions that is used
categories: A list of category classes or category definitions that is used
to filter the included quantities and sub sections. Only applied to
to filter the included quantities and sub sections. Only applied to
properties of this section, not on sub-sections. Is overwritten
properties of this section, not on sub-sections. Is overwritten
...
@@ -1208,7 +1211,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
...
@@ -1208,7 +1211,7 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
for
category
in
category_defs
)
for
category
in
category_defs
)
child_partial
=
lambda
*
args
,
**
kwargs
:
True
child_partial
=
lambda
*
args
,
**
kwargs
:
True
def
serialize_quantity
(
quantity
,
is_set
):
def
serialize_quantity
(
quantity
,
is_set
,
is_derived
):
quantity_type
=
quantity
.
type
quantity_type
=
quantity
.
type
serialize
:
TypingCallable
[[
Any
],
Any
]
=
str
serialize
:
TypingCallable
[[
Any
],
Any
]
=
str
...
@@ -1274,6 +1277,8 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
...
@@ -1274,6 +1277,8 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if
is_set
:
if
is_set
:
value
=
self
.
__dict__
[
quantity
.
name
]
value
=
self
.
__dict__
[
quantity
.
name
]
elif
is_derived
:
value
=
quantity
.
derived
(
self
)
else
:
else
:
value
=
quantity
.
default
value
=
quantity
.
default
...
@@ -1300,16 +1305,18 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
...
@@ -1300,16 +1305,18 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if
not
partial
(
quantity
,
self
):
if
not
partial
(
quantity
,
self
):
continue
continue
if
quantity
.
virtual
:
try
:
continue
if
quantity
.
virtual
:
if
include_derived
and
quantity
.
derived
is
not
None
:
is_set
=
self
.
m_is_set
(
quantity
)
yield
name
,
serialize_quantity
(
quantity
,
False
,
True
)
if
not
is_set
:
if
not
include_defaults
or
not
quantity
.
m_is_set
(
Quantity
.
default
):
continue
continue
try
:
is_set
=
self
.
m_is_set
(
quantity
)
yield
name
,
serialize_quantity
(
quantity
,
is_set
)
if
not
is_set
:
if
not
include_defaults
or
not
quantity
.
m_is_set
(
Quantity
.
default
):
continue
yield
name
,
serialize_quantity
(
quantity
,
is_set
,
False
)
except
ValueError
as
e
:
except
ValueError
as
e
:
import
traceback
import
traceback
...
@@ -1325,14 +1332,18 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
...
@@ -1325,14 +1332,18 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if
self
.
m_sub_section_count
(
sub_section_def
)
>
0
:
if
self
.
m_sub_section_count
(
sub_section_def
)
>
0
:
yield
name
,
[
yield
name
,
[
None
if
item
is
None
else
item
.
m_to_dict
(
None
if
item
is
None
else
item
.
m_to_dict
(
with_meta
=
with_meta
,
include_defaults
=
include_defaults
,
with_meta
=
with_meta
,
include_defaults
=
include_defaults
,
include_derived
=
include_derived
,
partial
=
child_partial
)
partial
=
child_partial
)
for
item
in
self
.
m_get_sub_sections
(
sub_section_def
)]
for
item
in
self
.
m_get_sub_sections
(
sub_section_def
)]
else
:
else
:
sub_section
=
self
.
m_get_sub_section
(
sub_section_def
,
-
1
)
sub_section
=
self
.
m_get_sub_section
(
sub_section_def
,
-
1
)
if
sub_section
is
not
None
:
if
sub_section
is
not
None
:
yield
name
,
sub_section
.
m_to_dict
(
yield
name
,
sub_section
.
m_to_dict
(
with_meta
=
with_meta
,
include_defaults
=
include_defaults
,
with_meta
=
with_meta
,
include_defaults
=
include_defaults
,
include_derived
=
include_derived
,
partial
=
child_partial
)
partial
=
child_partial
)
return
{
key
:
value
for
key
,
value
in
items
()}
return
{
key
:
value
for
key
,
value
in
items
()}
...
...
tests/metainfo/test_metainfo.py
View file @
410d8d97
...
@@ -492,6 +492,9 @@ class TestM1:
...
@@ -492,6 +492,9 @@ class TestM1:
system
.
atom_labels
=
[
'H'
,
'H'
,
'O'
]
system
.
atom_labels
=
[
'H'
,
'H'
,
'O'
]
assert
system
.
n_atoms
==
3
assert
system
.
n_atoms
==
3
assert
'n_atoms'
not
in
system
.
m_to_dict
()
assert
'n_atoms'
in
system
.
m_to_dict
(
include_derived
=
True
)
pass
pass
def
test_derived_cached
(
self
):
def
test_derived_cached
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment