IRIS Native Python Part-2
In the preceding section, we explored the installation process and initiated the writing of the IRIS in native Python. We will now proceed to examine global traversal and engage with IRIS class objects.
get: this function is used to get values from the traversal node.
deftraversal_firstlevel_subscript():"""
^mygbl(235)="test66,62" and ^mygbl(912)="test118,78"
"""for i in irispy.node('^mygbl'):
print(i, gbl_node.get(i,''))
node and items: single level traversal with node and get the values same as $Order(^mygbl(subscript), direction, data)
#single level traversaldeftraversal_dollar_order_single_level():for sub,val in irispy.node('^mygbl').items():
print('subscript:',sub,' value:', val)
# multi level traversaldeftraversal_dollar_order_multi_level():for sub,val in irispy.node('^mygbl').items():
print(f'sub type is: {type(sub)}{sub} and val type is {type(val)}')
for sub1,val1 in irispy.node('^mygbl',sub).items():
print('subscript:',sub1,' value:', val1)
nextsubscript: unlike above code you can use nextsubscript to get the next subscript easily
deftraversal_dollar_order_use_nextsubscript():
direction = 0
next_sub = ''while next_sub != None:
next_sub = irispy.nextSubscript(direction,'^mygbl', next_sub)
print(f'next subscript = {next_sub}' )
next_sub1=''if next_sub == None:returnwhile next_sub1 != None:
next_sub1 = irispy.nextSubscript(direction,'^mygbl',next_sub,next_sub1)
print(f'1st subscript = {next_sub} next subscript {next_sub1}' )
Classes and Objects
You can call the classmethods, methods from the class definition by using the specific function. As I mentioned earlier Typecast Methods are crucial to get the proper response from IRIS
classMethodValue: Call the Classmethod from python without initiate the object same as ( ex: Do ##Class(Test.MYTest).FirstNameGetStored(1) ) and get "string" default value in python. There are different typecast method available for expected return value instead of string. Please refer below.
defget_clsmethod_value():
print(irispy.classMethodValue('Test.MYTest','FirstNameGetStored',1)) #return string
date_horolog = irispy.classMethodInteger('Test.MYTest','GetHorolog') #return +$H value
print(irispy.classMethodVoid('Test.MYTest','SetTestGlobal','test')) # no return resposneclassMethodObject: Important function to Instantiate a new IRIS object or open the existing object. Set the properties and invoke instance methods etc...
New IRIS object: Initiate the class object for Test.MYTest and set the properties.
defcls_object_new():"""
initiate new object and store
"""
iris_proxy_obj = irispy.classMethodObject('Test.MYTest','%New','ashok','kumar')
birthdate_horolog = irispy.classMethodInteger('Test.MYTest','GetHorolog','12/12/1990')
horolog = irispy.classMethodInteger('Test.MYTest','GetHorolog')
iris_proxy_obj.set('BirthDate',birthdate_horolog) #set birthdate property
iris_proxy_obj.set('RegisteredDate',horolog) #set the RegisteredDate property
status = iris_proxy_obj.invoke('%Save') #call instance methodreturn status
Open IRIS Object: In the below code. Open Test.MyTest class object and get both Birthdate and RegisteredDate values from the objectid "2" and convert RegisteredDate as python list
defcls_object_open():
iris_proxy_obj = irispy.classMethodObject('Test.MYTest','%OpenId',2)
birth_date = iris_proxy_obj.get('BirthDate')
full_name iris_proxy_obj.InvokeString("GetFullName")
data = [birth_date, iris_proxy_obj.get('RegisteredDate')]
return dataIRIS Class definition which I used for the class and object python code demo
Spoiler
Class Test.MYTest Extends%Persistent
{
Property FirstName As%String;Property LastName As%String;Property BirthDate As%Date;Property RegisteredDate As%Date;
Method %OnNew(FirstName, LastName) As%Status
{
Set..FirstName = FirstName
Set..LastName = LastName
Return$$$OK
}
ClassMethod GetHorolog(pDate As%String = "") [ CodeMode = expression ]
{
$Select(pDate'="":$ZDH(pDate),1: +$H)
}
Method GetFullName() As%String
{
Return..FirstName_","_..LastName
}
}
Typecast methods:
these are few type cast method for retrieve proper return values from IRIS
classMethodValue() - for call general classmethods
classMethodVoid - No return value
classMethodValue - default string
classMethodFloat - float return value
invoke() - is used to call the instance methods. You have to initiate the object for call this invoke functions
invokeFloat - float return value
invokeInteger - return integer value
Will cover the functions, procedure calls in routines and other functionality in the upcoming article.