run_python_script#
- Mechanical.run_python_script(script_block, enable_logging=False, log_level='WARNING', progress_interval=2000)#
Run a Python script block inside Mechanical.
It returns the string value of the last executed statement. If the value cannot be returned as a string, it will return an empty string.
- Parameters:
- script_block
str
Script block (one or more lines) to run.
- enable_logging: bool, optional
Whether to enable logging. The default is
False
.- log_level: str
Level of logging. The default is
"WARNING"
. Options are"DEBUG"
,"INFO"
,"WARNING"
, and"ERROR"
.- progress_interval: int, optional
Frequency in milliseconds for getting log messages from the server. The default is
2000
.
- script_block
- Returns:
str
Script result.
Examples
Return a value from a simple calculation.
>>> mechanical.run_python_script('2+3') '5'
Return a string value from Project object.
>>> mechanical.run_python_script('ExtAPI.DataModel.Project.ProductVersion') '2023 R1'
Return an empty string, when you try to return the Project object.
>>> mechanical.run_python_script('ExtAPI.DataModel.Project') ''
Return an empty string for assignments.
>>> mechanical.run_python_script('version = ExtAPI.DataModel.Project.ProductVersion') ''
Return value from the last executed statement from a variable.
>>> script=''' addition = 2 + 3 multiplication = 3 * 4 multiplication ''' >>> mechanical.run_python_script(script) '12'
Return value from last executed statement from a function call.
>>> script=''' import math math.pow(2,3) ''' >>> mechanical.run_python_script(script) '8'
Handle an error scenario.
>>> script = 'hello_world()' >>> import grpc >>> try: mechanical.run_python_script(script) except grpc.RpcError as error: print(error.details()) name 'hello_world' is not defined