From robert.dodier at gmail.com Sat May 5 05:10:26 2007 From: robert.dodier at gmail.com (Robert Dodier) Date: Fri, 4 May 2007 23:10:26 -0600 Subject: [clpython-devel] calling Lisp from Python and vice versa Message-ID: Hello, I am interested in using CL-Python as a front end for Lisp libraries. To this end, I wonder how to call a Lisp function from withing Python. I know you can enter " (foo bar)" to call a Lisp function, but its return value is not known to Python. I'd like to be able to do something like "x = foo(bar)". Couple of questions related to this. How can I reference Lisp variables from Python and vice versa? Can I use import to load a Lisp file? After loading the Lisp file, how do functions and variables in it appear in Lisp --- does the Lisp package name come into play? What about Lisp symbols which contain characters not allowed in Python --- is there a way to escape them? Can a Lisp function return arbitrary Lisp objects, and if so how are those displayed in Python? Thanks a lot to Willem Broekma for inventing and maintaining CL-Python. I think it's a great idea. My immediate interest is to use CL-Python as an interface for the symbolic computation system Maxima which is written in CL. best, Robert Dodier From robert.dodier at gmail.com Sat May 5 05:15:00 2007 From: robert.dodier at gmail.com (Robert Dodier) Date: Fri, 4 May 2007 23:15:00 -0600 Subject: [clpython-devel] import fails due to null search paths in *habitat* Message-ID: Hello, I'm running CL-Python w/ Allegro Express 8.0 on Linux. I find that import fails for anything except stuff in the current working directory. That appears to be because *habitat* has search paths = nil. I tried (setf (habitat-search-paths *habitat*) ) and that works OK for importing .py files but attempting to import built-in stuff like sys fails. Is there some better way to establish the seach path? Shouldn't the built-in stuff be findable via the default seach paths? Thanks a lot for your help, Robert Dodier From metawilm at gmail.com Sat May 5 07:16:49 2007 From: metawilm at gmail.com (Willem Broekema) Date: Sat, 5 May 2007 09:16:49 +0200 Subject: [clpython-devel] import fails due to null search paths in *habitat* In-Reply-To: References: Message-ID: On 5/5/07, Robert Dodier wrote: > I'm running CL-Python w/ Allegro Express 8.0 on Linux. > I find that import fails for anything except stuff in the > current working directory. That appears to be because > *habitat* has search paths = nil. I tried > (setf (habitat-search-paths *habitat*) ) > and that works OK for importing .py files but attempting > to import built-in stuff like sys fails. Setting the search path in sys.path should work: >>> import sys # >>> sys.path.append("~") None >>> import bar ; Fast loading /Users/willem/bar.fasl bar # >>> import math # > Is there some better way to establish the seach path? > Shouldn't the built-in stuff be findable via the default seach paths? Yeah, there was a typo in py-import, which used (excl:package-children :clpython.modules) while the correct package name was :clpython.module. Sorry for that, please update CVS and try again. Importing modules should now mostly work, but there is still an issue with importing mutually dependent modules. - Willem From metawilm at gmail.com Sat May 5 08:30:51 2007 From: metawilm at gmail.com (Willem Broekema) Date: Sat, 5 May 2007 10:30:51 +0200 Subject: [clpython-devel] calling Lisp from Python and vice versa In-Reply-To: References: Message-ID: Hi Robert, Thanks for trying out CLPython! On 5/5/07, Robert Dodier wrote: > I am interested in using CL-Python as a front end for Lisp > libraries. To this end, I wonder how to call a Lisp function > from withing Python. I know you can enter " (foo bar)" > to call a Lisp function, but its return value is not known to > Python. I'd like to be able to do something like "x = foo(bar)". In the repl the last 3 values are bound to variables _, __ and ___ (like *, ** and *** in Lisp). That is true for both Python and Lisp evaluations: >>> (car '(22)) 22 >>> x = _ 22 >>> The variables _ etc are both Python variables and Lisp special variables; so I often do the following to inspect a value returned by Python: >>> x = .... >>> (inspect _) But there is a problem if the return value is NIL, as a variable bound to NIL is CLPython's (efficient) way to mark it as unbound: >>> (or nil) nil >>> x = _ Error: NameError: Variable '_' is unbound [condition type: NameError] To make a Lisp function available in CLPython, probably the cleanest (and about the only) way is to create a subpackage of clpython.module and make the function external: (defpackage :clpython.module.foo (:use #:common-lisp) (:export #:mylen)) (defun clpython.module.foo:mylen (x) (length x)) Then you can do: >>> import foo # >>> foo.mylen( [1,2,3] ) 3 >>> x = foo.mylen( "abc") 3 >>> > Couple of questions related to this. How can I reference > Lisp variables from Python Make the variable external, like 'pi in :clpython.module.math Note that because Lisp packages can use symbols from other Lisp packages, you should easily be able to write a clpython.module.X package that is just a bridge between Python and a regular Lisp package. (Perhaps function cascade-external-symbols in package.lisp is useful here.) > and vice versa? Reference it like clpython.module.math:pi or do (use-package :clpython.module.math). > Can I use import to load a Lisp file? Currently not, "import" only works for Python source files and the corresponding fasl files. But it would be nice to have it work for regular Lisp files too, indeed. > After loading the Lisp file, how > do functions and variables in it appear in Lisp --- does the > Lisp package name come into play? Currently you can only reference symbols in direct subpackages of :clpython.module, see above. > What about Lisp symbols which contain characters not allowed in Python --- > is there a way to escape them? Python has no escape syntax for that; but you can use getattr: (defpackage :clpython.module.foo (:use #:common-lisp) (:export #:a-b*5)) (defun clpython.module.foo:a-b*5 () "you found it!") >>> import foo # >>> getattr( foo, "a-b*5")() 'you found it!' > Can a Lisp function return arbitrary Lisp > objects, and if so how are those displayed in Python? >>> (defun clpython.module.foo:a-b*5 () (list (copy-readtable) *standard-input* *random-state*)) >>> getattr( foo, "a-b*5")() (#, #, #) > Thanks a lot to Willem Broekma for inventing and maintaining > CL-Python. I think it's a great idea. My immediate interest > is to use CL-Python as an interface for the symbolic > computation system Maxima which is written in CL. I only played with Maxima a little bit, and a Python interface could be nice, indeed. Let me know if you need additional functionality from CLPython for this! - Willem From robert.dodier at gmail.com Sat May 5 15:25:27 2007 From: robert.dodier at gmail.com (Robert Dodier) Date: Sat, 5 May 2007 09:25:27 -0600 Subject: [clpython-devel] import fails due to null search paths in *habitat* In-Reply-To: References: Message-ID: Hi Willem, I'm still having trouble with import. I updated from CVS and I see some changes there. In a fresh ACL session I loaded CL-Python and called the repl. >>> import sys Error: ImportError: Could not find module `sys'. Search paths tried: "." [condition type: ImportError] Then [1] CLPYTHON(6): (habitat-search-paths *habitat*) NIL What else can I try? Thanks for your help. Robert Dodier From metawilm at gmail.com Sat May 5 21:50:21 2007 From: metawilm at gmail.com (Willem Broekema) Date: Sat, 5 May 2007 23:50:21 +0200 Subject: [clpython-devel] import fails due to null search paths in *habitat* In-Reply-To: References: Message-ID: On 5/5/07, Robert Dodier wrote: > Hi Willem, I'm still having trouble with import. > I updated from CVS and I see some changes there. > In a fresh ACL session I loaded CL-Python and called the repl. > > >>> import sys > Error: ImportError: Could not find module `sys'. > Search paths tried: "." > [condition type: ImportError] Ah, I checked in Modern mode, while you work in ANSI. (CLPython ought to work fine in both modes.) I reproduced it, and will fix it now. - Willem