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

Remove type map in GenericDataContainer and add print of data tree #1040

Merged
merged 5 commits into from
Jul 19, 2023
Merged
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
12 changes: 12 additions & 0 deletions src/ansys/dpf/core/data_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,18 @@ def __setattr__(self, key, value):
return super.__setattr__(self, key, value)
self.add({key: value})

def __str__(self):
"""Describe the entity.

Returns
-------
str
Description of the entity.
"""
from ansys.dpf.core.core import _description

return _description(self._internal_obj, self._server)

def __del__(self):
try:
# needs a proper deleter only when real datatree and not dict
Expand Down
30 changes: 7 additions & 23 deletions src/ansys/dpf/core/generic_data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import traceback
import warnings
import builtins

from ansys.dpf.core import server as server_module
from ansys.dpf.core import errors
Expand Down Expand Up @@ -110,31 +111,14 @@
"""
any_ptr = self._api.generic_data_container_get_property_any(self, property_name)
any_dpf = Any(any_ptr, self._server)
output_type = self._type_to_output_method[self.get_property_description()[property_name]]
return any_dpf.cast(output_type)
output_type = self.get_property_description()[property_name]
class_ = getattr(builtins, output_type, None)
if class_ is None:
from ansys.dpf import core

Check warning on line 117 in src/ansys/dpf/core/generic_data_container.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/generic_data_container.py#L114-L117

Added lines #L114 - L117 were not covered by tests

@property
def _type_to_output_method(self):
# Only the types in any.py need to be casted
from ansys.dpf.core import (
field,
property_field,
string_field,
scoping,
)
class_ = getattr(core, output_type)

Check warning on line 119 in src/ansys/dpf/core/generic_data_container.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/generic_data_container.py#L119

Added line #L119 was not covered by tests

out = {
"bool": bool,
"int": int,
"str": str,
"float": float,
"Field": field.Field,
"PropertyField": property_field.PropertyField,
"StringField": string_field.StringField,
"Scoping": scoping.Scoping,
"GenericDataContainer": GenericDataContainer,
}
return out
return any_dpf.cast(class_)

Check warning on line 121 in src/ansys/dpf/core/generic_data_container.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/generic_data_container.py#L121

Added line #L121 was not covered by tests

def get_property_description(self):
"""Get a dictionary description of properties by name and data type
Expand Down
13 changes: 13 additions & 0 deletions tests/test_data_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ def test_read_from_txt_data_tree(server_type):
assert data_tree.has("list_string")


@conftest.raises_for_servers_version_under("4.0")
def test_print_data_tree(server_type):
data_tree = dpf.DataTree(server=server_type)
with data_tree.to_fill() as to_fill:
to_fill.int = 1
to_fill.double = 1.0
to_fill.string = "hello"
to_fill.list_int = [1, 2]
to_fill.list_double = [1.5, 2.5]
to_fill.add(list_string=["hello", "bye"])
assert str(data_tree) != ""


@conftest.raises_for_servers_version_under("4.0")
def test_sub_data_tree():
data_tree = dpf.DataTree()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_generic_data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_set_get_data_tree_generic_data_container(server_type):
entity = dpf.DataTree(server=server_type)
entity.add(name="john")
gdc.set_property("persons", entity)
new_entity = gdc.get_property("persons", dpf.DataTree)
new_entity = gdc.get_property("persons")
assert new_entity.get_as("name") == "john"


Expand Down
Loading