· Jul 23, 2018 5m read

Intersystems Cache Python Object-Relational Mapper


this is a public announcement for the first release of Intersystems Cache Object-Relational Mapper in Python 3. Project's main repository is located at Github (healiseu/IntersystemsCacheORM).

About the project

CacheORM module is an enhanced OOP porting of Intersystems Cache-Python binding. There are three classes implemented:

The intersys.pythonbind package is a Python C extension that provides Python application with transparent connectivity to the objects stored in the Caché database.

Source Code

The project's code that is released to the public was originally written and used as a module of TRIADB project.

Tests and Demos

There are two folders in this release:

  • testCacheORM contains python jupyter notebook files that demonstrate CacheQuery and CacheClass
  • testCacheBinding are tests written for Intersystems Cache python binding

One can simply compare tests with demos to appreciate the work in this project to leverage intersystems cache python binding. For example

# Intersystems Cache Python binding for queries

import intersys.pythonbind3

# Create a connection 
host = "localhost";
port = "1972";
url = host+"["+port+"]:SAMPLES"  
conn = intersys.pythonbind3.connection()

# Connect Now to SAMPLES namespace
conn.connect_now(url, user, password, None)

# Create a database object
samplesDB = intersys.pythonbind3.database(conn)

# create a query object
cq = intersys.pythonbind3.query(samplesDB)

# prepare and execute query
sql = "SELECT ID, Name, DOB, SSN FROM Sample.Person"

# Fetch rows
for x in range(0,10): 


Same code in only 4 lines using CacheORM python module

from CacheORM import CacheQuery

   samples_query = CacheQuery(namespace='SAMPLES', username='_SYSTEM', password='SYS', dl=99)

   samples_query.execute_sql('SELECT ID, Name, DOB, SSN FROM Sample.Person')


You can view the output from this python Jupyter Notebook at my Microsoft Azure CacheORM library 

Another example, this time  with Cache-Python Objects

# Demo of Intersystems Cache Python binding with Samples namespace and Sample.Person class
import intersys.pythonbind3

conn = intersys.pythonbind3.connection( )
conn.connect_now('localhost[1972]:SAMPLES', '_SYSTEM', '123', None)
samplesDB = intersys.pythonbind3.database(conn)

#%% Create a new instance of Sample.Person to be husband
husband =  samplesDB.create_new("Sample.Person", None)
ssn1 = samplesDB.run_class_method("%Library.PopulateUtils","SSN",[])
dob1 = samplesDB.run_class_method("%Library.PopulateUtils","Date",[])
husband.set("Name","Hatzis, Athanassios I")

# Save husband
print ("Saved id: "+str(husband.run_obj_method("%Id",[])))

#%% Create a new instance of Sample.Person to be wife
wife =  samplesDB.create_new("Sample.Person", None);
ssn2 = samplesDB.run_class_method("%Library.PopulateUtils","SSN",[])
dob2 = samplesDB.run_class_method("%Library.PopulateUtils","Date",[])
wife.set("Name","Kalamari, Panajota");

# Save wife
print ("Saved id: " + str(wife.run_obj_method("%Id",[])))

#%% Relate them


# Open an instance of the Sample.Person object
athanPerson = samplesDB.openid("Sample.Person",str(athanID),-1,-1)

# Open another instance
otherPerson = samplesDB.openid("Sample.Person",str(otherID),-1,-1)   
# Fetch some properties
print ("ID:   " + otherPerson.run_obj_method("%Id",[]))
print ("Name: " + otherPerson.get("Name"))
print ("SSN:  " + otherPerson.get("SSN"))
print ("DOB:  " + str(otherPerson.get("DOB")))
print ("Age: " + str(othePerson.get("Age")))


Same code using CacheORM python module, i.e. object-relational mapping

from CacheORM import CacheClass

# Create an instance of PopulateUtils to call built-in CACHE class method
populateUtils = CacheClass(namespace='%SYS', cachepackage='%Library', cacheclass='PopulateUtils',  username='_SYSTEM', password='SYS')

# Create CacheClass Instance
husband = CacheClass(username='_SYSTEM', password='SYS', dl=99)

# Create and populate a new CacheClass Object
husband.set_value("Name", "Hatzis, Athanassios I")
husband.set_value("DOB", populateUtils.class_method("Date"))

# Save husband

# Create another CacheClass object 
wife = CacheClass(username='_SYSTEM', password='SYS')
wife.set_value("Name", "Kalamari, Panajota")
wife.set_value("DOB", populateUtils.class_method("Date"))

# Relate them
wife.set_refobj("Spouse", person._cache_id)

husband.set_refobj("Spouse", female._cache_id)

# Get Object References

# Open an existing object with id=3 and read cache properties
person = CacheClass(username='_SYSTEM', password='SYS', objectID='3')
print(f"ID:{}\nSSN: {person.get('SSN')}\nName:{person.get('Name')}\nDateOfBirth:{person.get('DOB')}")

You can view the output from this python Jupyter Notebook at my Microsoft Azure CacheORM library 

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

Thanks Evgeny, you cannot execute my jupyter notebooks on Azure cloud and I think you have to login first in order to view them. In any case my CacheORM module is dependent on intersys.pythonbind module. One has to install this first and verify that it works then start playing with my demos. I wrote guidelines about installation in Github README file.