Written by

Senior Software Engineer
Question Ashok Kumar T · Aug 4, 2024

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!

Product version: IRIS 2024.1
$ZV: IRIS for Windows (x86-64) 2024.1 (Build 267.2U) Tue Apr 30 2024 16:41:33 EDT

Comments

Ashok Kumar T  Aug 5, 2024 to Tommy Heyding

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)
0
Julius Kavay  Aug 5, 2024 to Ashok Kumar T

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
}
0