-
Notifications
You must be signed in to change notification settings - Fork 12
5.18 The Python Library: pylispe
Claude Roux edited this page Mar 26, 2025
·
4 revisions
This library is both a LispE library and a python library. It offers the possibility either to execute python code in LispE or LispE code in python, using the same library.
Here are the functions that are exposed by pylispe
:
(deflib python() ; create a python interpreter for LispE)
(deflib python_run(py code (variable "") (timeout -1)) ; run a piece of code and returns the result in an optional variable, a timeout can be set as well)
(deflib python_runmodule(py name code (variable "")) ;run a module, with the final value in an optional variable)
deflib python_getmodule(py name (variable "")) ; load a python module);
deflib python_runfile(py path) ; execute a Python file)
deflib python_setpath(py path) ; set a path to find Python libraries)
deflib python_import(py path) ; import a module or a library)
deflib python_execute(py name arguments) ; execute a function)
deflib python_simple(py code) ; use python_simple to execute code)
deflib python_close(py) ; close the python interpreter)
; This code executes some Python instructions
(use 'pylispe)
; we create a python intepreter
(setq py (python))
;We execute some code
(setq res (python_run py "i = 100 + 20" "i"))
(println res) ; 120 since the calculus was stored in "i" and we ask "i" as the return variable
(python_run py "print('Within:',i)")
; We define a Python function
; as a LispE string
(setq myFunc
`def toto(i):
return i+10;
`
)
; we store our function definition
(python_run py myFunc)
; which we can execute and retrieve a result from it
(println (python_execute py "toto" '(11)))
Here the code that can be executed from within Python:
# This code executes some LispE code within Python
import pylispe
# we create a lisp interpreter
a=pylispe.lisp()
# We load the current file
print(a.load("called_in_python.lisp"))
# an evaluation
print(a.eval("(setq d 10)"))
# We can actually call an instruction or an operator directly
# Note the '".."' structure to pass strings to LispE, while
# atoms are passed as simple strings: the 'd' is the atom
# that was created above in the 'setq' instruction
print(a.execute("+", '"test"', 'd'))
# We can also use the add function that has been implemented in called.lisp
# The 'd' is again the one that was declared above with the value 10
print(a.execute("add", "d", 31.654))