Convert IRIS DynamicArray to Python list
Hello Community,
How to convert the IRIS %DynamicArray into python list. I got "<THROW>TestDyncArray+1^MyLearn.Pyth.NewClass1.1 *%Exception.PythonException <PYTHON EXCEPTION> 246 <class 'AttributeError'>: <unknown exception data>" error when passing array values to python class.
ClassMethod TestDyncArray()
{
Do..DyncArrayInPy([1,2,3,4])
}
ClassMethod DyncArrayInPy(numbers) [ Language = python ]
{
import iris
print(type(numbers)) ;<class 'iris.%Library.DynamicArray'> need to be a <class 'list'>for num in numbers:
print(num)
}Thanks!
Comments
Hi,
you can use this method to convert an ObjectScript $LIST to a Python list. I'm not sure if this works with %DynamicArray. If not you could try to convert the %DynamicArray to a $LIST first.
set plist = ##class(%SYS.Python).ToList(clist)https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…
Hello @Tommy.Heyding
Thank you for pointing ToList in %SYS.Python It works for $LIST only not for the dynamicarray. Yes I believe the same convert the dynamic array to $LIST and send to the python. I grab the sample from doc
set clist = $lb(123, 456.789, "hello world")
set plist = ##class(%SYS.Python).ToList(clist)First convert the dynamic array to a Cache List and then the Cache List to Python List - voila the job is done
/// Convert a dynamic array to a Cache List/// ClassMethod ArrayToList(arr)
{
q:'arr.%Size() $lb()
s list="", it=arr.%GetIterator()
while it,it.%GetNext(.key,.val) {
s typ=arr.%GetTypeOf(key)
s list=list_$case(typ,"object":$lb(val.%ToJSON()),"array":$lb(..ArrayToList(val)),"null":$lb(),:$lb(val))
}
q list
}