Hello Community
I have previously experimented with embedded Python in IRIS; however, I have not yet had the opportunity to implement IRIS using native Python. In this article, I aim to outline the steps I took to begin learning and implementing IRIS within the Python source. I would also like thank to @Guillaume Rongier and @Luis Angel Pérez Ramos for their help in resolving the issues I encountered during my recent PIP installation of IRIS in Python, which ultimately enabled it to function properly.
Let's begin to write IRIS in python.
First comes First, You have to install the intersystems_irispython-3.2.0-py3-none-any.whl file from the github repo. I downloaded and installed in my windows machine.
py -m pip install intersystems_irispython-3.2.0-py3-none-any.whl
PythonPython
Verified the packages are installed in my machine by executing py -m pip list
intersystems-irispython 3.2.0 iris 0.0.5
Now ready to start writing the python. I've created a .py file and import iris package on top of the class.
Now let's establish the connection to IRIS by using connection method and create use the connection object to instantiate the iris.IRIS object by using "createIRIS" and this is the crucial step to proceed further operations.
import iris
impor time
args = {'hostname':'127.0.0.1', 'port':1972,'namespace':'LEARNING', 'username':'_SYSTEM', 'password':'SYS'}
try:
"""
some other ways instead of kwargs
conn = iris.connect(hostname='127.0.0.1', port=1972, namespace='LEARNING',username='_SYSTEM',password='SYS')
"""
conn = iris.connect(**args)
# A new IRIS object that uses the given connection.
irispy = iris.createIRIS(conn)
print('connected!')
except Exception as e:
# Handling the exception and printing the error
print(f"An error occurred: {e}")
PythonPython
Now let's talk about the Methods for COS and global
Once you had successfully created an IRIS object. Now it's ready to use various operations
set : This function is used to define the global values in to the IRIS database
1. fist parameter is set value
2. second parameter is global name
3. *args - third parameter is subscript(s)
def set_global(value=None,gbl=None,*args):
#set method is in _IRIS from iris package
irispy.set('set from irispy','mygbl','a',str(time.time()))
print('global set done!')
set_global()
PythonPython
kill: This function is used to delete the global from database
def kill_global():
irispy.kill('mygbl')
print('global kill done!')
PythonPython
IsDefined: equals to $data : verify it exist
def data_isDefined():
# equal to $data
print(irispy.isDefined('mygbl','a')) # o/p 10
print(irispy.isDefined('mygbl','a','1724996313.480835')) # o/p 1
ObjectScriptObjectScript
nextSubscript: Equals to $Order
irispy.nextSubscript(0,'mygbl','a')
ObjectScriptObjectScript
tStart, tCommit and tRollback: same as TStart, TCommit, TRollback
def global_transaction_commit():
irispy.tStart()
print(irispy.getTLevel())
irispy.set('set from irispy','mygbl','trans',str(time.time()))
irispy.tCommit()
def global_transaction_rollback():
irispy.tStart()
print(irispy.getTLevel())
irispy.set('set from irispy','mygbl','trans1',str(time.time()))
irispy.tRollback() # tRollbackOne()
ObjectScriptObjectScript
lock and unlock: by default incremental lock/exclusive lock
def global_lock():
#default exclusive lock
s = irispy.lock('',1,'^mygbl')
time.sleep(10) # verify the lock in locktab
irispy.unlock('','^mygbl')
def global_shared_lock():
s = irispy.lock('S',1,'^mygbl')
time.sleep(10)
irispy.unlock('S','^mygbl')
PythonPython
node: subscript level traversal same as $Order
def node_traversal():
# subscript level traversal like $Order
for mrn in irispy.node('^mygbl'):
for phone in irispy.node('^mygbl',mrn):
print(f'patient mrn {mrn} and phone number: {phone}')
"""
o/p
patient mrn 912 and phone number: 3166854791
patient mrn 991 and phone number: 78050314
patient mrn 991 and phone number: 9128127353
"""
ObjectScriptObjectScript
The node, value traversal and class definitions and it members are covered in the next article.
You can refer the IRIS documentation for all the functions.