What is the Synonym of $property for Embedded Python?
Hi folks!
I have a need to use symilar to $property function from Python. Here is the code:
obj=iris.cls('some.persistent.class')._New()
for property, value in data.items():
$property(obj.property)=value ; I invented this
PythonPython
How could I do this? Any trick?
For now I plan to implement a helper function in iris that I will call, but I doubt also how can I transfer oref to IRIS.
Thoughts?
Hi Evgeny,
Not saying this is best way but this indirection can be achieved with python eval. For example:
$Classmethod equivalent
$Property equivalent
Instantiating a Python exception and then iterate over properties printing out name and value:
output was:
Thank you, @Alex Woodhead ! This works for me!
Another solution is to use getattr
classname="%SYSTEM.SYS" methodname="ProcessID" pid = getattr(iris.cls(classname), 'ProcessID')() myerror=iris.cls("%Exception.PythonException")._New("MyOops",123,"def+123^XYZ","SomeData") for propertyname in ["Name","Code","Data","Location"]: print(getattr(myerror, propertyname))
And for completeness: setting a property with a name not known until runtime can be done with the counterpart of getattr, setattr:
USER>do ##class(%SYS.Python).Shell()
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type quit() or Ctrl-D to exit this shell.
>>> e=iris.cls("%Exception.PythonException")._New("MyOops",123,"def+123^XYZ","SomeData")
>>> e.Name
'MyOops'
>>> setattr(e,'Name','TheirOops')
>>> e.Name
'TheirOops'
Thank you both @Dmitry Maslennikov @Gertjan Klein !
This is what I'm looking for!