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.
def traversal_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)
def traversal_dollar_order_single_level():
for sub,val in irispy.node('^mygbl').items():
print('subscript:',sub,' value:', val)
def traversal_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
def traversal_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:return
while 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
Prior to this, it is important to note that, Unlike IRIS datatypes, which we can treat everything as strings, Python data types such as int, str, bool, and list are classified as objects. Each of these types possesses its own attributes and methods; for instance, the Python string type includes functions such as .upper() and .lower(), which are not applicable to other data types. Consequently, IRIS is equipped with the ability to convert IRIS string values into Python-supported datatype objects through the use of Typecast Methods. This functionality is similarly applicable to class methods, user-defined functions, and procedures. Alternatively, one must utilize Python's type conversion functions to achieve the desired data type.
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.
def get_clsmethod_value():
print(irispy.classMethodValue('Test.MYTest','FirstNameGetStored',1))
date_horolog = irispy.classMethodInteger('Test.MYTest','GetHorolog')
print(irispy.classMethodVoid('Test.MYTest','SetTestGlobal','test'))
classMethodObject: 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.
def cls_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)
iris_proxy_obj.set('RegisteredDate',horolog)
status = iris_proxy_obj.invoke('%Save')
return 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
def cls_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 data
IRIS Class definition which I used for the class and object python code demo
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
classMethodInteger - Return integer value
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
invokeString - default string
invokeFloat - float return value
invokeInteger - return integer value
Will cover the functions, procedure calls in routines and other functionality in the upcoming article.