DataTree#

class ansys.dpf.core.data_tree.DataTree(data=None, data_tree=None, server=None)#

Represents an entity mapping attributes names to values.

Parameters:
  • data (dict(string:object)) – Dictionary attributes names to its associated data to add to the data tree.

  • data_tree (ctypes.c_void_p, ansys.grpc.dpf.data_tree_pb2.DataTree message, optional) –

  • server (DPFServer, optional) – Server with channel connected to the remote or local instance. The default is None, in which case an attempt is made to use the global server.

Examples

Create a data tree from a dictionary.

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree({"num_entities":3, "list_of_raws":[1,2,3,4], "name": "George"})
>>> data_tree.get_as("name", dpf.types.string)
'George'
>>> data_tree.get_as("list_of_raws", dpf.types.vec_int)
[1, 2, 3, 4]

Create a data tree with add.

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> data_tree.add(num_entities=3, list_of_raws=[1,2,3,4], name="George")
>>> txt = data_tree.write_to_txt()

Create a data tree with a context manager.

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> with data_tree.to_fill() as to_fill:
...     to_fill.num_entities = 3
...     to_fill.list_of_raws = [1,2,3,4]
>>> json = data_tree.write_to_json()

Notes

Class available with server’s version starting at 4.0 (Ansys 2022R2).

add(*args, **kwargs)#

Add attributes with their value to the data tree.

Parameters:
  • args (dict[string:object], optional) –

  • kwargs (int, float, string, list[int], list[double], list[str], DataTree) – Attributes names and their values to add.

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
to_fill()#

This method allows to access and modify the local copy of the data_tree without sending a request to the server. It should be used in a with statement so that the local data tree is released and the data is sent to the server in one action. If it is not used in a with statement, <release_data> DataTree.release_data() should be used to update the data tree.

Warning

If this <release_data> DataTree.to_fill() method is not used as a context manager in a with statement or if the method release_data() is not called, the data will not be updated.

Returns:

local_data_tree

Return type:

DataTree

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> with data_tree.to_fill() as to_fill:
...     to_fill.name = "George"
...     to_fill.qualities=["nice", "funny"]
...     to_fill.id = 3
write_to_txt(path=None)#

Writes the data tree either as a file or as returned string in a text format.

Parameters:

path (str, optional) – If a path is specified the output is written to this file.

Return type:

str

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
>>> txt = data_tree.write_to_txt()
>>> import tempfile
>>> import os
>>> data_tree.write_to_txt(os.path.join(tempfile.mkdtemp(), "data_tree.txt"))

...
write_to_json(path=None)#

Writes the data tree either as a file or as returned string in a json format.

Parameters:

path (str, optional) – If a path is specified the output is written to this file.

Return type:

str

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
>>> txt = data_tree.write_to_json()
>>> import tempfile
>>> import os
>>> data_tree.write_to_json(os.path.join(tempfile.mkdtemp(), "data_tree.json"))

...
static read_from_json(path=None, txt=None, server=None)#

Convert a json string or file to DataTree

Parameters:
  • path (str, optional) – If a path is specified the output is read from this file.

  • txt (str, optional) – If a txt is specified the output is read from this string.

  • server (DPFServer, optional) – Server with the channel connected to the remote or local instance. The default is None, in which case an attempt is made to use the global server.

Return type:

DataTree

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree({"num_entities":3, "list_of_raws":[1,2,3,4], "name": "George"})
>>> txt = data_tree.write_to_json()
>>> data_tree_copy = dpf.DataTree.read_from_json(txt=txt)
static read_from_txt(path=None, txt=None, server=None)#

Convert a text string or file to DataTree

Parameters:
  • path (str, optional) – If a path is specified the output is read from this file.

  • txt (str, optional) – If a txt is specified the output is read from this string.

  • server (DPFServer, optional) – Server with the channel connected to the remote or local instance. The default is None, in which case an attempt is made to use the global server.

Return type:

DataTree

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree({"num_entities":3, "list_of_raws":[1,2,3,4], "name": "George"})
>>> txt = data_tree.write_to_txt()
>>> data_tree_copy = dpf.DataTree.read_from_txt(txt=txt)
has(entry)#

Return True if the entry exists

Parameters:

entry (str) –

Return type:

bool

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
>>> data_tree.has("qualities")
True
>>> data_tree.has("flaws")
False
get_as(name, type_to_return=types.string)#

Returns an attribute value by its name in the required type.

Parameters:
  • name (str) – Name of the attribute to return

  • type_to_return (types, type (str, int, float...)) – Type of the attribute to return. String is supported for all attributes.

Return type:

str, int, float, list[int], list[float], list[str], DataTree

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
>>> data_tree.get_as("id")
'3'
>>> data_tree.get_as("id", dpf.types.int)
3
>>> data_tree.get_as("qualities", dpf.types.vec_string)
['nice', 'funny']
property attribute_names#

Returns a list of defined attribute names.

Return type:

list[str]

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
>>> data_tree.attribute_names
['id', 'name', 'qualities']
property sub_tree_names#

Returns a list of defined sub-tree names.

Return type:

list[str]

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> first_subtree = dpf.DataTree()
>>> second_subtree = dpf.DataTree()
>>> data_tree.add(first=first_subtree, second=second_subtree)
>>> data_tree.sub_tree_names
['first', 'second']
to_dict()#

Returns a read-only dictionary representation of the DataTree.

Return type:

dict

Examples

>>> from ansys.dpf import core as dpf
>>> data_tree = dpf.DataTree()
>>> sub = dpf.DataTree()
>>> sub.add(str="hello world")
>>> data_tree.add(id=3, sub_tree=sub)
>>> data_tree.to_dict()
{'id': '3', 'sub_tree': {'str': 'hello world'}}