From 183f4401227a3619d12153c3d2c22e55b9661c39 Mon Sep 17 00:00:00 2001 From: jhenriqu Date: Wed, 19 Jul 2023 11:17:59 +0200 Subject: [PATCH 1/5] add comment changes --- src/ansys/dpf/core/data_tree.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ansys/dpf/core/data_tree.py b/src/ansys/dpf/core/data_tree.py index d6025c167a6..80405421e81 100644 --- a/src/ansys/dpf/core/data_tree.py +++ b/src/ansys/dpf/core/data_tree.py @@ -482,6 +482,7 @@ def get_as(self, name, type_to_return=types.string): out = DataTree(data_tree=obj, server=self._server) return out + @property def get_attribute_names(self): """ Returns a list of defined attribute names. @@ -507,6 +508,7 @@ def get_attribute_names(self): return coll_obj.get_integral_entries() + @property def get_sub_tree_names(self): """ Returns a list of defined sub-tree names. @@ -546,7 +548,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 ------- From 59cb50f4f80588e62b830fa5187757ff66e36d73 Mon Sep 17 00:00:00 2001 From: jhenriqu Date: Wed, 19 Jul 2023 11:53:10 +0200 Subject: [PATCH 2/5] fix test --- src/ansys/dpf/core/data_tree.py | 4 ++-- tests/test_data_tree.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ansys/dpf/core/data_tree.py b/src/ansys/dpf/core/data_tree.py index 80405421e81..3be2bc32676 100644 --- a/src/ansys/dpf/core/data_tree.py +++ b/src/ansys/dpf/core/data_tree.py @@ -537,10 +537,10 @@ def get_sub_tree_names(self): return coll_obj.get_integral_entries() def __to_dict(self, dic): - for attribute_name in self.get_attribute_names(): + for attribute_name in self.get_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.get_sub_tree_names: sub_tree = self.get_as(sub_tree_name, types.data_tree) sub_dic = {} sub_tree.__to_dict(sub_dic) diff --git a/tests/test_data_tree.py b/tests/test_data_tree.py index 3d4db940cfe..a1f8911373a 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.get_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.get_attribute_names + sub_trees = data_tree.get_sub_tree_names assert attributes == ["attribute01"] assert sub_trees == ["sub_tree01", "sub_tree02"] From c54df83188b0acca8d302b9dfa4b89cc37d15c0b Mon Sep 17 00:00:00 2001 From: jhenriqu Date: Wed, 19 Jul 2023 13:34:20 +0200 Subject: [PATCH 3/5] fix docstrings --- src/ansys/dpf/core/data_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/dpf/core/data_tree.py b/src/ansys/dpf/core/data_tree.py index 3be2bc32676..e84bf34854a 100644 --- a/src/ansys/dpf/core/data_tree.py +++ b/src/ansys/dpf/core/data_tree.py @@ -496,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.get_attribute_names ['id', 'name', 'qualities'] """ coll_obj = collection.StringCollection( @@ -524,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.get_sub_tree_names ['first', 'second'] """ coll_obj = collection.StringCollection( From 279c4b7bc431cc21694ced6ab2f06ff9d8280319 Mon Sep 17 00:00:00 2001 From: jhenriqu Date: Wed, 19 Jul 2023 18:05:57 +0200 Subject: [PATCH 4/5] add suggestions --- src/ansys/dpf/core/data_tree.py | 22 +++++++++++++++------- tests/test_data_tree.py | 16 +++++++++++++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/ansys/dpf/core/data_tree.py b/src/ansys/dpf/core/data_tree.py index e84bf34854a..faa5e4addac 100644 --- a/src/ansys/dpf/core/data_tree.py +++ b/src/ansys/dpf/core/data_tree.py @@ -483,7 +483,7 @@ def get_as(self, name, type_to_return=types.string): return out @property - def get_attribute_names(self): + def attribute_names(self): """ Returns a list of defined attribute names. @@ -496,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( @@ -509,7 +509,7 @@ def get_attribute_names(self): return coll_obj.get_integral_entries() @property - def get_sub_tree_names(self): + def sub_tree_names(self): """ Returns a list of defined sub-tree names. @@ -524,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( @@ -536,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) @@ -570,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 a1f8911373a..c05cb6d8da8 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,13 @@ 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" \ No newline at end of file From be67545abd40b2939f40438023c2d3ed6fb202c4 Mon Sep 17 00:00:00 2001 From: jhenriqu Date: Thu, 20 Jul 2023 09:37:01 +0200 Subject: [PATCH 5/5] fix style --- tests/test_data_tree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_data_tree.py b/tests/test_data_tree.py index c05cb6d8da8..8a30a8ab6af 100644 --- a/tests/test_data_tree.py +++ b/tests/test_data_tree.py @@ -375,6 +375,7 @@ def test_list_attributes_recursive_data_tree(server_type): 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" ) @@ -383,4 +384,4 @@ def test_attribute_errors_data_tree(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" \ No newline at end of file + data_tree.sub_tree_names = "hello"