The py4cl Reference Manual

Table of Contents

Next: , Previous: , Up: (dir)   [Contents][Index]

The py4cl Reference Manual

This is the py4cl Reference Manual, generated automatically by Declt version 2.4 patchlevel 1 "Will Decker" on Fri May 24 09:37:03 2019 GMT+0.


Next: , Previous: , Up: Top   [Contents][Index]

1 Introduction

[[https://travis-ci.org/bendudson/py4cl][https://travis-ci.org/bendudson/py4cl.svg?branch=master]]

* Introduction

Py4CL is a bridge between Common Lisp and Python, which enables Common
Lisp to interact with Python code. It uses streams to communicate with
a separate python process, the approach taken by [[https://github.com/marcoheisig/cl4py][cl4py]]. This is
different to the CFFI approach used by [[https://github.com/pinterface/burgled-batteries][burgled-batteries]], but has the
same goal. 

** Installing

Depends on:

 - Currently tested with SBCL, CCL and ECL (after 2016-09-06). CLISP
   doesn't (yet) have =uiop:launch-program=.
 - ASDF3 version 3.2.0 (Jan 2017) or later, as =uiop:launch-program=
   is used to run and communicate with python asyncronously.
 - [[https://common-lisp.net/project/trivial-garbage/][Trivial-garbage]], available through Quicklisp.
 - Python 2 or 3
 - (optional) The [[http://www.numpy.org/][NumPy]] python library for multidimensional arrays

Clone this repository into =~/quicklisp/local-projects/= or other
location where it can be found by ASDF:
#+BEGIN_SRC bash
$ git clone https://github.com/bendudson/py4cl.git
#+END_SRC

then load into Lisp with
#+BEGIN_SRC lisp
(ql:quickload :py4cl)
#+END_SRC

** Tests

Tests use [[https://github.com/tgutu/clunit][clunit]], and run on [[https://travis-ci.org/][Travis]] using [[https://github.com/luismbo/cl-travis][cl-travis]]. Most development
is done under Arch linux with SBCL and Python3. To run the tests
yourself:
#+BEGIN_SRC lisp
(asdf:test-system :py4cl)
#+END_SRC
or
#+BEGIN_SRC lisp
(ql:quickload :py4cl/tests)
(py4cl/tests:run)
#+END_SRC

* Examples

Py4CL allows python modules to be imported as Lisp packages, python
functions to be called from lisp, and lisp functions called from
python. In the example below, [[https://www.scipy.org/][SciPy]]'s [[https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html][odeint]] function is used to
integrate ODEs defined by a Lisp function. The result is a Lisp array,
which is then plotted using the [[https://matplotlib.org/][matplotlib]] plotting library.

#+BEGIN_SRC lisp
(ql:quickload :py4cl)

(py4cl:import-module "numpy" :as "np")
(py4cl:import-module "scipy.integrate" :as "integrate")

;; Integrate some ODEs
(defparameter *data*
  (integrate:odeint 
   (lambda (y time) 
     (vector (aref y 1)       ; dy[0]/dt = y[1]
             (- (aref y 0)))) ; dy[1]/dt = -y[0]
   #(1.0 0.0)   ; Initial state
   (np:linspace 0.0 (* 2 pi) 20)))  ; Vector of times

; (array-dimensions *data*) => (20 2)

;; Make a plot, save and show it in a window
(py4cl:import-module "matplotlib.pyplot" :as "plt")

(plt:plot *data*)
(plt:xlabel "Time")
(plt:savefig "result.pdf")
(plt:show)
#+END_SRC

More detailed examples of using python packages using =py4cl=:
 - [[./docs/numpy.org][Numpy arrays]]
 - [[./docs/matplotlib.org][Matplotlib plotting]]
 - [[./docs/scipy.org][Scipy scientific library]]

** Direct evaluation of python code

For direct access to the python subprocess, =python-eval=
evaluates an expression, converting the result to a suitable lisp
type. Note that there are nicer, more lispy wrappers around this function,
described below, but they are mostly built on top of =python-eval=.

#+BEGIN_SRC lisp
(asdf:load-system "py4cl")

(py4cl:python-eval "[i**2 for i in range(5)]") ; => #(0 1 4 9 16)
#+END_SRC

#+RESULTS:
| 0 | 1 | 4 | 9 | 16 |

#+BEGIN_SRC lisp
(py4cl:python-eval "{'hello':'world', 'answer':42}") ; => #
#+END_SRC

#+RESULTS:
: #

Data is passed between python and lisp as text. The python function
=lispify= converts values to a form which can be read by the lisp
reader; the lisp function =pythonize= outputs strings which can be
=eval='d in python. The following type conversions are done:

| Lisp type | Python type   |
|-----------+---------------|
| NIL       | None          |
| integer   | int           |
| ratio     | float         |
| real      | float         |
| complex   | complex float |
| string    | str           |
| hash map  | dict          |
| list      | tuple         |
| vector    | list          |
| array     | NumPy array   |
| symbol    | Symbol class  |
| function  | function      |

Note that python does not have all the numerical types which lisp has,
for example rational numbers or complex integers.

Because =python-eval= and =python-exec= evaluate strings as python
expressions, strings passed to them are not escaped or converted as
other types are. To pass a string to python as an argument, call =py4cl::pythonize=

#+BEGIN_SRC lisp
(let ((my-str "testing"))
  (py4cl:python-eval "len(" (py4cl::pythonize my-str) ")" ))
#+END_SRC

#+RESULTS:
: 7

Note that this escaping is done automatically by higher-level interfaces like
=python-call= and =chain=:
#+BEGIN_SRC lisp
(let ((my-str "testing"))
  (py4cl:python-call "len" my-str))
#+END_SRC

#+RESULTS:
: 7

#+BEGIN_SRC lisp
(let ((my-str "testing"))
  (py4cl:chain (len my-str)))
#+END_SRC

#+RESULTS:
: 7

If python objects cannot be converted into a lisp value, then they are
stored and a handle is returned to lisp. This handle can be used to
manipulate the object, and when it is garbage collected the python
object is also deleted (using the [[https://common-lisp.net/project/trivial-garbage/][trivial-garbage]] package).

#+BEGIN_SRC lisp
(destructuring-bind (fig ax) (plt:subplots)
  ;; fig is #S(PY4CL::PYTHON-OBJECT :TYPE "" :HANDLE 6)
  (py4cl:python-eval ax ".plot(" #(0 1 0 1) ")")
  (plt:show)) 
#+END_SRC

The interface to python objects is nicer using =chain= (see below):
#+BEGIN_SRC lisp
(destructuring-bind (fig ax) (plt:subplots)
  (py4cl:chain ax (plot #(0 1 0 1)))
  (plt:show)) 
#+END_SRC

The python process can be explicitly started and stopped using
=python-start= and =python-stop=, but =py4cl= functions start python
automatically if needed by calling =python-start-if-not-alive=.

** Calling python functions

=python-call= can be used to pass arguments to any python callable, 
such as a function in a module:

#+BEGIN_SRC lisp
(py4cl:python-exec "import math")
(py4cl:python-call "math.sqrt" 42)
#+END_SRC

#+RESULTS:
: 6.4807405

or a lambda function:
#+BEGIN_SRC lisp
(py4cl:python-call "lambda x: 2*x" 21)
#+END_SRC

#+RESULTS:
: 42

Keywords are translated, with the symbol made lowercase:
#+BEGIN_SRC lisp
(py4cl:python-call "lambda a=0, b=1: a-b" :b 2 :a 1)
#+END_SRC

#+RESULTS:
: -1

** Calling python methods

Python methods on objects can be called by using the =python-method= function. The first argument
is the object (including strings, arrays, tuples); the second argument is either a string or a symbol
specifying the method, followed by any arguments:
#+BEGIN_SRC lisp
(py4cl:python-method "hello {0}" 'format "world") ; => "hello world"
#+END_SRC

#+RESULTS:
: hello world

#+BEGIN_SRC lisp
(py4cl:python-method '(1 2 3) '__len__) ; => 3
#+END_SRC

#+RESULTS:
: 3

** Chaining python methods

In python it is quite common to apply a chain of method calls, data
member access, and indexing operations to an object. To make this work
smoothly in Lisp, there is the =chain= macro (Thanks to @kat-co and
[[https://common-lisp.net/project/parenscript/reference.html][parenscript]] for the inspiration). This consists of a target object,
followed by a chain of operations to apply.  For example
#+BEGIN_SRC lisp
(py4cl:chain "hello {0}" (format "world") (capitalize)) ; => "Hello world"
#+END_SRC

#+RESULTS:
: Hello world

which is converted to python 
#+BEGIN_SRC python
return "hello {0}".format("world").capitalize()
#+END_SRC

#+RESULTS:
: Hello world

The only things which are treated specially by this macro are lists
and symbols at the top level. The first element of lists are treated as
python method names, top-level symbols are treated as data
members. Everything else is evaluated as lisp before being converted
to a python value.

If the first argument is a list, then it is assumed to be a python
function to be called; otherwise it is evaluated before converting to
a python value. For example
#+BEGIN_SRC lisp
(py4cl:chain (slice 3) stop)
#+END_SRC

#+RESULTS:
: 3

is converted to the python:
#+BEGIN_SRC python
return slice(3).stop
#+END_SRC

#+RESULTS:
: 3

Symbols as first argument, or arguments to python methods, are
evaluated, so the following works:
#+BEGIN_SRC lisp
(let ((format-str "hello {0}")
      (argument "world"))
 (py4cl:chain format-str (format argument))) ; => "hello world"
#+END_SRC

#+RESULTS:
: hello world

Arguments to methods are lisp, since only the top level forms in =chain= are treated specially:
#+BEGIN_SRC lisp
(py4cl:chain "result: {0}" (format (+ 1 2))) ; => "result: 3"
#+END_SRC

#+RESULTS:
: result: 3

Indexing with =[]= brackets is commonly used in python, which calls the =__getitem__= method.
This method can be called like any other method
#+BEGIN_SRC lisp
(py4cl:chain "hello" (__getitem__ 4)) ; => "o"
#+END_SRC

#+RESULTS:
: o

but since this is a common method an alias =[]= is supported:
#+BEGIN_SRC lisp
(py4cl:chain "hello" ([] 4)) ; => "o"
#+END_SRC

#+RESULTS:
: o

which is converted to the python
#+BEGIN_SRC python
return "hello"[4]
#+END_SRC

#+RESULTS:
: o

For simple cases where the index is a value like a number or string
(not a symbol or a list), the brackets can be omitted:
#+BEGIN_SRC lisp
(py4cl:chain "hello" 4) ; => "o"
#+END_SRC

#+RESULTS:
: o

Slicing can be done by calling the python =slice= function:
#+BEGIN_SRC lisp
(py4cl:chain "hello" ([] (py4cl:python-call "slice" 2 4)))  ; => "ll"
#+END_SRC

#+RESULTS:
: ll

which could be imported as a lisp function (see below):
#+BEGIN_SRC lisp
(py4cl:import-function "slice")
(py4cl:chain "hello" ([] (slice 2 4))) ; => "ll"
#+END_SRC

#+RESULTS:
: ll

This of course also works with multidimensional arrays:
#+BEGIN_SRC lisp
(py4cl:chain #2A((1 2 3) (4 5 6))  ([] 1 (slice 0 2)))  ;=> #(4 5)
#+END_SRC

#+RESULTS:
| 4 | 5 |

Sometimes the python functions or methods may contain upper case
characters; class names often start with a capital letter. All symbols
are converted to lower case, but the case can be controlled by passing
a string rather than a symbol as the first element:
#+BEGIN_SRC lisp
;; Define a class
(py4cl:python-exec
   "class TestClass:
      def doThing(self, value = 42):
        return value")

;; Create an object and call the method
(py4cl:chain ("TestClass") ("doThing" :value 31))  ; => 31
#+END_SRC
Note that the keyword is converted, converting to lower case.

** Asynchronous python functions

One of the advantages of using streams to communicate with a separate
python process, is that the python and lisp processes can run at the
same time. =python-call-async= calls python but returns a closure
immediately. The python process continues running, and the result can
be retrieved by calling the returned closure. 

#+BEGIN_SRC lisp
(defparameter thunk (py4cl:python-call-async "lambda x: 2*x" 21))

(funcall thunk)  ; => 42
#+END_SRC

#+RESULTS:
: 42

If the function call requires callbacks to lisp, then these will only
be serviced when a =py4cl= function is called. In that case the python
function may not be able to finish until the thunk is called. This
should not result in deadlocks, because all =py4cl= functions can
service callbacks while waiting for a result.

** Importing functions

Python functions can be made available in Lisp by using =import-function=. By
default this makes a function which can take any number of arguments, and then
translates these into a call to the python function.
#+BEGIN_SRC lisp
(asdf:load-system "py4cl")

(py4cl:python-exec "import math")
(py4cl:import-function "math.sqrt")
(math.sqrt 42) ; => 6.4807405
#+END_SRC

#+RESULTS:
: 6.4807405

If a different symbol is needed in Lisp then the =:as= keyword can be
used with either a string or symbol:
#+BEGIN_SRC lisp
(py4cl:import-function "sum" :as "pysum")
(pysum '(1 2 3))  ; => 6
#+END_SRC

#+RESULTS:
: 6

This is implemented as a macro which defines a function which in turn calls =python-call=.

** Importing modules

Python modules can be imported as lisp packages using =import-module=.
For example, to import the [[https://matplotlib.org/][matplotlib]] plotting library, and make its functions
available in the package =PLT= from within Lisp:
#+BEGIN_SRC lisp :session import-example
(asdf:load-system "py4cl")
(py4cl:import-module "matplotlib.pyplot" :as "plt") ; Creates PLT package
#+END_SRC

#+RESULTS:
: T

This will also import it into the python process as the module =plt=, so that
=python-call= or =python-eval= can also make use of the =plt= module. 

Like =python-exec=, =python-call= and other similar functions, 
=import-module= starts python if it is not already running, so that
the available functions can be discovered.

The python docstrings are made available as Lisp function docstrings, so we can see them
using =describe=:
#+BEGIN_SRC  lisp :session import-example
(describe 'plt:plot)
#+END_SRC

Functions in the =PLT= package can be used to make simple plots:
#+BEGIN_SRC lisp :session import-example
(plt:plot #(1 2 3 2 1) :color "r")
(plt:show)
#+END_SRC

#+RESULTS:
: NIL

** Exporting a function to python

Lisp functions can be passed as arguments to =python-call= 
or imported functions:
#+BEGIN_SRC lisp
(py4cl:python-exec "from scipy.integrate import romberg")

(py4cl:python-call "romberg" 
                   (lambda (x) (/ (exp (- (* x x)))
                                  (sqrt pi)))
                   0.0 1.0) ; Range of integration
#+END_SRC

#+RESULTS:
: 0.4213504

Lisp functions can be made available to python code using =export-function=:
#+BEGIN_SRC lisp
(py4cl:python-exec "from scipy.integrate import romberg")

(py4cl:export-function (lambda (x) (/ (exp (- (* x x)))
                                      (sqrt pi))) "gaussian")

(py4cl:python-eval "romberg(gaussian, 0.0, 1.0)") ; => 0.4213504
#+END_SRC

#+RESULTS:
: 0.4213504



Next: , Previous: , Up: Top   [Contents][Index]

2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 py4cl

Author

Ben Dudson <benjamin.dudson@york.ac.uk>

License

MIT

Description

Call Python libraries from Common Lisp

Dependency

trivial-garbage

Source

py4cl.asd (file)

Components

Next: , Previous: , Up: Top   [Contents][Index]

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.1 py4cl.asd

Location

/home/quickref/quicklisp/dists/quicklisp/software/py4cl-20190521-git/py4cl.asd

Systems

py4cl (system)

Packages

py4cl/config

Exported Definitions

*base-directory* (special variable)


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.2 py4cl/package.lisp

Parent

py4cl (system)

Location

package.lisp

Packages

py4cl


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.3 py4cl/reader.lisp

Dependency

package.lisp (file)

Parent

py4cl (system)

Location

reader.lisp

Internal Definitions

Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.4 py4cl/writer.lisp

Dependency

reader.lisp (file)

Parent

py4cl (system)

Location

writer.lisp

Internal Definitions

Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.5 py4cl/callbacks.lisp

Dependency

writer.lisp (file)

Parent

py4cl (system)

Location

callbacks.lisp

Internal Definitions

Previous: , Up: Lisp files   [Contents][Index]

3.1.6 py4cl/callpython.lisp

Dependency

callbacks.lisp (file)

Parent

py4cl (system)

Location

callpython.lisp

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

4 Packages

Packages are listed by definition order.


Next: , Previous: , Up: Packages   [Contents][Index]

4.1 py4cl/config

Source

/home/quickref/quicklisp/dists/quicklisp/software/py4cl-20190521-git/py4cl.asd

Exported Definitions

*base-directory* (special variable)


Previous: , Up: Packages   [Contents][Index]

4.2 py4cl

Source

package.lisp (file)

Use List

common-lisp

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Exported definitions


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.1 Special variables

Special Variable: *base-directory*
Package

py4cl/config

Source

/home/quickref/quicklisp/dists/quicklisp/software/py4cl-20190521-git/py4cl.asd

Special Variable: *python-command*

String, the Python executable to launch e.g. "python" or "python3"

Package

py4cl

Source

callpython.lisp (file)


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.2 Macros

Macro: chain TARGET &rest CHAIN

Chain method calls, member access, and indexing operations
on objects. The operations in CHAIN are applied in order from
first to last to the TARGET object.

TARGET can be
cons – a python function to call, returning an object to operate on otherwise – a value, to be converted to a python value

CHAIN can consist of
cons – a method to call
symbol – a member data variable
otherwise – a value put between [] brackets to access an index

Keywords inside python function calls are converted to python keywords.

Functions can be specified using a symbol or a string. If a symbol is used then it is converted to python using STRING-DOWNCASE.

Examples:

(chain "hello {0}" (format "world") (capitalize))
=> python: "hello {0}".format("world").capitalize()
=> "Hello world"

(chain (range 3) stop)
=> python: range(3).stop
=> 3

(chain "hello" 4)
=> python: "hello"[4]
=> "o"

Package

py4cl

Source

callpython.lisp (file)

Macro: import-function FUN-NAME &key DOCSTRING AS FROM

Define a function which calls python
Example
(py4cl:python-exec "import math")
(py4cl:import-function "math.sqrt")
(math.sqrt 42)
-> 6.4807405

Keywords:

AS specifies the symbol to be used in Lisp. This can be a symbol
or a string. If a string is given then it is read using READ-FROM-STRING.

DOCSTRING is a string which becomes the function docstring

FROM specifies a module to load the function from. This will cause the python module to be imported into the python session.

Package

py4cl

Source

callpython.lisp (file)

Macro: import-module MODULE-NAME &key AS RELOAD

Import a python module as a Lisp package. The module name should be
a string.

Example:
(py4cl:import-module "math")
(math:sqrt 4) ; => 2.0

or using
Keywords:
AS specifies the name to be used for both the Lisp package and python module. It should be a string, and if not supplied then the module name is used.

RELOAD specifies that the package should be deleted and reloaded.
By default if the package already exists then a string is returned.

Package

py4cl

Source

callpython.lisp (file)


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.3 Functions

Function: export-function FUNCTION PYTHON-NAME

Makes a lisp FUNCTION available in python process as PYTHON-NAME

Package

py4cl

Source

callpython.lisp (file)

Function: python-alive-p &optional PROCESS

Returns non-NIL if the python process is alive
(e.g. SBCL -> T, CCL -> RUNNING).
Optionally pass the process object returned by PYTHON-START

Package

py4cl

Source

callpython.lisp (file)

Function: python-call FUN-NAME &rest ARGS

Call a python function, given the function name as a string
and additional arguments. Keywords are converted to keyword arguments.

Package

py4cl

Source

callpython.lisp (file)

Function: python-call-async FUN-NAME &rest ARGS

Call a python function asynchronously.
Returns a lambda which when called returns the result.

Package

py4cl

Source

callpython.lisp (file)

Function: python-eval &rest ARGS

Evaluate an expression in python, returning the result Arguments ARGS can be strings, or other objects. Anything which is not a string is converted to a python value

Examples:

(python-eval "[i**2 for i in range(" 4 ")]") => #(0 1 4 9)

(let ((a 10) (b 2))
(py4cl:python-eval a

Package

py4cl

Source

callpython.lisp (file)

Function: python-exec &rest ARGS

Execute (using exec) an expression in python. This is used for statements rather than expressions.

Package

py4cl

Source

callpython.lisp (file)

Function: python-method OBJ METHOD-NAME &rest ARGS

Call a given method on an object OBJ. METHOD-NAME can be a symbol (converted to lower case) or a string.

Examples:

(python-method "hello {0}" ’format "world")
; => "hello world"

(python-method ’(1 2 3) ’__len__)
; => 3

Package

py4cl

Source

callpython.lisp (file)

Function: python-setf &rest ARGS

Set python variables in ARGS ("var1" value1 "var2" value2 ...)

Package

py4cl

Source

callpython.lisp (file)

Function: python-start &optional COMMAND

Start a new python subprocess
This sets the global variable *python* to the process handle,
in addition to returning it.
COMMAND is a string with the python executable to launch e.g. "python" By default this is is set to *PYTHON-COMMAND*

Package

py4cl

Source

callpython.lisp (file)

Function: python-start-if-not-alive ()

If no python process is running, tries to start it. If still not alive, raises a condition.

Package

py4cl

Source

callpython.lisp (file)

Function: python-stop &optional PROCESS
Package

py4cl

Source

callpython.lisp (file)

Function: python-version-info ()

Return a list, using the result of python’s sys.version_info.

Package

py4cl

Source

callpython.lisp (file)


Previous: , Up: Exported definitions   [Contents][Index]

5.1.4 Conditions

Condition: python-error ()
Package

py4cl

Source

callpython.lisp (file)

Direct superclasses

error (condition)

Direct methods

text (method)

Direct slots
Slot: text
Initargs

:text

Readers

text (generic function)


Previous: , Up: Definitions   [Contents][Index]

5.2 Internal definitions


Next: , Previous: , Up: Internal definitions   [Contents][Index]

5.2.1 Special variables

Special Variable: *callback-functions*

Hash table which maps unique IDs to functions.

Package

py4cl

Source

callbacks.lisp (file)

Special Variable: *last-callback-function-id*

The ID used for the last callback function.

Package

py4cl

Source

callbacks.lisp (file)

Special Variable: *python*

Most recently started python subprocess

Package

py4cl

Source

callpython.lisp (file)


Next: , Previous: , Up: Internal definitions   [Contents][Index]

5.2.2 Functions

Function: copy-python-object INSTANCE
Package

py4cl

Source

reader.lisp (file)

Function: dispatch-messages PROCESS

Read response from python, loop to handle any callbacks

Package

py4cl

Source

callpython.lisp (file)

Function: function-args ARGS

Internal function, intended to be called by the CHAIN macro. Converts function arguments to a list of strings and (pythonize ) function calls. Handles keywords and insertion of commas. Returns a list which can be passed to PYTHON-EVAL.

Examples:

(py4cl::function-args ’(1 :test 2))
=> ((PY4CL::PYTHONIZE 1) "," "test" "=" (PY4CL::PYTHONIZE 2))

Package

py4cl

Source

callpython.lisp (file)

Function: get-callback ID
Package

py4cl

Source

callbacks.lisp (file)

Function: make-python-object &key (TYPE TYPE) (HANDLE HANDLE)
Package

py4cl

Source

reader.lisp (file)

Function: make-python-object-finalize &key TYPE HANDLE

Make a PYTHON-OBJECT struct with a finalizer.
This deletes the object from the dict store in python.

Uses trivial-garbage (public domain)

Package

py4cl

Source

reader.lisp (file)

Function: python-eval* CMD-CHAR &rest ARGS

Internal function, which converts ARGS into a string to be evaluated This handles both EVAL and EXEC calls with CMD-CHAR being different in the two cases.

Anything in ARGS which is not a string is passed through PYTHONIZE

Package

py4cl

Source

callpython.lisp (file)

Function: python-object-handle INSTANCE
Function: (setf python-object-handle) VALUE INSTANCE
Package

py4cl

Source

reader.lisp (file)

Function: python-object-p OBJECT
Package

py4cl

Source

reader.lisp (file)

Function: python-object-type INSTANCE
Function: (setf python-object-type) VALUE INSTANCE
Package

py4cl

Source

reader.lisp (file)

Function: register-callback FUNCTION
Package

py4cl

Source

callbacks.lisp (file)

Function: stream-read-string STREAM

Reads a string from a stream
Expects a line containing the number of chars following e.g. ’5~%hello’
Returns the string or nil on error

Package

py4cl

Source

reader.lisp (file)

Function: stream-read-value STREAM

Get a value from a stream
Currently works by reading a string then using read-from-string

Package

py4cl

Source

reader.lisp (file)

Function: stream-write-string STR STREAM

Write a string to a stream, putting the length first

Package

py4cl

Source

writer.lisp (file)

Function: stream-write-value VALUE STREAM

Write a value to a stream, in a format which can be read by the python subprocess as the corresponding python type

Package

py4cl

Source

writer.lisp (file)


Next: , Previous: , Up: Internal definitions   [Contents][Index]

5.2.3 Generic functions

Generic Function: pythonize OBJ

Convert an object into a string which can be written to stream. Default implementation returns an empty string

Package

py4cl

Source

writer.lisp (file)

Methods
Method: pythonize (OBJ python-object)

A handle for a python object, stored in a dict in Python

Method: pythonize (OBJ function)

Handle a function by converting to a callback object

Method: pythonize (OBJ hash-table)

Convert hash-table to python map. Produces a string {key1:value1, key2:value2,}

Method: pythonize (OBJ symbol)

Handle symbols. Need to handle NIL,
converting it to Python None, and convert T to True.

Method: pythonize (OBJ string)
Method: pythonize (OBJ cons)

Convert a list. This leaves a trailing comma so that python evals a list with a single element as a tuple

Method: pythonize (OBJ array)
Method: pythonize (OBJ complex)

Create string of the form "(1+2j".
If imaginary part is negative the output is of form "(1+-2j" which is interpreted correctly by python (3.7.2).

Method: pythonize (OBJ real)

Write a real number.
Note that python doesn’t handle ’d’,’f’, ’s’ or ’L’ exponent markers

Method: pythonize OBJ
Generic Function: text CONDITION
Package

py4cl

Methods
Method: text (CONDITION python-error)
Source

callpython.lisp (file)


Previous: , Up: Internal definitions   [Contents][Index]

5.2.4 Structures

Structure: python-object ()

A handle for a python object
which couldn’t be translated into a Lisp value.
TYPE slot is the python type string
HANDLE slot is a unique key used to refer to a value in python.

Package

py4cl

Source

reader.lisp (file)

Direct superclasses

structure-object (structure)

Direct methods

pythonize (method)

Direct slots
Slot: type
Type

string

Initform

""

Readers

python-object-type (function)

Writers

(setf python-object-type) (function)

Slot: handle
Readers

python-object-handle (function)

Writers

(setf python-object-handle) (function)


Previous: , Up: Top   [Contents][Index]

Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts

Jump to:   F   L   P  
Index Entry  Section

F
File, Lisp, py4cl.asd: The py4cl<dot>asd file
File, Lisp, py4cl/callbacks.lisp: The py4cl/callbacks<dot>lisp file
File, Lisp, py4cl/callpython.lisp: The py4cl/callpython<dot>lisp file
File, Lisp, py4cl/package.lisp: The py4cl/package<dot>lisp file
File, Lisp, py4cl/reader.lisp: The py4cl/reader<dot>lisp file
File, Lisp, py4cl/writer.lisp: The py4cl/writer<dot>lisp file

L
Lisp File, py4cl.asd: The py4cl<dot>asd file
Lisp File, py4cl/callbacks.lisp: The py4cl/callbacks<dot>lisp file
Lisp File, py4cl/callpython.lisp: The py4cl/callpython<dot>lisp file
Lisp File, py4cl/package.lisp: The py4cl/package<dot>lisp file
Lisp File, py4cl/reader.lisp: The py4cl/reader<dot>lisp file
Lisp File, py4cl/writer.lisp: The py4cl/writer<dot>lisp file

P
py4cl.asd: The py4cl<dot>asd file
py4cl/callbacks.lisp: The py4cl/callbacks<dot>lisp file
py4cl/callpython.lisp: The py4cl/callpython<dot>lisp file
py4cl/package.lisp: The py4cl/package<dot>lisp file
py4cl/reader.lisp: The py4cl/reader<dot>lisp file
py4cl/writer.lisp: The py4cl/writer<dot>lisp file

Jump to:   F   L   P  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   (  
C   D   E   F   G   I   M   P   R   S   T  
Index Entry  Section

(
(setf python-object-handle): Internal functions
(setf python-object-type): Internal functions

C
chain: Exported macros
copy-python-object: Internal functions

D
dispatch-messages: Internal functions

E
export-function: Exported functions

F
Function, (setf python-object-handle): Internal functions
Function, (setf python-object-type): Internal functions
Function, copy-python-object: Internal functions
Function, dispatch-messages: Internal functions
Function, export-function: Exported functions
Function, function-args: Internal functions
Function, get-callback: Internal functions
Function, make-python-object: Internal functions
Function, make-python-object-finalize: Internal functions
Function, python-alive-p: Exported functions
Function, python-call: Exported functions
Function, python-call-async: Exported functions
Function, python-eval: Exported functions
Function, python-eval*: Internal functions
Function, python-exec: Exported functions
Function, python-method: Exported functions
Function, python-object-handle: Internal functions
Function, python-object-p: Internal functions
Function, python-object-type: Internal functions
Function, python-setf: Exported functions
Function, python-start: Exported functions
Function, python-start-if-not-alive: Exported functions
Function, python-stop: Exported functions
Function, python-version-info: Exported functions
Function, register-callback: Internal functions
Function, stream-read-string: Internal functions
Function, stream-read-value: Internal functions
Function, stream-write-string: Internal functions
Function, stream-write-value: Internal functions
function-args: Internal functions

G
Generic Function, pythonize: Internal generic functions
Generic Function, text: Internal generic functions
get-callback: Internal functions

I
import-function: Exported macros
import-module: Exported macros

M
Macro, chain: Exported macros
Macro, import-function: Exported macros
Macro, import-module: Exported macros
make-python-object: Internal functions
make-python-object-finalize: Internal functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, pythonize: Internal generic functions
Method, text: Internal generic functions

P
python-alive-p: Exported functions
python-call: Exported functions
python-call-async: Exported functions
python-eval: Exported functions
python-eval*: Internal functions
python-exec: Exported functions
python-method: Exported functions
python-object-handle: Internal functions
python-object-p: Internal functions
python-object-type: Internal functions
python-setf: Exported functions
python-start: Exported functions
python-start-if-not-alive: Exported functions
python-stop: Exported functions
python-version-info: Exported functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions
pythonize: Internal generic functions

R
register-callback: Internal functions

S
stream-read-string: Internal functions
stream-read-value: Internal functions
stream-write-string: Internal functions
stream-write-value: Internal functions

T
text: Internal generic functions
text: Internal generic functions

Jump to:   (  
C   D   E   F   G   I   M   P   R   S   T  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.3 Variables

Jump to:   *  
H   S   T  
Index Entry  Section

*
*base-directory*: Exported special variables
*callback-functions*: Internal special variables
*last-callback-function-id*: Internal special variables
*python*: Internal special variables
*python-command*: Exported special variables

H
handle: Internal structures

S
Slot, handle: Internal structures
Slot, text: Exported conditions
Slot, type: Internal structures
Special Variable, *base-directory*: Exported special variables
Special Variable, *callback-functions*: Internal special variables
Special Variable, *last-callback-function-id*: Internal special variables
Special Variable, *python*: Internal special variables
Special Variable, *python-command*: Exported special variables

T
text: Exported conditions
type: Internal structures

Jump to:   *  
H   S   T  

Previous: , Up: Indexes   [Contents][Index]

A.4 Data types

Jump to:   C   P   S  
Index Entry  Section

C
Condition, python-error: Exported conditions

P
Package, py4cl: The py4cl package
Package, py4cl/config: The py4cl/config package
py4cl: The py4cl system
py4cl: The py4cl package
py4cl/config: The py4cl/config package
python-error: Exported conditions
python-object: Internal structures

S
Structure, python-object: Internal structures
System, py4cl: The py4cl system

Jump to:   C   P   S