Question
· Oct 8, 2022

How to Print Out an Arbitrary Global via Embedded Python ?

Hi, devs!

Consider you have an arbitrary global with an unknown amount of indexes.

How to print all the values to the terminal with Embedded Python?

With IRIS I'd do:

USER>ZWrite ^Global

Or with the following code:

ClassMethod PrintGlobal(gname) As %Status
{

    
    set global=$Name(@gname)

    set next=$Query(@global@(""))

    for {
    quit:next=""
    write !,next,@next

    set next=$Query(@next)

}

with embedded python if I know that global is 2-dimensional I can do:

ClassMethod PrintGlobal() As %Status [ Language = python ]
{
i=0
j=0
    for (key,value) in bs.query([i,j]):
        print(key,value)
}

But if I don't know the depth of indexes?

Thoughts?

Product version: IRIS 2022.1
Discussion (3)1
Log in or sign up to continue

query is the way to go :

import iris

g = iris.gref("^MyGlobal")

# Insert some data
g[1] = "my first line"
g[2] = "my second line"
g[1,"a"] = "my first line with a key"
g[1,"b"] = "my first line with a key"
g[2,"a"] = "my second line with a key"
g[2,"b"] = "my second line with a key"
g[3,"a",1] = "my third line with a key and a subkey"

for (key, value) in g.query():
    print(f"{key} -> {value}")

result :

['1'] -> my first line
['1', 'a'] -> my first line with a key
['1', 'b'] -> my first line with a key
['2'] -> my second line
['2', 'a'] -> my second line with a key
['2', 'b'] -> my second line with a key
['3'] -> None
['3', 'a'] -> None
['3', 'a', '1'] -> my third line with a key and a subkey