Properties example#

This example shows how to use the Common class, which contains properties models. These properties provide for sharing information through all the workflow.

Add new properties#

Before importing the common module, you can add new properties. First create a file that contains the new properties type, Models. Then add a TOML file that sets the needed default values. Finally, import the properties.

[1]:
from models import properties

Perform required imports#

Perform the required imports.

[2]:
import sys
from ansys.aedt.toolkits.common.backend.api import Common

Initialize toolkit#

Initialize the toolkit with the new properties.

[3]:
toolkit = Common(properties)

Get properties#

Get the properties.

[4]:
toolkit.get_properties()
[4]:
{'aedt_version': '2024.2',
 'non_graphical': False,
 'active_project': '',
 'active_design': '',
 'project_list': [],
 'design_list': {},
 'selected_process': 0,
 'use_grpc': True,
 'is_toolkit_busy': False,
 'url': '127.0.0.1',
 'port': 5001,
 'debug': True,
 'toolkit_name': 'example',
 'log_file': 'example_backend.log',
 'example': {'invented_property': [10], 'test': 'hola'}}

Set property#

Use set_properties to set the new property.

[5]:
set_properties = {"invented_property": [1, 2, 3]}
toolkit.set_properties(set_properties)
toolkit.get_properties()
INFO - Updating internal properties.
DEBUG - Updating 'invented_property' with value [1, 2, 3]
DEBUG - Properties were updated successfully.
[5]:
{'aedt_version': '2024.2',
 'non_graphical': False,
 'active_project': '',
 'active_design': '',
 'project_list': [],
 'design_list': {},
 'selected_process': 0,
 'use_grpc': True,
 'is_toolkit_busy': False,
 'url': '127.0.0.1',
 'port': 5001,
 'debug': True,
 'toolkit_name': 'example',
 'log_file': 'example_backend.log',
 'example': {'invented_property': [1, 2, 3], 'test': 'hola'}}

Set property directly#

Set the property directly.

[6]:
properties.invented_property = [10, 20, 30]
toolkit.get_properties()
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[6], line 1
----> 1 properties.invented_property = [10, 20, 30]
      2 toolkit.get_properties()

File C:\Users\Public\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\pydantic\main.py:881, in BaseModel.__setattr__(self, name, value)
    879     attr.__set__(self, value)
    880 elif self.model_config.get('validate_assignment', None):
--> 881     self.__pydantic_validator__.validate_assignment(self, name, value)
    882 elif self.model_config.get('extra') != 'allow' and name not in self.model_fields:
    883     # TODO - matching error
    884     raise ValueError(f'"{self.__class__.__name__}" object has no field "{name}"')

ValidationError: 1 validation error for Properties
invented_property
  Object has no attribute 'invented_property' [type=no_such_attribute, input_value=[10, 20, 30], input_type=list]
    For further information visit https://errors.pydantic.dev/2.9/v/no_such_attribute

Set wrong property#

Set the wrong property. It is not possible to change the property type.

[7]:
set_properties = {"invented_property": 1}
toolkit.set_properties(set_properties)
INFO - Updating internal properties.
DEBUG - Updating 'invented_property' with value 1
DEBUG - Properties were updated successfully.
[7]:
(True, 'Properties were updated successfully.')