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).
Overview#
Add attributes with their value to the data tree. |
|
Use with a with statement to modify local data_tree and sync with the server in one action. |
|
Write the data tree either as a file or as returned string in a text format. |
|
Write the data tree either as a file or as returned string in a json format. |
|
Return True if the entry exists. |
|
Return an attribute value by its name in the required type. |
|
Return a read-only dictionary representation of the DataTree. |
Returns a list of defined attribute names. |
|
Returns a list of defined sub-tree names. |
Convert a json string or file to DataTree. |
|
Convert a text string or file to DataTree. |
Set an attribute for the DataTree object. |
|
Describe the entity. |
|
Delete this instance. |
Import detail#
from ansys.dpf.core.data_tree import DataTree
Property detail#
- property DataTree.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 DataTree.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']
Method detail#
- DataTree.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")
- DataTree.to_fill()#
Use with a with statement to modify local data_tree and sync with the server in one action.
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 awith
statement,
should be used to update the data tree.DataTree.release_data() Warning
If this
method is not used as a context manager in aDataTree.to_fill() with
statement or if the method release_data() is not called, the data will not be updated.- Returns:
local_data_tree
- Return type:
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
- DataTree.write_to_txt(path=None)#
Write 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")) ...
- DataTree.write_to_json(path=None)#
Write 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 DataTree.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:
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 DataTree.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:
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)
- DataTree.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
- DataTree.get_as(name, type_to_return=types.string)#
Return 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']
- DataTree.to_dict()#
Return 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'}}
- DataTree.__setattr__(key, value)#
Set an attribute for the DataTree object.
- Parameters:
key (str) – The name of the attribute to set. If the attribute is a reserved key (e.g., internal attributes starting with “_common_keys” or attributes defined in the class), it is set using the parent class’s __setattr__ method. Otherwise, it adds the attribute and its value to the data tree.
value (object) – The value of the attribute to set.
- DataTree.__str__()#
Describe the entity.
- Returns:
Description of the entity.
- Return type:
str
- DataTree.__del__()#
Delete this instance.