有没有一种简单的方法可以使用 Common Lisp 中的 Python 库?

发布于 2024-10-20 07:09:02 字数 184 浏览 2 评论 0原文

在编写 Common Lisp 代码时,我真正怀念的一件事是访问 Python 库,包括标准库和第三方模块。 CLPython 提供了 Python 功能的有限子集,这阻止了大多数库的使用,因此这对我来说并不是很有用。我希望能够从 Common Lisp 调用 Python 代码,以便它在 CPython 或 PyPy 等 Python VM 中运行。

One thing I really miss when writing Common Lisp code is access to Python libraries, both standard library and third party modules. CLPython provides a limited subset of Python functionality which precludes the use of most libraries, so that's not really useful to me. I would like to be able to call Python code from Common Lisp such that it runs in a Python VM like CPython or PyPy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蝶…霜飞 2024-10-27 07:09:02

(编辑)我们现在有 py4cl: https://github.com/bendudson/py4cl

Py4CL 是 Common Lisp 和 Python 之间的桥梁,它使 Common Lisp 能够与 Python 代码进行交互。它使用流与单独的 python 进程进行通信,这是 cl4py 采用的方法。这与被盗电池使用的 CFFI 方法不同,但目标相同。

下面是在 Lisp 程序中使用 Numpy 和 Scipy 的示例:

(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

您可能还喜欢 async-process ,它允许将代码发送到正在运行的 Python 进程。它用于 Lem 编辑器

使用示例:

CL-USER> (ql:quickload :async-process)
To load "async-process":
  Load 1 ASDF system:
    async-process
; Loading "async-process"
..................................................
[package async-process].
(:ASYNC-PROCESS)
CL-USER> (in-package async-process)
#<PACKAGE "ASYNC-PROCESS">
ASYNC-PROCESS> (create-process "python")
#.(SB-SYS:INT-SAP #X7FFFEC002830)
ASYNC-PROCESS> (defparameter p *)
#.(SB-SYS:INT-SAP #X7FFFEC002830)
ASYNC-PROCESS> (process-receive-output p)
"Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>> "
ASYNC-PROCESS> (process-send-input p "1+1
")
; No value
ASYNC-PROCESS> (process-receive-output p)
"1+1
2
>>> "

(edit) We now have py4cl: https://github.com/bendudson/py4cl

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 cl4py. This is different to the CFFI approach used by burgled-batteries, but has the same goal.

Here's its example using Numpy and Scipy from a Lisp program:

(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

You might also like async-process, which allows to send code to a running Python process. It is used in the Lem editor.

Example use:

CL-USER> (ql:quickload :async-process)
To load "async-process":
  Load 1 ASDF system:
    async-process
; Loading "async-process"
..................................................
[package async-process].
(:ASYNC-PROCESS)
CL-USER> (in-package async-process)
#<PACKAGE "ASYNC-PROCESS">
ASYNC-PROCESS> (create-process "python")
#.(SB-SYS:INT-SAP #X7FFFEC002830)
ASYNC-PROCESS> (defparameter p *)
#.(SB-SYS:INT-SAP #X7FFFEC002830)
ASYNC-PROCESS> (process-receive-output p)
"Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>> "
ASYNC-PROCESS> (process-send-input p "1+1
")
; No value
ASYNC-PROCESS> (process-receive-output p)
"1+1
2
>>> "
疧_╮線 2024-10-27 07:09:02

一种解决方案是 python-on-lisp。它应该可以通过 ASDF 安装。它已经有几年没有维护或更新了,所以可能有更好的东西可用。

One solution is python-on-lisp. It should be ASDF-installable. It hasn't been maintained or updated for a couple years, so there may be something better available.

女皇必胜 2024-10-27 07:09:02

您可能想尝试 burgled-batteries,它是 Python 和 Lisp 之间的桥梁(FFI 绑定等)。 )。

从描述来看,“burgled-batteries 提供了 Python(特别是 Python 的 CPython 实现)和 Common Lisp 之间的填充程序。”

You may want to try burgled-batteries, a bridge between Python and Lisp (FFI bindings, etc.).

From the description, "burgled-batteries provides a shim between Python (specifically, the CPython implementation of Python) and Common Lisp."

如梦初醒的夏天 2024-10-27 07:09:02

我建议为您的代码编写一个“暴露器”接口,该接口接受文本并写入文本,以便您可以在命令行上调用它。理想情况下,使用典型的 STDIN |标准输出方法。

我相信这通常是非性能应用程序的最佳方法。

I would suggest writing an "exposer" interface for your code that takes text and writes text such that you can call it on the command line. Ideally, with a typical STDIN | STDOUT approach.

I believe that is typically the best approach for non-performance applications.

掌心的温暖 2024-10-27 07:09:02

我知道这是不同的语言,但为什么不尝试 Clojure 和 Jython :) : )

也许你会发现自己恰好在你想要的地方。

I know this are different languages, but why don't you give Clojure and Jython a try :) :)

Perhaps you find your self exactly where you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文