-
Notifications
You must be signed in to change notification settings - Fork 23
Update DataTree #1042
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
Update DataTree #1042
Changes from all commits
183f440
59cb50f
c54df83
279c4b7
be67545
ca97465
af833f3
640cbad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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): | ||
| """ | ||
| Returns a dictionary representation of the DataTree | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ------- | ||
|
|
@@ -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}) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can make this a property
You could also define the
__dict__dunder method so thatdict(your_object)worksThere was a problem hiding this comment.
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_dictin thedictproperty, and then call directly thedictproperty in other parts of the code.There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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).