Skip to content
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
26 changes: 18 additions & 8 deletions src/ansys/dpf/core/data_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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(
Expand All @@ -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.

Expand All @@ -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(
Expand All @@ -534,19 +536,27 @@ 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)
dic[sub_tree_name] = sub_dic

def to_dict(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def to_dict(self):
@property
def dict(self):

You can make this a property
You could also define the __dict__ dunder method so that dict(your_object) works

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'd personally integrate __to_dict in the dict property, and then call directly the dict property in other parts of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PProfizi Will do! Wasn't aware of this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PProfizi Seems dict is meant for something else (e.g. object inspection).

"""
Returns a dictionary representation of the DataTree
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you enforce the fact it is read-only if you return an actual Python dict?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was originally a comment from @cbellot000 meaning that modifying the attributes & attribute-names in the dict didn't actually modify anything the DataTree. Which on second thought, the fact that we return a dict makes it clear enough. But no harm in leaving there I think.

Returns a read-only dictionary representation of the DataTree.

Returns
-------
Expand All @@ -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})

Expand Down
17 changes: 14 additions & 3 deletions tests/test_data_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"]
Expand All @@ -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"