My work in the area of NLP requires me to work with several frameworks across multiple languages such as Java, Python and Lisp. Sometime ago I got a chance to experiment with CLPython, an open-source implementation of Python in Common Lisp. Although CLPython is not under active development now, I found it quite usable. In fact it worked pretty well with both Allegro CL and LispWorks.

In today’s post, I would like to show some examples of this implementation.

You can install CLPython through Quicklisp:

For convenience, I created a package and imported symbols from this and standard packages.

(defpackage :pytest

(:use :cl :clos :clpython))

(in-package :pytest)

The first thing I did was to create a habitat – a runtime environment within which expression values are saved across Python calls. Although this is strictly not necessary, I felt that the interaction would be a bit tedious without it.

(setf *habitat* (make-habitat))

We are all set now. We can execute any Python code by enclosing it in double quotes and passing it to the Lisp function run. The following function assigns to a variable and modifies it:

(defun test1 ()

(run “x = 100”)

(run “x = x + 200”))

This is what you get when you run this function:

PYTEST 4 > (test1)

300

The answer is as expected. Note that if we had not initialized the default habitat, the variable x would not have been shared across the two different run statements. The sharing happens through the habitat.

OK. Let us define a Python function dynamically.

(defun test2 (k)

(run

(format nil

“def myfun (arg):

return arg * ~d” k)

))

The interesting thing about the above Lisp function is that it creates a Python function to make use of the argument supplied to it. So, for instance, the following call:

PYTEST 30 > (test2 50)

#<python-function myfun (interpreted) (__main__/myfun) 4030001CA4>

actually defines the following Python function:

def myfun(arg):

return arg * 50

That is, the argument supplied to test2 is embedded in the generated Python function!

The other neat thing is that we can call the generated Python function as if it is a Lisp function!

PYTEST 31 > (funcall (test2 50) 25)

1250

Isn’t that interesting?

You can do the same thing on a Python lambda too.

PYTEST 32 > (funcall (run “lambda x: x * x”) 50)

2500

What sbout Python functions that return multiple values? CLPython makes the result available as a list. Consider this example:

(defun test4 ()

(run

“def myfun ():

return 10, 20, 30″))

PYTEST 14 > (setf fn (test4))

#<python-function myfun (interpreted) (__main__/myfun) 4030000F14>

PYTEST 15 > (funcall fn)

(10 20 30)

Python lists can be accessed as vectors in Lisp.

PYTEST 33 > (setf res (run “[1, 2, 3, 4]”))

#(1 2 3 4)

We can use aref to access the individual elements of the vector.

The last feature we will look at is Python Dictionary.

PYTEST 34 > (setf table (run “dict = {‘a’: 30, ‘b’: 40}”))

#<PY-==->LISP-VAL Hash Table{2} 4020088B1B>

As you would have guessed, CLPython converts this to a hashtable in Lisp. This means we can access the Python dictionary elements as hashtable elements in Lisp.

PYTEST 7 > (setf (gethash “b” table) 60)

60

PYTEST 8 > (run “print dict”)

{‘a’: 30, ‘b’: 60}

PYTEST 9 > (setf (gethash “c” table) 123)

123

PYTEST 10 > (run “print dict”)

{‘a’: 30, ‘b’: 60, ‘c’: 123}

Not only are we able to modify the values in the dictionary, but we can also insert new elements!

That is it for today’s post. Hope you enjoyed it.

Have a great day!

Tags: ,
{"ft_lang_label":"__label__en","ft_lang_prob":0.81862456,"math_prob":0.80711466,"size":3486,"snap":"2023-14-2023-23","text_gpt3_token_len":942,"char_repetition_ratio":0.12004595,"word_repetition_ratio":0.009966778,"special_character_ratio":0.28743544,"punctuation_ratio":0.11292719,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95956266,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-05-29T15:18:58Z\",\"WARC-Record-ID\":\"<urn:uuid:a7a9de79-97e2-4476-913c-a3aa4a89f17f>\",\"Content-Length\":\"58948\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2a53c02f-ccbf-4895-aad8-5cbe468c219e>\",\"WARC-Concurrent-To\":\"<urn:uuid:b3ba9f4b-b6de-4f9a-a7c7-bd718e86bf83>\",\"WARC-IP-Address\":\"204.93.178.22\",\"WARC-Target-URI\":\"https://www.rangakrish.com/index.php/2017/07/07/clpython-python-in-common-lisp/\",\"WARC-Payload-Digest\":\"sha1:3N6DZNNGXR7DI4RH2VFFCJSBLRI6STI6\",\"WARC-Block-Digest\":\"sha1:4RD433DQHSY3E2GRGXZNG3DNBQR7KIEO\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224644867.89_warc_CC-MAIN-20230529141542-20230529171542-00529.warc.gz\"}"}