Skip to content

Commit 99e8b4f

Browse files
add data-tree list-attributes (#1025)
* add data-tree list-attributes * fix style * fix style * add test skip for older versions * add to_dict() & docstring examples * add newline * fix style * fix gdc test * fix style check * rename test
1 parent 01e38f9 commit 99e8b4f

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/ansys/dpf/core/data_tree.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,91 @@ def get_as(self, name, type_to_return=types.string):
482482
out = DataTree(data_tree=obj, server=self._server)
483483
return out
484484

485+
def get_attribute_names(self):
486+
"""
487+
Returns a list of defined attribute names.
488+
489+
Returns
490+
-------
491+
list[str]
492+
493+
Examples
494+
--------
495+
>>> from ansys.dpf import core as dpf
496+
>>> data_tree = dpf.DataTree()
497+
>>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
498+
>>> data_tree.get_attribute_names()
499+
['id', 'name', 'qualities']
500+
"""
501+
coll_obj = collection.StringCollection(
502+
collection=self._api.dpf_data_tree_get_available_attributes_names_in_string_collection(
503+
self
504+
),
505+
server=self._server,
506+
)
507+
508+
return coll_obj.get_integral_entries()
509+
510+
def get_sub_tree_names(self):
511+
"""
512+
Returns a list of defined sub-tree names.
513+
514+
Returns
515+
-------
516+
list[str]
517+
518+
Examples
519+
--------
520+
>>> from ansys.dpf import core as dpf
521+
>>> data_tree = dpf.DataTree()
522+
>>> first_subtree = dpf.DataTree()
523+
>>> second_subtree = dpf.DataTree()
524+
>>> data_tree.add(first=first_subtree, second=second_subtree)
525+
>>> data_tree.get_sub_tree_names()
526+
['first', 'second']
527+
"""
528+
coll_obj = collection.StringCollection(
529+
collection=self._api.dpf_data_tree_get_available_sub_tree_names_in_string_collection(
530+
self
531+
),
532+
server=self._server,
533+
)
534+
535+
return coll_obj.get_integral_entries()
536+
537+
def __to_dict(self, dic):
538+
for attribute_name in self.get_attribute_names():
539+
dic[attribute_name] = self.get_as(attribute_name)
540+
541+
for sub_tree_name in self.get_sub_tree_names():
542+
sub_tree = self.get_as(sub_tree_name, types.data_tree)
543+
sub_dic = {}
544+
sub_tree.__to_dict(sub_dic)
545+
dic[sub_tree_name] = sub_dic
546+
547+
def to_dict(self):
548+
"""
549+
Returns a dictionary representation of the DataTree
550+
551+
Returns
552+
-------
553+
dict
554+
555+
Examples
556+
--------
557+
>>> from ansys.dpf import core as dpf
558+
>>> data_tree = dpf.DataTree()
559+
>>> sub = dpf.DataTree()
560+
>>> sub.add(str="hello world")
561+
>>> data_tree.add(id=3, sub_tree=sub)
562+
>>> data_tree.to_dict()
563+
{'id': '3', 'sub_tree': {'str': 'hello world'}}
564+
"""
565+
dic = {}
566+
self.__to_dict(dic)
567+
568+
return dic
569+
485570
def __setattr__(self, key, value):
486571
if key == "_common_keys" or key in self._common_keys:
487572
return super.__setattr__(self, key, value)

tests/test_data_tree.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,48 @@ def test_unsupported_types_data_tree(server_type):
329329
data_tree.add(data1=[[1]])
330330
with pytest.raises(TypeError):
331331
data_tree.add(data1=(1, 2))
332+
333+
334+
@pytest.mark.skipif(
335+
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
336+
)
337+
def test_list_attributes_data_tree(server_type):
338+
data_tree = dpf.DataTree(server=server_type)
339+
with data_tree.to_fill() as to_fill:
340+
to_fill.int = 1
341+
to_fill.double = 1.0
342+
to_fill.string = "hello"
343+
to_fill.list_int = [1, 2]
344+
to_fill.list_double = [1.5, 2.5]
345+
to_fill.add(list_string=["hello", "bye"])
346+
347+
attributes = data_tree.get_attribute_names()
348+
349+
assert ["double", "int", "list_double", "list_int", "list_string", "string"] == attributes
350+
351+
352+
@pytest.mark.skipif(
353+
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, reason="Available for servers >=7.0"
354+
)
355+
def test_list_attributes_recursive_data_tree(server_type):
356+
data_tree = dpf.DataTree(server=server_type)
357+
with data_tree.to_fill() as to_fill:
358+
to_fill.attribute01 = 1
359+
sub_tree01 = dpf.DataTree(server=server_type)
360+
with sub_tree01.to_fill() as to_fill01:
361+
to_fill01.attribute02 = 2
362+
to_fill.sub_tree01 = sub_tree01
363+
sub_tree02 = dpf.DataTree(server=server_type)
364+
to_fill.sub_tree02 = sub_tree02
365+
366+
attributes = data_tree.get_attribute_names()
367+
sub_trees = data_tree.get_sub_tree_names()
368+
369+
assert attributes == ["attribute01"]
370+
assert sub_trees == ["sub_tree01", "sub_tree02"]
371+
372+
dic = data_tree.to_dict()
373+
374+
assert ["attribute01", "sub_tree01", "sub_tree02"] == list(dic.keys())
375+
assert {"attribute02": "2"} == dic["sub_tree01"]
376+
assert {} == dic["sub_tree02"]

0 commit comments

Comments
 (0)