Question Ronaldo Nascimento · Aug 19

Do I need a separate python iris connection for each Namespace?

Or can I change namespaces within one connection object?

Product version: IRIS 2024.1
$ZV: IRIS for UNIX (Red Hat Enterprise Linux 8 for x86-64) 2024.1.2 (Build 398_0_23819) Wed Nov 20 2024 18:24:21 EST [Health:7.0.3]

Comments

DC AI Bot · Aug 19 Bot

Yes, it is possible to change namespaces within a single connection object. This can be done in ObjectScript using the $NAMESPACE or $ZNSPACE special variables, or by using commands such as ZNSPACE or SET $NAMESPACE. These methods allow temporarily switching the current namespace, either for the duration of the routine or programmatically as needed.

When changing namespaces programmatically, the recommended approach is to use NEW $NAMESPACE followed by SET $NAMESPACE, which ensures that the namespace automatically reverts when the routine concludes or an unexpected error occurs. This prevents issues that may arise from leaving the namespace context inappropriately changed [1][2][3].

For additional control or safety when switching back to the previous namespace, you can store the original namespace, perform your operations in the desired namespace, and explicitly return to the original one using SET $NAMESPACE=<PreviousNamespace>[3][2].

Changing namespaces programmatically in application code should usually be avoided unless strictly necessary, as it may have performance implications and could introduce errors in handling objects or SQL cursors if they span across namespaces. For application-specific functionality, other practices such as global mappings or methods in separate productions might be more suitable [1][3][2].

Sources:

0
Raj Singh  Aug 19 to DC AI Bot

The AI bot isn't paying attention to the Python part of the question?

0
Anastasia Dyubaylo  Aug 19 to Raj Singh

Thank you for the note, Raj. The DC AI bot is designed to be helpful but sometimes it does not fully take the context into account. We are continuing to improve it and comments like yours are helpful :) 

0
Evgeny Shvarov  Aug 19 to Raj Singh

I guess DC AI bot takes only the post body into the consideration thinking the title is a summary of the post. I this case title is one question and post body is another one.

0
David Hockenbroch · Aug 20

In ObjectScript, you would do:

new$NAMESPACEset$NAMESPACE = "USER"

In embedded Python, you can execute those commands using iris.execute:

import iris
iris.execute('new $NAMESPACE')
iris.execute('set $NAMESPACE = "USER"')

I am not far enough into embedded Python to know if that's considered a best practice or if there's another way, but it does work. Here are my results in a Python shell terminal session:

>>> print(iris.system.SYS.NameSpace())
USER
>>> iris.execute('new $NAMESPACE')>>> iris.execute('set $NAMESPACE = "%SYS"')>>> print(iris.system.SYS.NameSpace())%SYS
0
Ronaldo Nascimento  Aug 20 to David Hockenbroch

Im not using embedded python, but a script using the intersystems-irispython module. So the iris object has no method `execute()`

0
Ronaldo Nascimento  Aug 20 to Raj Singh
import iris

address = os.environ['IRIS_HOST']
port = int(os.environ['IRIS_PORT'])
namespace = os.environ['IRIS_NAMESP']
username = os.environ['IRIS_USER']
password = os.environ['IRIS_PASSWD']
# TODO: change to %SYS namespace programmaticly, save the original
# namespace and revert when done
namespace = "%SYS"
connection_string = f"{address}:{port}/{namespace}"
logging.info(f"connecting to: {connection_string}")
connection = iris.connect(connection_string, username, password)
logging.debug(f"{connection}")
cursor = connection.cursor()
0
Owen Crane  Aug 20 to Ronaldo Nascimento
import iris

# Open a connection to the server
args = {'hostname':'127.0.0.1', 'port':52773,
    'namespace':'USER', 'username':'_SYSTEM', 'password':'SYS'
}
conn = iris.connect(**args)
# Create an iris object
irispy = iris.createIRIS(conn)
0
Owen Crane  Aug 20 to Owen Crane

Looks like you specify namespace in the iris.connect() args.

0
David Hockenbroch  Aug 20 to Ronaldo Nascimento

In this case, you'd probably have to create a class method in ObjectScript that changes the namespace, then call it using classMethodVoid() from the IRIS native SDK. But you'd have to make sure the necessary class method exists in all of the namespaces you want to change to and from, including %SYS.

0
Vitaliy Serdtsev · Aug 21
import iris

# first, we connect to the %SYS namespace
conn = iris.connect('localhost:1972/%SYS','_SYSTEM',pwd,10,True)
irispy = iris.createIRIS(conn)

# here we switch to the USER namespace
newNameSpace = irispy.classMethodString('%SYSTEM.Process','SetNamespace','USER')

# now we can call the method from the new namespace
#print(irispy.classMethodInteger('DC.Unix','RoundPosixToSeconds',1154669852181849976))

conn.close()
0