Como percorrer uma estrutura de dados global do IRIS a partir do Python usando o último SDK Nativo do IRIS para Python
O InterSystems IRIS 2022.2 tem um SDK Nativo para Python (https://docs.intersystems.com/iris20222/csp/docbook/Doc.View.cls?KEY=PAGE_python_native).
Sabemos como percorrer uma estrutura de dados global usando a função $Order do ObjectScript do IRIS.
SET key=""FOR {
SET key=$ORDER(^myglobal(key))
QUIT:key=""WRITE !,^myglobal(key)
}Como fazer o mesmo no Python usando o SDK Nativo do IRIS para Python? Aqui está um exemplo de código:
import iris
args = {'hostname':'127.0.0.1', 'port':51772,
'namespace':'USER', 'username':'_SYSTEM', 'password':'SYS'
}
conn = iris.connect(**args)
# Create an iris object
irispy = iris.createIRIS(conn)
# Create a global array in the USER namespace on the server
irispy.set('A', 'root', 'foo', 'SubFoo')
irispy.set(123, 'root', 'bar', 'lowbar', 'UnderBar')
irispy.set(124, 'root', 'bar', 'lowbar', 'UnderBar2')
irispy.set("hi", 'root', 'bar', 'lowbar')
irispy.set("hi again", 'root', 'bar3')
# Read the values from the database and print them
subfoo_value = irispy.get('root', 'foo', 'SubFoo')
underbar_value = irispy.get('root', 'bar', 'lowbar', 'UnderBar')
underbar2_value = irispy.get('root', 'bar', 'lowbar', 'UnderBar2')
lowbar_value = irispy.get('root', 'bar', 'lowbar')
bar3_value = irispy.get('root', 'bar3')
print('Created two values: ')
print(' root("foo","SubFoo")=', subfoo_value)
print(' root("bar","lowbar","UnderBar")=', underbar_value)
print(' root("bar","lowbar","UnderBar2")=', underbar2_value)
print(' root("bar","lowbar")=', lowbar_value)
print(' root("bar3")=', bar3_value)
direction = 0# direction of iteration (boolean forward/reverse)
next_sub = chr(0) # start at first possible subscript
subs = []
print("\n Iterating root \n")
isDef = irispy.isDefined('root', *subs)
while isDef:
next_sub = irispy.nextSubscript(False, 'root', *subs, next_sub) # get first subscriptif next_sub == None: # we finished iterating nodes on this tree branch, move a level upif len(subs) == 0: # no more things to iteratebreak
next_sub = subs.pop(-1) # pop last subscript in order to continue iterating this levelif irispy.isDefined('root', *subs, next_sub) == 11:
print('root(',*subs, next_sub, ')=',irispy.get('root', *subs, next_sub))
continuecontinue
isDef = irispy.isDefined('root', *subs, next_sub)
if isDef in [10, 11]: # keep building subscripts for depth first search
subs.append(next_sub)
next_sub = chr(0)
continueelif isDef == 1: # reached a leaf node, print it
print('root(',*subs, next_sub, ')=',irispy.get('root', *subs, next_sub))
else: # def 0 is not really expected
print("error")
irispy.kill('root')
conn.close()
exit(-1)
# Delete the global array and terminate
irispy.kill('root') # delete global array root
conn.close()Discussão (0)0