Convert IRIS DynamicArray to Python list
Hello Community,
How to convert the IRIS %DynamicArray into python list. I got "
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)
}
ObjectScriptObjectScript
Thanks!
Product version: IRIS 2024.1
$ZV: IRIS for Windows (x86-64) 2024.1 (Build 267.2U) Tue Apr 30 2024 16:41:33 EDT
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.cls...
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 }