在编写 Common Lisp 代码时,我真正想念的一件事是访问 Python 库,包括标准库和第三方模块。 CLPython 提供了一个有限的 Python 功能子集,它排除了大多数库的使用,所以这对我来说并不是很有用。我希望能够从 Common Lisp 调用 Python 代码,使其在 CPython 或 PyPy 等 Python VM 中运行。
最佳答案
(编辑)我们现在有了 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.
这是它在 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 editor .
使用示例:
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
>>> "
关于python - 有没有一种简单的方法可以使用 Common Lisp 中的 Python 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5174199/