#Embedded Python

0 Followers · 283 Posts

Embedded Python refers to the integration of the Python programming language into the InterSystems IRIS kernel, allowing developers to operate with data and develop business logic for server-side applications using Python.

Documentation.

Question Evgeny Shvarov · Oct 2, 2022

Hi folks!

How can I refer to a classmethod of the same class while coding another classmethod with Embedded python?

I know that I can call it with iris.cls(classname).MethodName(), but it's more cumbersome even comparing with ObjectScript, where I can call ..MethodName().

Compare ObjectScript:

do..SetupGame()

and the same call in EmbeddedPython:

    iris.cls('eshvarov.sample.SeaBattle.GamePython').SetupGame()

 

Thoughts?

6
0 408
Question Robbie Luman · Jun 27, 2023

Has anyone come across a good using Embedded Python to convert a Python List object to an IRIS %List object?

My use case is I want to open an SQL entry with an Objectscript class method, then pass some information from that row into a separate Python class method which will then create a Python List object, then have the Python class method return that list back to the Objectscript class method in such a way that the Python List can be converted to an IRIS %List object for me to then use in the Objectscript code.

3
0 447
Question Evgeny Shvarov · Jun 25, 2023

Hi folks!

I have a need to use symilar to $property function from Python. Here is the code:

obj=iris.cls('some.persistent.class')._New()

for property, value in data.items():

 $property(obj.property)=value ; I invented this

How could I do this? Any trick?

For now I plan to implement a helper function in iris that I will call, but I doubt also how can I transfer oref to IRIS.

Thoughts?

5
0 307
Question Evgeny Shvarov · Jun 25, 2023

Hi folks!

Consider I need to call a python function which name contains "_" symbol (which is quite often in Python). How it could be called from ObjectScript?

E.g. here is the code:

Set sm = ##class(%SYS.Python).Import("sample")

write sm.helloworld() ; function without _
white sm.hello_world() ; function with _ - won't compile.

Thanks in advance!

3
0 303
Article Robert Cemper · Jun 12, 2023 2m read

Scenario

You all know Open Exchange (OEX) and the is no need for a detailed explanation.

It consists of a directory with various filters and detail pages for packages.

This is great for manual navigation. 
But the most interesting information for me is the content of the blue box on the right.
All content comes from a database somewhere in the background and is not accessible to me and you.

Navigating manually over more than 700 packages in the search of a particular entity is not funny.
So  I decided to have my own table with my criteria of interest.

2
0 413
Article Alex Woodhead · Jun 20, 2023 2m read

Excuse if this is obvious to Python programmers but for those crossing over from ObjectScript this may be a useful tip.

The scenario is developing some Embedded python commands.

Testing out functionality is being confirmed via the shell:

$SYSTEM.Python.Shell()
 
Python 3.9.5 (default, Mar 14 2023, 06:58:44) [MSC v.1927 64 bit (AMD64)] on win32
Type quit() or Ctrl-D to exit this shell.
>>>

When Python evaluates an expression in the shell, it prints the result of the expression to the terminal.

>>> 1 + 2

3

It is quite easy to accidentally evaluate and print out values

0
0 277
Article Robert Cemper · Jun 13, 2023 2m read

Technology Strategy

When I started this project I had set myself limits:
Though there is a wide range of almost ready-to-use modules in various languages
and though IRIS has excellent facilities and interfaces to make use of them
I decided to solve the challenge "totally internal" just with embedded Python, SQL, ObjectScript
Neither Java, nor Nodes, nor Angular, PEX, ... you name it.
The combination of embedded Python and SQL is preferred. ObjectScript is just my last chance.

0
0 218
Article Alex Woodhead · Jun 10, 2023 5m read

Written in reply to community post for can Python create HL7 Message dynamically.

Pre-requisites and setup

Use an integration enabled namespace.
Note: USER namespace is not enabled for interoperability by default.
If following suggest create a new interoperatibility namespace to explore functionality.

# Switch to
ZN "[Interoperability Namespace Name]"

# Launch interactive Python shell:
Do $SYSTEM.Python.Shell()

Script start

0
2 611
Article Joel Solon · May 23, 2023 5m read

Methods written in ObjectScript can use pass-by-reference arguments to return information to the caller. Python doesn’t support pass-by-reference arguments, so Embedded Python in IRIS doesn’t support them either. That's it, that's the end of the post, hope you liked it. 😉 But wait, what about the Classic Rock & Roll?

1
1 592
Article Henry Pereira · Jan 16, 2023 14m read

In this article, I will show you how one can easily create and read Microsoft Word documents using InterSystems IRIS with the leverage power of embedded Python.

Setup

First things first, let’s install the Python module called python-docx. There are a lot of modules to write MS Word files in Python. However, this one is the easiest one to use.

Just execute the following command on the terminal:

!pip3 install python-docx

If you are working with Docker, like I do, just add the following line to a Dockerfile

ENV PIP_TARGET=${ISC_PACKAGE_INSTALLDIR}/mgr/python RUN pip3 install python-docx
7
7 1271
Article Evgeny Shvarov · May 16, 2023 1m read

Hi folks!

Just want to introduce you a new util to import CSV into IRIS - csvgenpy!

Install

USER>zpm "install csvgenpy"

Use:

do ##class(shvarov.csvgenpy.csv).Generate("file or url","table","schema")

Example:

USER>do ##class(shvarov.csvgenpy.csv).Generate("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv","titanic","data")

This will create table and class data.titanic in IRIS and will load the data.  you can proof it with:

0
1 305
Article Dmitry Maslennikov · Feb 6, 2023 4m read

Let me introduce my new project, which is irissqlcli, REPL (Read-Eval-Print Loop)  for InterSystems IRIS SQL 

  • Syntax Highlighting
  • Suggestions (tables, functions)
  • 20+ output formats
  • stdin support
  • Output to files 

Install it with pip

pipinstallirissqlcli

Or run with docker

dockerrun-itcaretdev/irissqlcliirissqlcliiris://_SYSTEM:SYS@host.docker.internal:1972/USER

Connect to IRIS

20
2 895
Article Luis Angel Pérez Ramos · May 8, 2023 6m read

Welcome, dear members of the community!

In this article we are going to demonstrate the great potential that IRIS/HealthConnect makes available to all its users with the use of Embedded Python and we are going to do it by developing a small production that will allow us to recognize and identify the faces present in a JPG file from some images that we will use as a reference.

Project configuration:

2
1 536
Article Seisuke Nakahashi · Apr 27, 2023 2m read

Let's say you have Python including variable-length arguments methods. How can you call it from ObjectScript? 

deftest1(*args):return sum(args)
  
deftest2(**kwargs):
  a1 = kwargs.get("a1",None)
  a2 = kwargs.get("a2",None)
  return a1+a2

You can call this "a.py" from ObjectScript as below.  For **kwargs argument, create Dynamic Object in ObjectScript and put it into methods with <variablename>... (3 dots) format. 

set a=##class(%SYS.Python).Import("a")
    write a.test1(1,2,3)   ;; 6set req={}
    set req.a1=10set req.a2=20write a.test2(req...)   ;; 30
0
1 320
Article Alex Woodhead · Apr 16, 2023 4m read

Overview

Cross-Skilling from IRIS objectScript to Python it becomes clear there are some fascinating differences in syntax.

One of these areas was how Python returns Tuples from a method with automatic unpacking.

Effectively this presents as a method that returns multiple values. What an awesome invention :)

out1, out2 = some_function(in1, in2)

ObjectScript has an alternative approach with ByRef and Output parameters.

Do ##class(some_class).SomeMethod(.inAndOut1, in2, .out2)

Where:

  • inAndOut1 is ByRef
  • out2 is Output

The leading dot (".") in front of the variable name passes ByRef and for Output.

0
0 562
Question Raj Singh · Mar 1, 2023

My general question is how to convert to a %Library.ListOfObjects to a Python "array-like" structure for use in Matplotlib.

Specifically, I have a Line Object which is comprised of a list of Points (see classes below). I want to pass the line to Python to create a Matplotlib Path.

Bonus points for converting the Point to a Python tuple!

Class geo.model.Point Extends %SerialObject
{
  Property latitude As %Float(MAXVAL = 90.0, MINVAL = -90.0, SCALE = 6);
  Property longitude As %Float(MAXVAL = 180.0, MINVAL = -180.0, SCALE = 6);
}
13
0 535
Article Guillaume Rongier · Mar 29, 2023 1m read

Quick Tips: Total Productive Maintenance

Named parameters can be achieved with SQLAlchemy :  

from sqlalchemy import create_engine, text,types,engine

_engine = create_engine('iris+emb:///')

with _engine.connect() as conn:
    rs = conn.execute(text("select :some_private_name"), {"some_private_name": 1})
    print(rs.all())

or with native api

from sqlalchemy import create_engine, text,types,engine

# set URL for SQLAlchemy
url = engine.url.URL.create('iris', username='SuperUser', password='SYS', host='localhost', port=33782, database='FHIRSERVER')

_engine = create_engine(url)

with _engine.connect() as conn:
    rs = conn.execute(text("select :some_private_name"), {"some_private_name": 1})
    print(rs.all())
0
0 551
Article Guillaume Rongier · Nov 30, 2022 2m read

If you are using Python, you can use the built-in venv module to create a virtual environment. This module is the recommended way to create and manage virtual environments.

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

So if like me you work a lot with Python, you can use the venv module to create a virtual environment for your project. This will allow you to install packages without affecting the global Python installation.

You will find here two neat alias to create and activate a virtual environment.

Python aliases

alias venv="python3 -m venv .venv; source .venv/bin/activate"
alias irisvenv="python3 -m venv .venv; source .venv/bin/activate; pip install https://github.com/grongierisc/iris-embedded-python-wrapper/releases/download/v0.0.3/iris-0.0.3-py3-none-any.whl"
3
2 1984
Question Elijah Cotterrell · Mar 6, 2023

I'm curious about how embedded Python is handled by %CSP classes, particularly in the case of defining REST endpoints on IRIS.
Here is a simple dispatch class for the endpoint /api/pythonapp on my local IRIS instance (2022.3):

Class Python.App.Dispatch Extends%CSP.REST
{

XData UrlMap [ XMLNamespace = "https://www.intersystems.com/urlmap" ]
{
<Routes>
    <Route Url="/test" Method="GET" Call="Hello" />
</Routes>
}

ClassMethod Hello() As%Status [ Language = python ]
{
    import iris

    print('Hello World!')
    return True
}

}
4
0 297
Question Kurt Hofman · Feb 10, 2023

Hello,

I'm starting testing embedded python and external libraries.

When I try to use the win32api-library I get the following error :

<THROW> *%Exception.PythonException  230 ^^0^ <class 'ModuleNotFoundError'>: No module named 'win32api' -

This is my code :

ClassMethod CreateDocument(path As %String) [ Language = python ]
{
import win32api

...
}
 

The librarie is visible in C:\InterSystems\IRIS\mgr\python\win32.

Am I doing something wrong ?

Regards,

Kurt Hofman,

ASCI nv.

7
0 651
Article Muhammad Waseem · Feb 9, 2023 2m read

Hi Community,
In this article I will demonstrate below steps to add Interactive map and visualize geographic data to web application:

  • Step1 : Install Application
  • Step2 : Create CSP Page
  • Step3 : Extend dc.IrisGeoMap.Folium class
  • Step4 : Invoke DrawGeoDetails() Method

So Let us start.
 

Step1 : Install Application

First of all we need to install iris-geo-map application by using ZPM

zpm "install iris-geo-map"
0
1 382
Question Dmitry Maslennikov · Feb 6, 2023

So, I know that I can return a SQL Error message from my SQL Procedure written in ObjectScript, with code like this

$ cat <<EOF | irissqlcli iris://_SYSTEM:SYS@localhost:1972/USER
CREATEorREPLACEPROCEDUREtest()
LANGUAGE OBJECTSCRIPT
{
 SET %sqlcontext.%SQLCODE = 400SET %sqlcontext.%ROWCOUNT = -1SET %sqlcontext.%Message = "test error message"
};

CALLtest();

EOF
[SQLCODE: <-400>:<Fatal error occurred>]
[Location: <SPFunction>]
[%msg: <test error message>]

But I did not find how to do it with Python. I can't find %sqlcontext variable available there

1
0 235
Question Oliver Wilms · Feb 5, 2023

I copied a 5 MB messages.log file to AWS where I have iris-log-viewer app deployed. I ran the test to see how it takes in IRIS code to import the lines into a persistent table:

IRISAPP>set m5mb="/home/irisowner/irisdev/messages.old_20221231.log"

IRISAPP>

IRISAPP>do ##class(otw.log.irislogreader).Test1(m5mb)
Test1 begins at 02/05/2023 12:49:30
ReadLogLines
/home/irisowner/irisdev/messages.old_20221231.log
Open
Test1 ends at 02/05/2023 12:49:34
Test1 execution time: 3.500789

select count(*) from otw_log.Log

63239

7
0 299
Article Oliver Wilms · Feb 5, 2023 2m read

As my entry into InterSystems Developer Tools Contest happening now I have been working on an alternative to the Console Log Viewer web page provided as part of InterSystems IRIS Management Portal. Console log is a file called messages.log. It is what InterSystems support asks me to send to them when I open a support ticket. Working on IRIS containers deployed in the cloud it is not straight forward to copy the messages.log file to a place where I can attach it to an email. Iris-log-viewer app provides a Download link to download messages.log from any browser. 

0
1 209
Article Oliver Wilms · Feb 4, 2023 1m read

I developed iris-log-viewer on a work laptop using an older version of IRIS. My messages.log file has nearly 10k lines. I noticed it takes a couple of seconds to read messages.log file line by line, import each line into a persistent class, and display messages on my screen. I wonder if Python can speed up the process.

I developed EmbeddedPython class. Initially I imported a python script file to read messages.log file in Python. Later I figured out how to write a Python classMethod where I invoke IRIS classMethod to import one line at a time while reading messages.log.

0
0 276
Article Yuri Marx · Jul 20, 2022 6m read

Python has become the most used programming language in the world (source: https://www.tiobe.com/tiobe-index/) and SQL continues to lead the way as a database language. Wouldn't it be great for Python and SQL to work together to deliver new functionality that SQL alone cannot? After all, Python has more than 380,000 published libraries (source: https://pypi.org/) with very interesting capabilities to extend your SQL queries within Python. This article details how to create new SQL Stored Procedures in InterSystems IRIS Database using Embedded Python.

Python libraries used as samples

3
0 1279