Skip to content

Commit 5f1c99f

Browse files
authored
Choose output type for a GenericDataContainer (#1403)
1 parent 0ea8fa3 commit 5f1c99f

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

src/ansys/dpf/core/generic_data_container.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
if TYPE_CHECKING: # pragma: no cover
1313
from ansys.dpf.core import Field, Scoping, StringField, GenericDataContainer
1414

15+
from ansys.dpf.core.dpf_operator import _write_output_type_to_type
16+
17+
1518
from ansys.dpf.core import server as server_module
16-
from ansys.dpf.core import errors
19+
from ansys.dpf.core import errors, types
1720
from ansys.dpf.core.any import Any
1821
from ansys.dpf.core import collection
1922
from ansys.dpf.core.mapping_types import map_types_to_python
@@ -104,21 +107,32 @@ def set_property(
104107
any_dpf = Any.new_from(prop, self._server)
105108
self._api.generic_data_container_set_property_any(self, property_name, any_dpf)
106109

107-
def get_property(self, property_name):
110+
def get_property(self, property_name, output_type: Union[None, type, types] = None):
108111
"""Get property with given name.
109112
110113
Parameters
111114
----------
112115
property_name : str
113116
Property name.
114117
118+
output_type : None, type, types, optional
119+
Expected type of the output. By default, type is deduced using
120+
`GenericDataContainer.get_property_description`.
121+
115122
Returns
116123
-------
117124
Property object instance.
118125
"""
119126
any_ptr = self._api.generic_data_container_get_property_any(self, property_name)
120127
any_dpf = Any(any_ptr, self._server)
121-
output_type = self.get_property_description()[property_name]
128+
if output_type is None:
129+
output_type = self.get_property_description()[property_name]
130+
else:
131+
if not isinstance(output_type, type):
132+
output_type = _write_output_type_to_type(output_type)
133+
134+
output_type = str(output_type.__name__)
135+
122136
class_ = getattr(builtins, output_type, None)
123137
if class_ is None:
124138
from ansys.dpf import core

tests/test_generic_data_container.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ansys.dpf import core as dpf
22
from conftest import (
3-
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0,
3+
SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0,
44
)
55
import pytest
66

@@ -68,3 +68,67 @@ def test_get_property_description_generic_data_container(server_type):
6868
"my-string": "str",
6969
"my-field": "Field",
7070
}
71+
72+
73+
@pytest.mark.skipif(
74+
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
75+
)
76+
def test_get_by_type_generic_data_container(server_type):
77+
gdc = dpf.GenericDataContainer(server=server_type)
78+
entity = 42
79+
gdc.set_property("my-int", entity)
80+
new_entity = gdc.get_property("my-int")
81+
assert 42 == new_entity
82+
new_entity = gdc.get_property("my-int", int)
83+
assert 42 == new_entity
84+
new_entity = gdc.get_property("my-int", dpf.types.int)
85+
assert 42 == new_entity
86+
87+
entity = 4.2
88+
gdc.set_property("my-float", entity)
89+
new_entity = gdc.get_property("my-float")
90+
assert 4.2 == new_entity
91+
new_entity = gdc.get_property("my-float", float)
92+
assert 4.2 == new_entity
93+
new_entity = gdc.get_property("my-float", dpf.types.double)
94+
assert 4.2 == new_entity
95+
96+
entity = "hello world"
97+
gdc.set_property("my-string", entity)
98+
new_entity = gdc.get_property("my-string")
99+
assert "hello world" == new_entity
100+
new_entity = gdc.get_property("my-string", str)
101+
assert "hello world" == new_entity
102+
new_entity = gdc.get_property("my-string", dpf.types.string)
103+
assert "hello world" == new_entity
104+
105+
entity = dpf.Field(location="phase", nature=dpf.natures.scalar, server=server_type)
106+
gdc.set_property("my-field", entity)
107+
new_entity = gdc.get_property("my-field")
108+
assert isinstance(new_entity, dpf.Field)
109+
110+
new_entity = gdc.get_property("my-field", dpf.Field)
111+
assert isinstance(new_entity, dpf.Field)
112+
113+
new_entity = gdc.get_property("my-field", dpf.types.field)
114+
assert isinstance(new_entity, dpf.Field)
115+
116+
117+
@pytest.mark.skipif(
118+
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_8_0, reason="Available for servers >=8.0"
119+
)
120+
def test_get_bytes_generic_data_container(server_type):
121+
gdc = dpf.GenericDataContainer(server=server_type)
122+
123+
entity = "hello world"
124+
gdc.set_property("my-string", entity)
125+
new_entity = gdc.get_property("my-string")
126+
assert "hello world" == new_entity
127+
new_entity = gdc.get_property("my-string", str)
128+
assert "hello world" == new_entity
129+
new_entity = gdc.get_property("my-string", dpf.types.string)
130+
assert "hello world" == new_entity
131+
new_entity = gdc.get_property("my-string", bytes)
132+
assert b"hello world" == new_entity
133+
new_entity = gdc.get_property("my-string", dpf.types.bytes)
134+
assert b"hello world" == new_entity

0 commit comments

Comments
 (0)