如何将Lisp程序的输出输出到Python中? [英] How to get output of a Lisp program into Python?

查看:113
本文介绍了如何将Lisp程序的输出输出到Python中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的Lisp项目,我希望将其输出以编程方式传递到Python程序,即使用Python在某些输入上调用Lisp程序,然后将输出返回到Python.

I've got a very large Lisp project whose output I'd like to programmatically pipe to a Python program, i.e. use Python to call the Lisp program on some input and get the output back into Python.

该项目仅在Clozure Common Lisp(ccl64)中进行编译,我确实试图找到一种方法将其转换为可执行文件(我使用的是Mac OS X),但是却遇到了很多死胡同(我是不是Lisp程序员).

The project only compiles in Clozure Common Lisp (ccl64) and I did try to find a way to turn it into an executable (I'm using Mac OS X), but that ran into a lot of dead ends (I am not a Lisp programmer).

此Clozure Common Lisp的文档应提供上述解决方案,但我无法理解.我创建的示例创建了一个文件,但是Terminal不会将它们作为可执行文件运行.

This documentation for Clozure Common Lisp should provide the solution to the above, but I was not able to understand it. The examples I made created a file, but Terminal would not run them as executables.

如何为ccl64创建可执行文件

我试图遵循此问题的答案将Common Lisp编译为可执行文件除了使用ccl64的保存应用程序功能.

I tried to follow this question's answer Compiling Common Lisp to an executable except using ccl64's save application function.

$ ccl64
Welcome to Clozure Common Lisp Version 1.9-dev-r15612M-trunk  (DarwinX8664)!
? (in-package :ccl)  
#<Package "CCL">
? (defun main () (print "hello"))
MAIN
? (save-application "hello" :toplevel-function #'main)

我正在尝试使用Python的子进程来调用ccl64,运行Lisp程序并获取输出.但是,子进程由于某种原因拒绝运行ccl64命令.这是我到目前为止写的:

I am trying to use Python's subprocess to invoke ccl64, run the Lisp program, and get the output. However, subprocess for some reason refuses to run the ccl64 command. Here is what I wrote so far:

import subprocess

process = subprocess.Popen(['ccl64', '-h'], stdout=subprocess.PIPE)
out, err = process.communicate()

变量out应该包含从ccl64获取用法/帮助的输出.相反,我得到一个错误:

The variable out should contain the output of getting the usage/help from ccl64. Instead I get an error:

Traceback (most recent call last):
  File "sub.py", line 3, in <module>
    process = subprocess.Popen(['ccl64', '-h'], stdout=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

如何获取Python来调用ccl64并从Lisp项目获取输出?

How can I get Python to invoke ccl64 and get output from the Lisp project?

推荐答案

Python代码中的错误很明显:没有这样的文件或目录.

The error in your Python code is clear: No such file or directory.

您需要在Python代码中告诉您要以实际找到它的方式运行哪个应用程序.

You need to tell in your Python code which application you want to run in a way that it actually finds it.

也不清楚为什么将Lisp可执行文件保存在名为hello的位置,但是您没有尝试调用它.具有必要的路径.您的代码尝试调用Clozure CL-没有必要的路径-但是为什么呢?您刚刚保存了一个可执行文件.您为什么要致电Clozure CL来运行它?我还将使用在内核之前保存可执行文件-这使它成为独立的.

It's also not clear why you save a Lisp executable somewhere named hello, but you are not trying to call it. With the necessary path. Your code tries to call Clozure CL - without the necessary path - but why? You just saved an executable. Why would you call Clozure CL to run it? I would also save the executable with prepending the kernel - that makes it self-contained.

示例:

致电Clozure CL:

Calling Clozure CL:

rjmba:~ joswig$ ccl
Welcome to Clozure Common Lisp Version 1.9-dev-r15612M-trunk  (DarwinX8664)!

定义main函数:

? (defun main () (print "hello"))
MAIN

保存可执行文件:

? (save-application "hello" :toplevel-function #'main :prepend-kernel t)

从同一目录运行新的可执行文件:

Running the new executable from the same directory:

rjmba:~ joswig$ ./hello

"hello" 

使用以下参数调用Clozure CL应用程序:

bash-3.2$ ccl
Welcome to Clozure Common Lisp Version 1.9-dev-r15612M-trunk  (DarwinX8664)!

函数ccl::command-line-arguments将参数作为列表返回.第一项是被调用的应用程序本身.

The function ccl::command-line-arguments returns the arguments as a list. The first item is the called application itself.

? (defun main ()                                                                
    (print (second (ccl::command-line-arguments))))
MAIN

? (save-application "hello"                                                     
                    :toplevel-function #'main                                   
                    :prepend-kernel t)

调用:

bash-3.2$ ./hello hello!

"hello!"

这篇关于如何将Lisp程序的输出输出到Python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆