Skip to main content

Accessing Global Arrays with Python

This chapter covers the following topics:

Creating a Database Connection in Python

The examples in this chapter assume that an iris object named irispy already exists and is connected to the server. The following code was used to create and connect it:

  import iris
  conn = iris.connect('', 51773, 'USER', '_SYSTEM', 'SYS')
  irispy = iris.createIRIS(conn)

For more information, see the Quick Reference entries for iris package functions connect() and createIRIS().

Introduction to Global Arrays

A global array, like all sparse arrays, is a tree structure rather than a sequential list. The basic concept behind global arrays can be illustrated by analogy to a file structure. Each directory in the tree is uniquely identified by a path composed of a root directory identifier followed by a series of subdirectory identifiers, and any directory may or may not contain data.

Global arrays work the same way: each node in the tree is uniquely identified by a node address composed of a global name identifier and a series of subscript identifiers, and a node may or may not contain a value. For example, here is a global array consisting of six nodes, two of which contain values:

   root -->|--> foo --> SubFoo='A'
           |--> bar --> lowbar --> UnderBar=123

Values could be stored in the other possible node addresses (for example, root or root->bar), but no resources are wasted if those node addresses are valueless. In InterSystems ObjectScript globals notation, the two nodes with values would be:


The global name (root) is followed by a comma-delimited subscript list in parentheses. Together, they specify the entire path to the node.

This global array could be created by two calls to the Native SDK set() method:

  irisObject.set('A', 'root', 'foo', 'SubFoo')
  irisObject.set(123, 'root', 'bar', 'lowbar', 'UnderBar')

Global array root is created when the first call assigns value 'A' to node root('foo','SubFoo'). Nodes can be created in any order, and with any set of subscripts. The same global array would be created if we reversed the order of these two calls. The valueless nodes are created automatically, and will be deleted automatically when no longer needed. For details, see “Creating, Updating, and Deleting Nodes” in the next chapter.

The Native SDK code to create this array is demonstrated in the following example. A connection object establishes a connection to the server. The connection will be used by an instance of class iris named irispy. Native SDK methods are used to create a global array, read the resulting persistent values from the database, and then delete the global array.

The NativeDemo Program
# Import the Native SDK module
import iris

# Open a connection to the server
args = {'hostname':'', 'port':52773,
    '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')

# Read the values from the database and print them
subfoo = irispy.get('root', 'foo', 'SubFoo')
underbar = irispy.get('root', 'bar', 'lowbar', 'UnderBar')
print('Created two values: ')
print('   root("foo","SubFoo")=', subfoo)
print('   root("bar","lowbar","UnderBar")=', underbar)

# Delete the global array and terminate
irispy.kill('root') # delete global array root

NativeDemo prints the following lines:

  Created two values:

In this example, iris is the Native SDK iris package, and irispy is an instance of iris.IRIS. Native SDK methods perform the following actions:

  • iris.connect() creates a connection object named conn, connected to the database associated with the USER namespace.

  • iris.createIRIS() creates a new instance of iris.IRIS named irispy, which will access the database through server connection conn.

  • iris.IRIS.set() creates new persistent nodes in database namespace USER.

  • iris.IRIS.get() returns the values of the specified nodes.

  • iris.IRIS.kill() deletes the specified root node and all of its subnodes from the database.

  • connection.close() closes the connection.

The next section provides detailed explanations and examples for all of these methods.

Glossary of Native SDK Terms

See the previous section for an overview of the concepts listed here. Examples in this glossary will refer to the global array structure listed below. The Legs global array has ten nodes and three node levels. Seven of the ten nodes contain values:

  Legs                       // root node, valueless, 3 child nodes
    fish = 0                 // level 1 node, value=0
    mammal                   // level 1 node, valueless
      human = 2              // level 2 node, value=2
      dog = 4                // level 2 node, value=4
    bug                      // level 1 node, valueless, 3 child nodes
      insect = 6             // level 2 node, value=6
      spider = 8             // level 2 node, value=8
      millipede = Diplopoda  // level 2 node, value="Diplopoda", 1 child node
        centipede = 100      // level 3 node, value=100
Child node

The nodes immediately under a given parent node. The address of a child node is specified by adding exactly one subscript to the end of the parent subscript list. For example, parent node Legs('mammal') has child nodes Legs('mammal','human') and Legs('mammal','dog').

Global name

The identifier for the root node is also the name of the entire global array. For example, root node identifier Legs is the global name of global array Legs. Unlike subscripts, global names can only consist of letters, numbers, and periods (see Global Naming Rules).


An element of a global array, uniquely identified by a namespace consisting of a global name and an arbitrary number of subscript identifiers. A node must either contain data, have child nodes, or both.

Node level

The number of subscripts in the node address. A ‘level 2 node’ is just another way of saying ‘a node with two subscripts’. For example, Legs('mammal','dog') is a level 2 node. It is two levels under root node Legs and one level under Legs('mammal').

Node address

The complete namespace of a node, including the global name and all subscripts. For example, node address Legs('fish') consists of root node identifier Legs plus a list containing one subscript, 'fish'. Depending on context, Legs (with no subscript list) can refer to either the root node address or the entire global array.

Root node

The unsubscripted node at the base of the global array tree. The identifier for a root node is its global name with no subscripts.


All descendants of a given node are referred to as subnodes of that node. For example, node Legs('bug') has four different subnodes on two levels. All nine subscripted nodes are subnodes of root node Legs.

Subscript / Subscript list

All nodes under the root node are addressed by specifying the global name and a list of one or more subscript identifiers. (The global name plus the subscript list is the node address).

Target address

Many Native SDK methods require you to specify a valid node address that does not necessarily point to an existing node. For example, the set() method takes a value argument and a target address, and stores the value at that address. If no node exists at the target address, a new node is created.


A node can contain a value of any supported type (bool, bytes, float, integer, string, or None). A node with no child nodes must contain a value; a node that has child nodes can be valueless.

Valueless node

A node must either contain data, have child nodes, or both. A node that has child nodes but does not contain data is called a valueless node. Valueless nodes only exist as pointers to lower level nodes.

Global Naming Rules

Global names and subscripts obey the following rules:

  • The length of a node address (totaling the length of the global name and all subscripts) can be up to 511 characters. (Some typed characters may count as more than one encoded character for this limit. For more information, see “Maximum Length of a Global Reference” in Using Globals).

  • A global name can include letters, numbers, and periods ('.'), and can have a length of up to 31 significant characters. It must begin with a letter, and must not end with a period.

  • A subscript can be a Unicode string, Integer/Long, Float, or Byte string. String subscripts are case-sensitive, and can include characters of all types. Length is limited only by the 511 character maximum for the total node address.

Creating, Accessing, and Deleting Nodes

This section demonstrates how to use the the set() and kill() methods to create, update, and delete nodes.

Setting and changing node values

iris.IRIS.set() takes a value argument and stores the value at the specified address.

If no node exists at that address, a new one is created.

The set() method can assign values of any supported datatype. In the following example, the first call to set() creates a new node at subnode address myGlobal('A') and sets the value of the node to string 'first'. The second call changes the value of the subnode, replacing it with integer 1.

  irispy.set('first','myGlobal','A')  # create node myGlobal('A') = 'first'
  irispy.set(1,'myGlobal','A')        # change value of myGlobal('A') to 1.

A global name can only include letters, numbers, and periods ('.'). The name must begin with a character and may not end with a period. A subscript string is case-sensitive and can include non-alphanumeric characters. See “Global Naming Rules” for more information.

Deleting a node or group of nodes

iris.IRIS.kill() — deletes the specified node and all of its subnodes. The entire global array will be deleted if the root node is deleted or if all nodes with values are deleted.

Global array myGlobal initially contains the following nodes:

   myGlobal = <valueless node>
     myGlobal('A') = 0
       myGlobal('A',1) = 0
       myGlobal('A',2) = 0
     myGlobal('B') = <valueless node>
       myGlobal('B',1) = 0

This example will delete the global array by calling kill() on two of its subnodes. The first call will delete node myGlobal('A') and both of its subnodes:

  irispy.kill('myGlobal','A')    # also kills myGlobal('A',1) and myGlobal('A',2)

The second call deletes the last remaining subnode with a value, killing the entire global array:

  irispy.kill('myGlobal','B',1)  # deletes last value in global array myGlobal
  • The parent node, myGlobal('B'), is deleted because it is valueless and now has no subnodes.

  • Root node myGlobal is valueless and now has no subnodes, so the entire global array is deleted from the database.

Finding Nodes in a Global Array

The Native SDK provides ways to iterate over part or all of a global array. The following topics describe the various iteration methods:

Iterating Over a Set of Child Nodes

Child nodes are sets of nodes immediately under the same parent node. Any child node address can be defined by appending one subscript to the subscript list of the parent. For example, the following global array has four child nodes under parent node heroes('dogs'):

The heroes global array

This global array uses the names of several heroic dogs (plus a reckless boy and a pioneering sheep) as subscripts. The values are birth years.

  heroes                                           // root node,    valueless, 2 child nodes
     heroes('dogs')                                // level 1 node, valueless, 4 child nodes
        heroes('dogs','Balto') = 1919              // level 2 node, value=1919
        heroes('dogs','Hachiko') = 1923            // level 2 node, value=1923
        heroes('dogs','Lassie') = 1940             // level 2 node, value=1940, 1 child node
           heroes('dogs','Lassie','Timmy') = 1954  // level 3 node, value=1954
        heroes('dogs','Whitefang') = 1906          // level 2 node, value=1906
     heroes('sheep')                               // level 2 node, valueless, 1 child node
        heroes('sheep','Dolly') = 1996             // level 2 node, value=1996

The following methods are used to create an iterator, define the direction of iteration, and set the starting point of the search:

  • IRIS.iterator() returns an instance of Iterator for the child nodes of the specified target node.

  • Iterator.reversed() toggles direction of iteration between forward and reverse collation order.

  • Iterator.startFrom() sets the iterator's starting position to the specified subscript. The subscript is an arbitrary starting point, and does not have to address an existing node.

Read child node values in reverse order

The following code iterates over child nodes of heroes('dogs') in reverse collation order, starting with subscript V:

# Iterate in reverse, seeking nodes lower than heroes('dogs','V') in collation order
  iterDogs = irispy.iterator('heroes','dogs').reversed().startFrom('V')
  output = '\nDog birth years: '
  for key,value in iterDogs.items():
    output +=  '%s:%i  ' % (key, value)

This code prints the following output:

  Dog birth years: Lassie:1940  Hachiko:1923  Balto:1919

In this example, two subnodes of heroes('dogs') are ignored:

  • Child node heroes('dogs','Whitefang') will not be found because it is outside of the search range (Whitefang is higher than V in collation order).

  • Level 3 node heroes('dogs','Lassie','Timmy') will not be found because it is a child of Lassie, not dogs.

See the last section in this chapter (“Testing for Child Nodes and Node Values”) for a discussion of how to iterate over multiple node levels.

Collation Order

The order in which nodes are retrieved depends on the collation order of the subscripts. This is not a function of the iterator. When a node is created, it is automatically stored it in the collation order specified by the storage definition. In this example, the child nodes of heroes('dogs') would be stored in the order shown (Balto, Hachiko, Lassie, Whitefang) regardless of the order in which they were created. For more information, see “Collation of Global Nodes” in Using Globals.

Iteration with next()

The Native SDK also supports the next() method and return type iterator methods:

  • — positions the iterator at the next child node (if one exists) and returns a tuple containing the subscript and value of the next node in the iteration. The subscript is the first element of the tuple and the value is the second.

  • Iterator.items() — sets return type to an array containing both the subscript and the value of the child node. For example, the returned value for node heroes(,'dogs','Balto') would be ['Balto',1919].

  • Iterator.subscripts() — sets return type to return only the subscript.

  • Iterator.values() — sets return type to return only the node value.

In the following example, iterDogs is set to iterate over child nodes of heroes(,'dogs'). Since the subscripts() method is called when the iterator is created, each call to next() will return only the subscript for the current child node. Each subscript is appended to the output variable, and the entire list will be printed when the loop terminates. A StopIteration exception is thrown when there are no more child nodes in the sequence.

Use next() to list the subscripts under node heroes('dogs')
  # Get a list of child subscripts under node heroes('dogs')
  iterDogs = irispy.iterator('heroes','dogs').subscripts()
  output = "\nSubscripts under node heroes('dogs'): "
    while True: output += '%s ' %
  except StopIteration:  # thrown when there are no more child nodes
    print(output + '\n')

This code prints the following output:

  Subscripts under node heroes('dogs'): Balto Hachiko Lassie Whitefang

Testing for Child Nodes and Node Values

In the previous examples, the scope of the search is restricted to child nodes of heroes('dogs'). The iterator fails to find two values in global array heroes because they are under different parents:

  • Level 3 node heroes('dogs','Lassie','Timmy') will not be found because it is a child of Lassie, not dogs.

  • Level 2 node heroes('sheep','Dolly') is not found because it is a child of sheep, not dogs.

To search the entire global array, we need to find all of the nodes that have child nodes, and create an iterator for each set of child nodes. The isDefined() method provides the necessary information:

  • iris.IRIS.isDefined() — can be used to determine if a node has a value, a subnode, or both. It returns one of the following values:

    • 0 — the specified node does not exist

    • 1 — the node exists and has a value

    • 10 — the node is valueless but has a child node

    • 11 — the node has both a value and a child node

    The returned value can be used to determine several useful boolean values:

       exists = (irispy.isDefined(root,subscripts) > 0)       # returned 1, 10, or 11
       hasValue = (irispy.isDefined(root,subscripts)%10 > 0)  # returned 1 or 11
       hasChild = (irispy.isDefined(root,subscripts) > 9)     # returned 10 or 11

The following example consists of two methods:

  • findAllHeroes() iterates over child nodes of the current node, and calls testNode() for each node. Whenever testNode() indicates that the current node has child nodes, findAllHeroes() creates a new iterator for the next level of child nodes.

  • testNode() will be called for each node in the heroes global array. It calls isDefined() on the current node, and returns a boolean value indicating whether the node has child nodes. It also prints node information for each node.

Method findAllHeroes()

This example processes a known structure, and traverses the various levels with simple nested calls. In the less common case where a structure has an arbitrary number of levels, a recursive algorithm could be used.

def findAllHeroes():
  root = 'heroes'
  iterRoot = irispy.iterator(root)
  hasChild = False

  # Iterate over children of root node heroes
  for sub1,value in iterRoot:
    hasChild = testNode(value,root,*[sub1])

    # Iterate over children of heroes(sub1)
    if hasChild:
      iterOne = irispy.iterator(root,sub1)
      for sub2,value in iterOne:
        hasChild = testNode(value,root,*[sub1,sub2])

      # Iterate over children of heroes(sub1,sub2)
        if hasChild:
          iterTwo = irispy.iterator(root,sub1,sub2)
          for sub3,value in iterTwo:
            testNode(value,root,*[sub1,sub2,sub3]) #no child nodes below level 3
Method testNode()
def testNode(value, root, *subs):
  # Test for values and child nodes
  state = irispy.isDefined(root,*subs)
  hasValue = (state%10 > 0) # has value if state is 1 or 11
  hasChild = (state > 9)    # has child if state is 10 or 11

  # format the node address output string
  level = len(subs)-1
  address = "  %s%s('%s')" % ("  "*level, root, "','".join(subs))

  # Add node value to string and note special cases
  if (hasValue):  # ignore valueless nodes
    address += ' = %i' % value
    for name in ['Timmy','Dolly']:
      if name == subs[level]:
        address += ' (not a dog!)'
  return hasChild

This method will write the following lines:

    heroes('dogs','Balto') = 1919
    heroes('dogs','Hachiko') = 1923
    heroes('dogs','Lassie') = 1940
      heroes('dogs','Lassie','Timmy') = 1954 (not a dog!)
    heroes('dogs','Whitefang') = 1906
    heroes('sheep','Dolly') = 1996 (not a dog!)

The output of testNodes() includes some nodes that were not found in previous examples because they are not child nodes of heroes('dogs'):

  • heroes('dogs','Lassie','Timmy') is a child of Lassie, not dogs.

  • heroes('sheep','Dolly') is a child of sheep, not dogs.