diff --git a/src/ansys/dpf/core/data_tree.py b/src/ansys/dpf/core/data_tree.py index d6025c167a6..faa5e4addac 100644 --- a/src/ansys/dpf/core/data_tree.py +++ b/src/ansys/dpf/core/data_tree.py @@ -482,7 +482,8 @@ def get_as(self, name, type_to_return=types.string): out = DataTree(data_tree=obj, server=self._server) return out - def get_attribute_names(self): + @property + def attribute_names(self): """ Returns a list of defined attribute names. @@ -495,7 +496,7 @@ def get_attribute_names(self): >>> from ansys.dpf import core as dpf >>> data_tree = dpf.DataTree() >>> data_tree.add(id=3, qualities=["nice", "funny"], name="George") - >>> data_tree.get_attribute_names() + >>> data_tree.attribute_names ['id', 'name', 'qualities'] """ coll_obj = collection.StringCollection( @@ -507,7 +508,8 @@ def get_attribute_names(self): return coll_obj.get_integral_entries() - def get_sub_tree_names(self): + @property + def sub_tree_names(self): """ Returns a list of defined sub-tree names. @@ -522,7 +524,7 @@ def get_sub_tree_names(self): >>> first_subtree = dpf.DataTree() >>> second_subtree = dpf.DataTree() >>> data_tree.add(first=first_subtree, second=second_subtree) - >>> data_tree.get_sub_tree_names() + >>> data_tree.sub_tree_names ['first', 'second'] """ coll_obj = collection.StringCollection( @@ -534,11 +536,19 @@ def get_sub_tree_names(self): return coll_obj.get_integral_entries() + @attribute_names.setter + def attribute_names(self, val): + raise AttributeError("can't set attribute") + + @sub_tree_names.setter + def sub_tree_names(self, val): + raise AttributeError("can't set attribute") + def __to_dict(self, dic): - for attribute_name in self.get_attribute_names(): + for attribute_name in self.attribute_names: dic[attribute_name] = self.get_as(attribute_name) - for sub_tree_name in self.get_sub_tree_names(): + for sub_tree_name in self.sub_tree_names: sub_tree = self.get_as(sub_tree_name, types.data_tree) sub_dic = {} sub_tree.__to_dict(sub_dic) @@ -546,7 +556,7 @@ def __to_dict(self, dic): def to_dict(self): """ - Returns a dictionary representation of the DataTree + Returns a read-only dictionary representation of the DataTree. Returns ------- @@ -568,7 +578,7 @@ def to_dict(self): return dic def __setattr__(self, key, value): - if key == "_common_keys" or key in self._common_keys: + if key == "_common_keys" or key in self._common_keys or key in dir(self): return super.__setattr__(self, key, value) self.add({key: value}) diff --git a/tests/test_data_tree.py b/tests/test_data_tree.py index 3d4db940cfe..8a30a8ab6af 100644 --- a/tests/test_data_tree.py +++ b/tests/test_data_tree.py @@ -344,7 +344,7 @@ def test_list_attributes_data_tree(server_type): to_fill.list_double = [1.5, 2.5] to_fill.add(list_string=["hello", "bye"]) - attributes = data_tree.get_attribute_names() + attributes = data_tree.attribute_names assert ["double", "int", "list_double", "list_int", "list_string", "string"] == attributes @@ -363,8 +363,8 @@ def test_list_attributes_recursive_data_tree(server_type): sub_tree02 = dpf.DataTree(server=server_type) to_fill.sub_tree02 = sub_tree02 - attributes = data_tree.get_attribute_names() - sub_trees = data_tree.get_sub_tree_names() + attributes = data_tree.attribute_names + sub_trees = data_tree.sub_tree_names assert attributes == ["attribute01"] assert sub_trees == ["sub_tree01", "sub_tree02"] @@ -374,3 +374,14 @@ def test_list_attributes_recursive_data_tree(server_type): assert ["attribute01", "sub_tree01", "sub_tree02"] == list(dic.keys()) assert {"attribute02": "2"} == dic["sub_tree01"] assert {} == dic["sub_tree02"] + + +@pytest.mark.skipif( + not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0" +) +def test_attribute_errors_data_tree(server_type): + data_tree = dpf.DataTree(server=server_type) + with pytest.raises(AttributeError, match="can't set attribute"): + data_tree.attribute_names = "hello" + with pytest.raises(AttributeError, match="can't set attribute"): + data_tree.sub_tree_names = "hello"