Article
· Sep 22, 2022 4m read

Python and IRIS in practice - with examples!

Here you'll find a simple program that uses Python in an IRIS environment and another simple program that uses ObjectScript in a Python environment. Also, I'd like to share a few of the troubles I went trough while learning to implement this.

 

Python in IRIS environment

Let's say, for example, you're in an IRIS environment and you want to solve a problem that you find easy, or more efficient with Python.

You can simply change the environment: create your method as any other, and in the end of it's name and specifications, you add [ Language = python ]:

 

You can use any kinds of arguments in the method, and to access them you do the exact same thing you would in COS:

Say you have this %String argument, Arg, and an argument that comes from a custom class, OtherArg. This other class may have properties such as Title, and Author. You will want to access them like this:

This method provides an output such as:

 

And, for accessing class methods, it's pretty much the same. Let's say we have a method in Demo.Books.PD.Books called "CreateString" that concatenates the title and author into something like "Title: <Title>; Author: <Author>".

Adding this to the end of our python method:

will provide the following output:

(To access the method, you use OtherArg.CreateString(), but I chose to pass the same values in OtherArg to the CreateString method so the outputs would look alike and for the code to look simpler)

 

ObjectScript in Python Environment

Also, there might exist situations where you are in a Python environment, but you want ObjectScript's supplies to help you.

 

First, you will want to check a few items off of this list to be able to access your COS files from a Python environment in many ways (I won't necessarily use all of it here):

You can always come back to those links or check the errors I ran into while first creating my python files with COS, if you find it helpful.

So let's start coding!

 

First, we will have to adapt a few things from COS to Python. Luckily, InterSystems already did it and we only have to type "import iris" to access everything!

On the next step, we create a connection to the desired namespace, using a path with host, port and namespace (host:port/namespace), and providing user and password:

Notice how we created an IRIS OBJECT in the end, so we can use this connection to have access to everything we want in that namespace.

 

Finally, you code everything you want:

You can access methods with the irispy.classMethodValue() by providing the class' name, the method's name and arguments, manipulate objects with .set() (for properties) and many other possibilites, while treating everything on python the way you prefer.

For more information on functions provided by iris and how to use them, check Introduction to the Native SDK for Python

 

In this example, on line 16 I instanciated a Persistent class, to set it's properties Title and Author to Lord of the Rings and Tolkien on the following lines.

On line 20 I called a method from another class that saves the object to a table and returns a status if it worked. Finally , I print the status on line 23.

 

Mix!

Within an ObjectScript environment, you might want to use python's known libraries or your own custom files with functions and routines.

You can use the "import" command with Numpy, SciPy and everything you want (provided that you have them correctly installed: check here how to do that)

But also, if you want to access your local files, there are several ways to do that, and it's easy to find tutorials for that, since Python is so popular.

 

To me, the easiest to work with is the following:

Here I imported everything from the file testesql.py located in C:/python, and printed the results of the select() function

 

 

Extras - troubles I ran into

  • SHELLS: When using Shells, remember that the Windows PowerShell works as an UNIX-based system (since it is based on .NET) and the Command Prompt is the one that will work with the Windows' examples in the official documentation. This can sound basic for some more experienced programmers, but if you're not paying enough attention you can lose quite some time on this so I found important to write a bit about it.
  • USER AND PRIVILEGES: The current user for coding with ObjectScript from a Python environment needs to have privileges for the Namespace's resources. Remember that if you don't select any user, the current is UnknownUser, and if you don't select any namespaces, the current is USER. So, in the most simple access, you might want to follow: Management Portal > System Management > Security > Users > UnknownUser > Roles and select %DB_USER and save.
  • I DON'T KNOW WHAT'S GOING ON: To check more information on errors you're having, you might want to follow Management Portal > System Explorer > SQL and type "SELECT * FROM %SYS.Audit ORDER BY UTCTimeStamp Desc" to get the most recent audit. There you'll find causes for errors such as IRIS_ACCESSDENIED() and much more you might be getting even outside IRIS environment.
  • PYTHON COMPILING ERROR: You will want to avoid Methods names such as try() or functions that are already built in python. The compiler won't understand the difference from the Method to the function.

 

 

Thank you for reading, and please feel free to share suggestions, comments, doubts or just whatever you're developing!

Discussion (9)2
Log in or sign up to continue

@Heloisa Paiva 
Thanks for this great primer.

Does embedded Python support return values?  I don't see any examples here or on the IRIS documentation of class methods using embedded Python returning a value.

Also, is there a well documented source for the iris Python package?  I was following the documentation to read a global using iris.gref("^Global") to read the global from the keys and it took testing it on the API command line to find that to get a value at a global with multiple keys I had pass in something like globalRef.get(["key1","key2"]).  Not horrible to figure out but for someone used to COS it would be helpful 1) know what the iris package has in it and 2) how to use them without testing them each time.

Thank!

Hi @Michael Davidovich ! Thank you for the feedback. Now, let's take a look at those questions:

1- Does embedded Python support return values?
For a  short answer, yes it does. But as there are many ways to access python, there are many ways I can answer that. In fact I found it a very interesting topic to discuss and I'm working on a new article so I can cover more cases, but just to give it a taste, if you're using in IRIS a ClassMethod [ Language = python ] you can use "return ..." to finish the method and return a value, as you would after declaring a function in python.

2- 
Also, is there a well documented source for the iris Python package?

The documented source is Native SDK for Python Quick Reference. Now, if it is "well documented" you'll have to answer for yourself hahaha, but I'm working on getting more information and examples here. From what I've learned so far, the package has ways to connect from your python environment to IRIS, in such a way that you can access any methods you've created on IRIS, so I see it as a door to everything in IRIS.