Python and IRIS in practice - with examples!
Tags: python, programming, beginners, tutorial
Published: 2023-12-23 14:43:08

<p>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&nbsp;while learning to implement this.</p>
<p>&nbsp;</p>
<h2>Python in IRIS environment</h2>
<p>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&nbsp;with Python.</p>
<p>You can simply change the environment:&nbsp;create your method as any other, and in the end of it's name and specifications, you add [ Language = python ]:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avwqpkwbli9y8vr9youl.png)
<p>&nbsp;</p>
<p>You can use any kinds of arguments in the method, and to access them you do the exact same thing you would in COS:</p>
<p>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:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e773o1m8enyy014h1534.png)
<p>This method provides an output such as:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwjch3wlzohjbeibthdh.png)
<p>&nbsp;</p>
<p>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: &lt;Title&gt;; Author: &lt;Author&gt;".</p>
<p>Adding this to the end of&nbsp;our&nbsp;python method:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qc4lmczubfs6hlin5xr.png)
<p>will provide the following output:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jd4fu5fzvsltuqeiauvu.png)
<p>(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)</p>
<p>&nbsp;</p>
<h2>ObjectScript in Python Environment</h2>
<p>Also, there might exist situations where you are in a Python environment, but you want ObjectScript's supplies to help you.</p>
<p>&nbsp;</p>
<p>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):</p>
<ul>
<li>Do you have the prerequisites?&nbsp;<a href="https://docs.intersystems.com/iris20221/csp/docbook/Doc.View.cls?KEY=AEPYTHON#AEPYTHON_prereqs" target="_blank">Check here</a></li>
<li>Can you use a python external server?&nbsp;<a href="https://docs.intersystems.com/iris20221/csp/docbook/Doc.View.cls?KEY=BEXTSERV_install#BEXTSERV_install_python" target="_blank">Check here</a></li>
<li>Do you have the drivers you need?&nbsp;<a href="https://intersystems-community.github.io/iris-driver-distribution/" target="_blank">Download them here</a>&nbsp;or&nbsp;<a href="https://docs.intersystems.com/iris20221/csp/docbook/DocBook.UI.Page.cls?KEY=BTPI_pyapi#BTPI_pyapi_usage_example" target="_blank">Learn more here</a></li>
</ul>
<p>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.</p>
<p>So let's start coding!</p>
<p>&nbsp;</p>
<p>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!</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3uvo7hyd8jbm3z1gpgh.png)
<p>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:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wp31yq31nmvtbffqfsx.png)
<p>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.</p>
<p>&nbsp;</p>
<p>Finally, you code everything you want:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4ljvt373aa46te954v5.png)
<p>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.</p>
<p>For more information on functions provided by iris and how to use them, check&nbsp;<a href="https://docs.intersystems.com/iris20221/csp/docbook/DocBook.UI.Page.cls?KEY=BPYNAT_about" target="_blank">Introduction to the Native SDK for Python</a></p>
<p>&nbsp;</p>
<p>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.</p>
<p>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.</p>
<p>&nbsp;</p>
<h2>Mix!</h2>
<p>Within an ObjectScript environment, you might want to use python's known libraries or your own custom files with functions and routines.</p>
<p>You can use the "import" command with Numpy, SciPy and everything you want (provided that you have them correctly installed:&nbsp;<a href="https://docs.intersystems.com/iris20221/csp/docbook/DocBook.UI.Page.cls?KEY=AFL_epython#AFL_epython_pylibrary" target="_blank">check here how to do that</a>)</p>
<p>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.</p>
<p>&nbsp;</p>
<p>To me, the easiest to work with is the following:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0g21qcgjxrrtfs5d3o0x.png)
<p>Here I imported everything from the file testesql.py located in C:/python, and printed the results of the select() function</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>Extras - troubles I ran into</h2>
<ul>
<li><strong>SHELLS</strong>: 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.</li>
<li><strong>USER AND PRIVILEGES</strong>: The current user for coding with ObjectScript from a Python environment needs to have privileges for the Namespace's resources. Remember that&nbsp;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 &gt; System Management &gt; Security &gt; Users &gt; UnknownUser &gt; Roles and select %DB_USER and save.</li>
<li><strong>I DON'T KNOW WHAT'S GOING ON:&nbsp;</strong>To check more information on errors you're having, you might want to follow Management Portal &gt; System Explorer &gt; SQL and type "SELECT *&nbsp;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.</li>
<li><strong>PYTHON COMPILING ERROR:&nbsp;</strong>You will want to avoid Methods&nbsp;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.</li>
</ul>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Thank you for reading, and please feel free to share suggestions, comments, doubts or just whatever you're developing!</p>