Hi all,

As promised on comp.lang.lisp to some howls of protest, I have
integrated Python, and its libraries, with Lisp.

Python has an API for embedded use, and it is setup so that you
initialise Python once, and then you call it any number of times
afterwards with a string of code to execute. It's very efficient,
because you only initialise it once.

Initialising Python was easy in CFFI, and so was running strings of
text. The problem was passing data back and forth between Python and
Lisp. Luckily I've used the embedded Python API before, and I knew that
you can pass an array of callback functions to Python, which call a
Python DLL function to get their arguments, and can even return a value
to Python afterwards (using another DLL function). These callbacks are
called from within Python as a virtual module, prefixed by the module
name (eg "Lisp").

I somehow managed to pass this array to Python, so the proof of concept
is complete. Now I just need a little help tidying up the CFFI.

Okay, someone will say "Why would you need to run Python code from
Lisp?". Well, in a lot of cases it's easier to grab a webpage via Python
than it is to dig around for Lisp libraries that will do it. Python
probably has inbuilt maths libraries which Lisp doesn't have as default,
etc.

My basic argument is not to see this as just running bits of Python -
it's an interface to the Python libraries which are cross-platform and
easy to use. The key thing is not a big stick saying "use PythOnLisp"
but rather, giving Lisp newbies more choice until they can tackle the
hard stuff.

The currently-defined callbacks are GetString (return the value of
*callbackstr*), SetString (set the value of *callbackstr* to the given
value) and LispEval (execute a string of Lisp code from Python). This
allows data to be passed back and forth, and Lisp code to be called when
there is no corresponding callback.

Finally, here's a sample of what you can do. Here, I'm grabbing the
system time and passing it to the callback.

[53]> (pythonlisp "from time import *;
lisp.SetString(strftime(\"%a, %d %b %Y %H:%M:%S %Z\", localtime()))")
NIL
[54]> *callbackstr*
"Tue, 31 Jan 2006 19:00:26 GMT Standard Time"
[55]>

Here I'm executing (+ 1 2) in Lisp via Python, and printing the result
(in Python):

[60]> (pythonlisp "print zz.LispEval(\"(+ 1 2)\")")
3
NIL
[61]>

The reason I'm posting here is to ask for somewhere on the web to put
the project, someone to help me with tidying up the CFFI, someone to
maybe test it, and any feedback on what people think of it. What I'm not
asking for is for help writing it, because it's already written. :-)

Cheers,

Jeremy.
-- 
| Jeremy Smith BSc (Hons)
| Chief Scientist, Decompiler Technologies
| Member, British Computer Society
| England
_______________________________________________
Gardeners mailing list
[email protected]
http://www.lispniks.com/mailman/listinfo/gardeners

Reply via email to