Do I need a separate python iris connection for each Namespace?
Or can I change namespaces within one connection object?
Comments
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:
The AI bot isn't paying attention to the Python part of the question?
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 :)
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.
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())%SYSIm not using embedded python, but a script using the intersystems-irispython module. So the iris object has no method `execute()`
@Ronaldo Nascimento what kind of connection? DBAPI? Can you share a code sample?
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()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)
Looks like you specify namespace in the iris.connect() args.
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.
It seems like each connection object represents a connection to only one namespace.It looks like the constructor for the connection object will take a namespace, and therefore will be locked to that one.
I'm making that assumption based on this page: https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…
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()