Is there a simple way to use Python libraries from Common Lisp?

前端 未结 5 2632
闹比i
闹比i 2021-02-20 03:35

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

相关标签:
5条回答
  • 2021-02-20 03:55

    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.

    0 讨论(0)
  • 2021-02-20 03:58

    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.

    0 讨论(0)
  • 2021-02-20 04:04

    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."

    0 讨论(0)
  • 2021-02-20 04:11

    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.

    0 讨论(0)
  • 2021-02-20 04:13

    (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
    >>> "
    
    • https://github.com/CodyReichert/awesome-cl#python
    0 讨论(0)
提交回复
热议问题