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': '2025.1',
'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': False,
'toolkit_name': 'example',
'log_file': 'example_backend.log',
'state': '',
'progress': 0,
'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.
[5]:
{'aedt_version': '2025.1',
'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': False,
'toolkit_name': 'example',
'log_file': 'example_backend.log',
'state': '',
'progress': 0,
'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:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\Lib\site-packages\pydantic\main.py:998, in BaseModel.__setattr__(self, name, value)
996 # if None is returned from _setattr_handler, the attribute was set directly
997 elif (setattr_handler := self._setattr_handler(name, value)) is not None:
--> 998 setattr_handler(self, name, value) # call here to not memo on possibly unknown fields
999 self.__pydantic_setattr_handlers__[name] = setattr_handler
File C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\Lib\site-packages\pydantic\main.py:114, in <lambda>(model, name, val)
108 object.__setattr__(model, '__pydantic_private__', {})
109 model.__pydantic_private__[name] = val # pyright: ignore[reportOptionalSubscript]
112 _SIMPLE_SETATTR_HANDLERS: Mapping[str, Callable[[BaseModel, str, Any], None]] = {
113 'model_field': _model_field_setattr_handler,
--> 114 'validate_assignment': lambda model, name, val: model.__pydantic_validator__.validate_assignment(model, name, val), # pyright: ignore[reportAssignmentType]
115 'private': _private_setattr_handler,
116 'cached_property': lambda model, name, val: model.__dict__.__setitem__(name, val),
117 'extra_known': lambda model, name, val: _object_setattr(model, name, val),
118 }
121 class BaseModel(metaclass=_model_construction.ModelMetaclass):
122 """!!! abstract "Usage Documentation"
123 [Models](../concepts/models.md)
124
(...) 151 __pydantic_private__: Values of private attributes set on the model instance.
152 """
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.11/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.
[7]:
(True, 'Properties were updated successfully.')