GT.M binding to Python

GT.M binding to Python

 

M is both a language and a database. Just like with most other popular NoSQL databases it is useful to be able to access M from many different languages, even though using it from the M language might be the preferred approach. Bindings to other languages make the M database available to a larger number of applications and facilitates the growth of a larger community.

 

A Python module has now been created to make possible to use GT.M from python scripts.

This Python module builds upon the gtm_cip C-API of GT.M.

It wraps the C API into a C++ class, that in turn is wrapped with SWIG for Python.

 

Details of this Python binding for GT.M are available in this article in the OSEHRA Technical Journal.

 

http://code.osehra.org/journal/browse/publication/26

 

The Python module is called “gtm” and has been uploaded to pypi at,

 

https://pypi.python.org/pypi/gtm/1.0.0

 

Making it possible to install it using the “easy_install” command as:

 

easy_install   gtm

 

Note that this install only the GT.M bindings for Python.
It is expected that GT.M will already be installed in the target system.

 

After installing the gtm python module, you still have to proceed with the two following post-installation steps

 

A) Set the GTMCI environment variable to the full path of the gtm_access.ci file. Typically, this file will go to your python installation after you call easy_install. In our particular case, that location is:

/usr/local/lib/python2.7/dist-packages/gtm-1.0.0-py2.7.egg/gtm_access.ci

The location in your system, of course, could vary.

 

B) Copy the _gtmaccess.m file in to one of the directories where you have M routines. These directories are the ones that you already include in the gtmroutines environment variable. The _gtmaccess.m file is placed by easy_install in the directory:

/usr/local/lib/python2.7/dist-packages/gtm-1.0.0-py2.7.egg
 

C) Add to your LD_LIBRARY_PATH environment variable the directory where you installed GT.M.

More precisely, the directory where the GT.M library

                           libgtmshr.so

is currently located. This will make possible for the Python binding to find the GT.M shared library when you run python scripts. This directory is what you would typically have in the "gtm_dist" environment variable.

 

 

Once you have completed the post-installation, you can try some of the tests available at

 

https://github.com/OSEHRA-Sandbox/gtm-bindings/tree/master/Testing

 

For example, start python code such as:

 

from gtm import GTM

db = GTM()

print db.version()

print db.about()

 

 

Then follow with more interesting code, such as:

 

from gtm import GTM

db = GTM()

globalName = '^Capital("US")'

setValue = 'Washington'

db.set( globalName, setValue )

globalName = '^Capital("UK")'

setValue = 'London'

db.set( globalName, setValue )

getValue = db.order( globalName )

print "Order of ", globalName, " = ", getValue

getValue = db.query( globalName )

print "Query of ", globalName, " = ", getValue

db.execute( 'write $ZVERSION,!')

db.kill('^Capital')

 

 

Enjoy GT.M from Python !

 

like0

Comments

GT.M binding to Python

Sam Habiel's picture

Bravo Bravo Bravo! This so far seems to be the best integration effort for M with other languages (certainly the one with the best license). Sam

like0

Measuring Python binding performance

Luis Ibanez's picture

For an quick evaluation of comparative performance,
Please see:

http://www.osehra.org/blog/gtm-binding-python-and-c-profiling

like0

GT.M binding to Python

Erik Zoltan's picture

This is a great article but it is out of date and some of the links and github resources no longer work. I wonder if anyone is actively using GTM or YottaDB from Python, and if there are resources with OSEHRA on this subject?

like0

Functionality should still work; updated links

K.S. Bhaskar's picture

Although the article is out of date, the functionality described should still work as documented on the YottaDB/GT.M side; I don't know about on the Python side. With regard to links, for YottaDB user documentation, please go to https://docs.yottadb.com, and get software at https://gitlab.com/YottaDB/DB/YDB

Although the content is still valid (calling from Python into an API created in M on YottaDB/GT.M), for someone who wants to access the data management capabilities of YottaDB from Python (as opposed to calling an API for a pre-existing application like VistA/Fileman), I would recommend calling the C Simple API (https://docs.yottadb.com/MultiLangProgGuide/MultiLangProgGuide.html) from Python. The YottaDB development team intends to create a standard Python wrapper in the future, as resources permit.

Regards
– Bhaskar

like0