Python Interface

 

Introduction

Python is an interpretive scripting language which has become a center for Data Analytics libraries and applications. The AIS interface to the Python language is designed to give seemless access to Python data analytics libraries and scripts, and also to allow seamless extension of Python with AIS Lisp lambdas. Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python?s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python web site, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation. The Python interpreter is easily extended with new functions and data types implemented in Lisp, C, C++, or other languages callable from C. Python is also suitable as an extension language for customizable applications. For a description of standard objects and modules, see The Python Standard Library. The Python Language Reference gives a more formal definition of the language. To write extensions in C or C++, read Extending and Embedding the Python Interpreter and Python/C API Reference Manual. There are also several books covering Python in depth.

python

The python function executes one or more Python statements. The Python statements are in a single string and are separated by semicolons. If only a single Python expression is specified, then the value of the expression is returned.

Type: Function

Syntax: (python statements)
statements statementList One or more Python statements separated by semicolons.
Returns value Always returns the value of the single expression or true if statements are present.

Example

        ;; Loads the os module and the sys module into the Python workspace.
(python "import os;import sys;") ;; Returns true
;; Sets the variable x.
(python "x = 22;") ;; Returns true
;; Increments the variable x.
(python "x = (x + 1);") ;; Returns true
;; Evaluates the expression.
(python "(x + 1);") ;; Returns 23

python initialize

The python initialize command initializes the python interpreter and clears the Python workspace.

Type: Function command

Syntax: (python initialize:)
command initialize: Initializes the Python interpreter and clears the Python workspace.
Returns true Always returns true unless an error occurs.

Example

        ;; Initializes the Python interpreter and clears the Python workspace.
;; Should be the first command issued to the python interpreter.
(python initialize:)

python finalize

The python finalize command clears the Python interpreter and clears the Python workspace..

Type: Function command

Syntax: (python finalize:)
command finalize: Clears the Python interpreter and clears the Python workspace.
Returns true Always returns true unless an error occurs.

Example

        ;; Finalizes the Python interpreter and clears the Python workspace.
;; Should be the last command to the Python interpreter.
(python finalize:)

python eval

The python eval command evaluates the specified Python expression.

Type: Function command

Syntax: (python eval: expression)
command eval: Evaluates the specified Python expression.
expression expression A Python expression.
Returns expression value Always returns the value of the expression.

Example

        ;; Returns the value of the specified Python expression.
(python eval: "(1 + 1)")

python run:

The python run command executes one or more Python statements. The Python statements are in a single string and are separated by semicolons.

Type: Function command

Syntax: (python run: statements)
command run: Executes the specified statements.
statements statementList One or more Python statements separated by semicolons.
Returns true Always returns true unless an error occurs.

Example

        ;; Loads the os module and the sys module into the Python workspace.
(python run: "import os;import sys;")

python type

The python type command returns the type name of the Python object pointer.

Type: Function command

Syntax: (python type: pointer)
command type: Returns the type name of the Python object pointer.
pointer pointer A Python object pointer.
Returns type name Returns the type name of the Python object pointer.

Example

        ;; Returns a pointer to the specified Python object.
(setq pointer (python eval: "platform.version"))
;; Returns the type name of the pointer = "function"
(python type: pointer) ;; Returns "function".

python incref

The python incref command Increments the reference count of the Python object pointer. This protects the python object from python garbage collection.

Type: Function command

Syntax: (python incref: pointer)
command incref: Increments the reference count of the Python object pointer.
pointer pointer A Python object pointer.
Returns true Always returns true.

Example

        ;; returns a Python object pointer
(setq obj (python eval: "__builtins__.print"))
(python incref: obj) ;; increment reference counter to protect from Python garbage collection.

python decref

The python decref command decrements the reference count of the Python object pointer. This allows the python object to be python garbage collected.

Type: Function command

Syntax: (python decref: pointer)
command decref: Decrements the reference count of the Python object pointer.
pointer pointer A Python object pointer.
Returns true Always returns true.

Example

        ;; returns a Python object pointer
(setq obj (python eval: "__builtins__.print"))
(python incref: obj) ;; increment reference counter to protect from Python garbage collection. (python decref: obj) ;; decrement reference counter to allow Python garbage collection.

Python ais module

The Python ais module provides Python data analyst with seamless access to the Analytic Information Server via the AIS Lisp language module. The ais.lisp function executes one or more Lisp statements from within Python.

Using the ais module, the Python data analyst has seamless access to the vast data analytic libraries and modules provided by the Analytic Information Server. Some of these include: the MySQL relational database engine; the QSQL database engine; the math library; the ARC Algebra Network machine learning module, and more.

Using the ais module, the Python data analyst can replace C as the source for fast computation intensive modules. The AIS Lisp language executes as fast as or faster than C for most register and pointer based numeric and string computations (see the Tutorial_TestSuite for actual timings on your cpu).

Type: Function

Syntax: (ais.lisp "(* 23 4)")
expression expressionList One or more Lisp expressions.
Returns expression value Always returns the value of the expression.

Example: Simple Lisp Expressions

        ;; Imports the ais module into the Python workspace.
(python "import ais":) ;; Allows Python to evaluate Lisp expressions.
(python "ais.lisp( '(+ 1 2 3 4 5))' ") ;; Returns 15
(python "ais.lisp( '(setq x (+ 1 2 3 4 5))' )") ;; Returns 15
x ;; Returns 15 as it was set in previous Python call
(setq y (python {ais.lisp( '(python "1 + 2 + 3 + 4 + 5")' )})) ;; Returns 15 recursively

Example: Redirecting Python Output

The Python standard output and standard error data can be redicrected to the AIS console.

       ;; Redirect python stdout to AisRad Console
(python "old_stdout = sys.stdout; old_stderr = sys.stderr;") ;; keep a copy of the original stdout & stderr if we plan to restore it later
(python "sys.stdout = ais; sys.stderr = ais;") ;; replace the default stdout & stderr objects with AIS to trick the print function
(python "print('1000')") ;; will now appear in the AIS console

Example: Running the ARC Test Suite

The full services of the Abstract Regression Classification (ARC) module are available to the Python programmer (via the ais module). The ARC module is a machine learning module using deep learning algebra networks.

       ;; Run the ARC Test Suite
(python "ais.lisp('(arc.runTestSuite)')") ;; Run the ARC algebra network test suite.

Example: Running AIS MySQL

The full services of the Abstract Regression Classification (ARC) module are available to the Python programmer (via the ais module). The ARC module is a machine learning module using deep learning algebra networks.

       ;; Run embedded AIS MySQL relational database
(python {ais.lisp('(sql "SELECT * FROM Sales ORDER BY Salesman")))')}) ;; Run a MySQL Select statement on the Sales relation.