From: Jeremy Smith
Subject: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <Xns9727C9ECC9549jeremyalansmithsofth@62.253.170.163>
Hi,

I had an idea earlier, after reading the various threads and articles on 
Reddit's switch to Python. The problem, people say, is that Lisp has a 
library deficiency, so why not call Python's library code from Lisp?

A while ago, I needed to use Python as an embedded scripting language, so I 
found out how to do this. Shelling out to Python was not an option because 
it's grossly inefficient (and not Windows-friendly), but Python is actually 
embeddable, in a fashion where it is called from a DLL, and remains 
resident in memory until Lisp wants to execute Python code (as text).

Here are some details:

    	*Speed

Once you start Python from the DLL, it initialises Python just once, so all 
you have to do after that is run code. The overhead for execution is 
minimal.

    	*Return values

A callback function may be required, which takes the return value and puts 
it into a string or integer (in Lisp). I'm not sure about passing and 
returning structures.

     *Stdout

You can redirect stdout to a callback function (which can append it to a 
list) with a bit of Python code, executed before the desired code.

---

Now, this is promising, because it means you can do everything in Lisp, but 
call out seamlessly to Python when you want to do things like FTP, HTTP, 
and other stuff.

Why would anyone use Lisp when they could do it all in Python? Well, I have 
a project which would probably not run particularly fast in Python. It runs 
very fast in C++, but I prefer to use Lisp over C++ due to its many 
features (dynamic typing is very important in my project due to the tree-
like data structure), so Lisp is the second best for speed.

However, if you could call Python libraries from Lisp, you get the best of 
both worlds. You get Lisp's speed and features, and Python's extensive  
Internet protocol libraries, with no more CPU overhead than if you called 
those libraries from Python. You get the portability of Python; because it 
uses a dynamically linked library approach, you can use it with any Lisp on 
most platforms.

For instance, if you just want to run Python code without the libraries, in 
Windows, all you need is Python22.dll.

Okay, that's my reasons over with. I would like to implement this in an 
open fashion (ie, here's the source files, do what you want with it), but 
here are some questions before I re-invent the wheel:

    	*What utilities are there to convert the Python header files to FFI?
    	*Can I link with the python22.lib from FFI, or do I just call the DLL 
directly from FFI?
    	*Callbacks would have to be passed as pointers to functions. Is this 
possible in Lisp?
    	*Would anybody use this?
    	*Has this been done before?
    	*How about the name "LisPy"?

Cheers,

Jeremy.

From: Marco Antoniotti
Subject: Re: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <1134160975.554511.88220@g43g2000cwa.googlegroups.com>
Jeremy Smith wrote:
> Hi,
>
> I had an idea earlier, after reading the various threads and articles on
> Reddit's switch to Python. The problem, people say, is that Lisp has a
> library deficiency, so why not call Python's library code from Lisp?

The notion that there is a library deficiency for Common Lisp is quite
disproved these days.  Chances are, the library you need is just a
Google or a question on C.L.L. away.

> A while ago, I needed to use Python as an embedded scripting language, so I
> found out how to do this. Shelling out to Python was not an option because
> it's grossly inefficient (and not Windows-friendly), but Python is actually
> embeddable, in a fashion where it is called from a DLL, and remains
> resident in memory until Lisp wants to execute Python code (as text).
>
> Here are some details:
>
>     	*Speed
>
> Once you start Python from the DLL, it initialises Python just once, so all
> you have to do after that is run code. The overhead for execution is
> minimal.
>
>     	*Return values
>
> A callback function may be required, which takes the return value and puts
> it into a string or integer (in Lisp). I'm not sure about passing and
> returning structures.
>
>      *Stdout
>
> You can redirect stdout to a callback function (which can append it to a
> list) with a bit of Python code, executed before the desired code.
>
> ---
>
> Now, this is promising, because it means you can do everything in Lisp, but
> call out seamlessly to Python when you want to do things like FTP, HTTP,
> and other stuff.
>
> Why would anyone use Lisp when they could do it all in Python?

Because doing it directly in Lisp is way better?  :)

> Well, I have
> a project which would probably not run particularly fast in Python. It runs
> very fast in C++, but I prefer to use Lisp over C++ due to its many
> features (dynamic typing is very important in my project due to the tree-
> like data structure), so Lisp is the second best for speed.
>
> However, if you could call Python libraries from Lisp, you get the best of
> both worlds. You get Lisp's speed and features, and Python's extensive
> Internet protocol libraries, with no more CPU overhead than if you called
> those libraries from Python. You get the portability of Python; because it
> uses a dynamically linked library approach, you can use it with any Lisp on
> most platforms.

Not quite.  In order to link in the Python interpreter (and, again, why
would you want to do that in any case?) you need a FFI, which, given
the current state of affairs will give you more or less protability.

> For instance, if you just want to run Python code without the libraries, in
> Windows, all you need is Python22.dll.
>
> Okay, that's my reasons over with. I would like to implement this in an
> open fashion (ie, here's the source files, do what you want with it), but
> here are some questions before I re-invent the wheel:
>
>     	*What utilities are there to convert the Python header files to FFI?
>     	*Can I link with the python22.lib from FFI, or do I just call the DLL
> directly from FFI?
>     	*Callbacks would have to be passed as pointers to functions. Is this
> possible in Lisp?

See my comment above.

>     	*Would anybody use this?

I very much doubt it.

>     	*Has this been done before?

Looks like nobody needed anything like this.

>     	*How about the name "LisPy"?

What about just using Lisp?

Cheers
--
Marco
From: Magnus Henoch
Subject: Re: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <87y82ton5v.fsf@zemdatav.stor.no-ip.org>
Jeremy Smith <············@decompiler.org> writes:

> I had an idea earlier, after reading the various threads and articles on 
> Reddit's switch to Python. The problem, people say, is that Lisp has a 
> library deficiency, so why not call Python's library code from Lisp?

I had another idea (which I definitely won't have time nor energy to
implement).  If Python is a subset of Lisp, you could write a py2cl
translator and run Python code in a Lisp image.

Magnus
From: Edi Weitz
Subject: Re: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <u7jadsuan.fsf@agharta.de>
On Fri, 09 Dec 2005 23:39:40 GMT, Magnus Henoch <·····@freemail.hu> wrote:

> I had another idea (which I definitely won't have time nor energy to
> implement).  If Python is a subset of Lisp, you could write a py2cl
> translator and run Python code in a Lisp image.

This has already been done.  I saw this at a semi-private presentation
so I'm not sure if I'm allowed to talk about the details.

Cheers,
Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Jens Axel Søgaard
Subject: Re: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <439ab7e5$0$38616$edfadb0f@dread12.news.tele.dk>
Magnus Henoch wrote:

> I had another idea (which I definitely won't have time nor energy to
> implement).  If Python is a subset of Lisp, you could write a py2cl
> translator and run Python code in a Lisp image.

You might find these interesting then:

     <http://plt-spy.sourceforge.net/documentation.html>
     <http://plt-spy.sourceforge.net/home.html>

-- 
Jens Axel S�gaard
From: Jeremy Smith
Subject: Re: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <Xns9728D41B7A53Ajeremyalansmithsofth@80.5.182.99>
Jens Axel S�gaard <······@soegaard.net> wrote in news:439ab7e5$0$38616
·········@dread12.news.tele.dk:

> Magnus Henoch wrote:
> 
>> I had another idea (which I definitely won't have time nor energy to
>> implement).  If Python is a subset of Lisp, you could write a py2cl
>> translator and run Python code in a Lisp image.
> 
> You might find these interesting then:
> 
>      <http://plt-spy.sourceforge.net/documentation.html>
>      <http://plt-spy.sourceforge.net/home.html>
> 

I see you Reddit'ed the link (it's on the main page). Finding Lisp's post 
is high on their list. Perhaps Reddit and the Lisp community are now 
irrecovably bound, because of the split.

Anyway, the thing about the conversion approach is that it ignores the fact 
that many Python libraries are actually compiled C. My approach is simpler, 
and no less efficient in terms of speed of execution, and would work fine.

Cheers,

Jeremy.
From: Jeremy Smith
Subject: Re: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <Xns9728D4B186880jeremyalansmithsofth@80.5.182.99>
Jeremy Smith <············@decompiler.org> wrote in
·········································@80.5.182.99: 

> Anyway, the thing about the conversion approach is that it ignores the
> fact that many Python libraries are actually compiled C. My approach
> is simpler, and no less efficient in terms of speed of execution, and
> would work fine. 

Sorry, I wrote that a bit nastily and I don't want it to come across as a 
flame.

I should have said: Conversion is okay for the Python code itself, but some 
of the base libraries are written in C and compiled as part of Python, for 
maximum speed and efficiency. Those wouldn't be a problem if you link with 
the Python DLL, and run code from within it.

Regards,

Jeremy.
From: Jens Axel Søgaard
Subject: Re: Lisp libraries - How about calling Python from Lisp?
Date: 
Message-ID: <439ab7e5$0$38619$edfadb0f@dread12.news.tele.dk>
Magnus Henoch wrote:

> I had another idea (which I definitely won't have time nor energy to
> implement).  If Python is a subset of Lisp, you could write a py2cl
> translator and run Python code in a Lisp image.

You might find these interesting then:

     <http://plt-spy.sourceforge.net/documentation.html>
     <http://plt-spy.sourceforge.net/home.html>

-- 
Jens Axel S�gaard