Python 3.5 b2 out for testing – https://www.python.org/downloads/release/python-350b2/
Using Pythons matplotlib
I had a look at the python 2D plotting library matplotlib. For more information about this library, you might want to have a look at the corresponding Wikipedia article.
I’m looking for a way to make simple plots from x,y value pairs and it seems that matplotlib is the way to go in Python environments. Let’s have a look at an example. The following script will save a simple bar plot to a PNG file. Nothing fancy, just something to have a starting point.
import matplotlib.pyplot as plt import numpy as np data = (1, 2, 3, 4, 5) pos = np.arange(len(data)) plt.bar(pos, data) plt.xticks(pos+0.4, data) plt.savefig("bar.png")
If you have matplotlib correctly installed in your Python environment, it will generate the following bar plot in a PNG file called “bar.png” in the current directory.
Please not that the script generates just the plot, not the subtitle. While you can do this in matplotlib, I have added the subtitle here in HTML for simplicity. I have decided to have a further look at matplotlib. It looks promising and I would like to use it inside a simple web application. Let’s see how far this goes.
common-lisp.net to use gitlab
CMUCL announcement: “common-lisp.net has been slowing transitioning services to use gitlab. The transition is pretty much done, and cmucl is now officially at https://gitlab.common-lisp.net/cmucl/cmucl. The ticket system and wiki is still using https://trac.common-lisp.net/cmucl, but we hope to migrate that to gitlab as well.”
Dynamic Languages Symposium 2015 Call For Papers
ESUG 2015 in Brescia, Italy – July 13-17
Steel Bank Common Lisp 1.2.11, releas…
Steel Bank Common Lisp 1.2.11, released April 27, 2015 – http://sbcl.sourceforge.net/news.html#1.2.11 #lisp
Thoughts on creating objects in Lisp
If you work with classes in Lisp, you might want to hide the details of creating an object of a class. This practise is generally known as the factory method pattern. I’ll show a very simple example.
Let’s assume, you have defined the following class.
(defclass noun () ((es :accessor noun-spanish) (es-gender :accessor noun-spanish-gender) (de :accessor noun-german) (de-gender :accessor noun-german-gender)))
To create an object of this class, you have to call make-instance and setf the attributes (slots). To avoid writing this again and again, you can define the following function.
(defun create-noun (es es-gender de de-gender) (let ((n (make-instance 'noun))) (progn (setf (noun-spanish n) es) (setf (noun-spanish-gender n) es-gender) (setf (noun-german n) de) (setf (noun-german-gender n) de-gender) n)))
You can then use this function to create objects of your class.
CL-USER> (defparameter *noun* (create-noun 'falda 'f 'Rock 'm)) *NOUN* CL-USER> (noun-german *noun*) ROCK CL-USER> (noun-spanish *noun*) FALDA CL-USER>
While this is a very simple application of a factory method (or function), you might still find it useful.
23rd International Smalltalk Joint Conference – Call for Contributions
Defining Lisp classes in packages
In Lisp, when you define classes in packages, you have to remember that you have to explicitly export the accessors too. For example, you define the following package with a class inside.
(defpackage :mhn-foo (:use :common-lisp) (:export #:fooclass)) (in-package :mhn-foo) (defclass fooclass () ((fooatt :accessor fooattribute)))
However, while you can instantiate the class, you get an error message when you try to access the attribute of the class.
CL-USER> (defparameter *fooparameter* (make-instance 'mhn-foo:fooclass)) *FOOPARAMETER* CL-USER> (setf (mhn-foo:fooattribute *fooparameter*) "foo") ; Evaluation aborted on #<SB-INT:SIMPLE-READER-PACKAGE-ERROR "The symbol ~S is not external in the ~A package." {1005FDFC03}>.
To be able to access the attribute of the class, you have to export it.
(defpackage :mhn-foo (:use :common-lisp) (:export #:fooclass #:fooattribute))
Now you can work with the attribute of the class without problems.
CL-USER> (defparameter *fooparameter* (make-instance 'mhn-foo:fooclass)) *FOOPARAMETER* CL-USER> (setf (mhn-foo:fooattribute *fooparameter*) "foo") "foo" CL-USER> (mhn-foo:fooattribute *fooparameter*) "foo"
This is not very different in other programming languages. For example, in Java and C#, you define visibility not only for the class, but for the class members too. If you forget to set visibility for class members, you might have problems accessing them.
Making decisions
It is interesting to have a look at how you make decisions in different programming languages. In Python, for example, the code is straightforward.
>>> if 1 < 2:
… print ‘smaller’
… else:
… print ‘bigger’
…
smaller
>>>
Serializing Lisp lists
In Lisp, it is very easy to serialize and deserialize lists into files. You can use the macros with-open-file and print for this. Using macro with-open-file makes sure that the file is closed automatically even in case of failures. The print macro prints lists in a way that they can be read by the Lisp reader. For example, you define the following function for serialization.
(defun serialize (file-name list)
(with-open-file (stream file-name :direction :output)
(print list stream)))
You might want to use keyword argument :if-exists :supersede if you want to overwrite existing files. Add another function for deserialization. It uses the read function to read the list back from the file.
(defun deserialize (file-name)
(with-open-file (stream file-name :direction :input)
(read stream)))
Now you can use these two functions to save your lists to file and read them back in. I have defined the two functions in a package called mhn-serial.
CL-USER> (mhn-serial:serialize “~/tmp/list.ser” ‘(1 2 3))
(1 2 3)
CL-USER> (mhn-serial:deserialize “/home/neifer/tmp/list.ser”)
(1 3 4)
CL-USER> (car (mhn-serial:deserialize “/home/neifer/tmp/list.ser”))
1
CL-USER> (cdr (mhn-serial:deserialize “/home/neifer/tmp/list.ser”))
(3 4)
CL-USER> (nth 2 (mhn-serial:deserialize “/home/neifer/tmp/list.ser”))
4
CL-USER> (mhn-serial:deserialize “/home/neifer/tmp/list.ser”)
((1 2 3) (4 5 6))
CL-USER> (nth 1 (car (cdr (mhn-serial:deserialize “/home/neifer/tmp/list.ser”))))
5
CL-USER>
Please note that I have edited the file between the calls. I have changed the file content from (1 2 3) to (1 3 4) and then to ((1 2 3)(4 5 6)). Just to check if reading works. You could change the content to something more interesting, of course.
CL-USER> (mhn-serial:deserialize “/home/neifer/tmp/list.ser”)
(+ 2 3)
CL-USER> (eval (mhn-serial:deserialize “/home/neifer/tmp/list.ser”))
5
CL-USER>
You see that you have to be very careful when using eval on objects that you have read from another source. The bad guys could easily compromise your system.
JSON in Python and Common Lisp
Both, Python and Common Lisp have libraries for writing and reading JSON format. JSON can be used as an alternative to XML, when not only machines will read the data. XML can be hard to read when you have more tags than data. JSON does not have the overhead of start and end tags.
In Python, you can import module json from the Python standard library.
>>> import json
>>> a = [1, 2, 3]
>>> json.dumps(a)
‘[1, 2, 3]’
>>> json.loads(‘[1, 2, 3]’)
[1, 2, 3]
In Common Lisp, you can use the CL-JSON library.
CL-USER> (ql:quickload “CL-JSON”)
CL-USER> (cl-json:encode-json ‘(1 2 3))
[1,2,3]
NIL
CL-USER> (with-input-from-string (s “[1, 2, 3]”) (cl-json:decode-json s))
(1 2 3)
These are just very simple examples, but you see that usage is straightforward. If you don’t like the syntax of JSON, you might want to have a look at YAML. JSON is a subset of YAML version 1.2.
Regular Expressions in Lisp
There are various different regular expression libraries available for Lisp. A nice overview can be found at CLiki. I had a look at the Portable Perl-compatible regular expressions for Common Lisp (CL-PPCRE). Installation is very easy using Quicklisp. After installation you can add the library to your image as follows.
(ql:quickload “cl-ppcre”)
Then you can use the library in your code. Let’s define a very simple function to extract some text from a given string.
(defpackage :mhn-regex
(:use :common-lisp :cl-ppcre)
(:export #:print-match))
(in-package :mhn-regex)
(defun print-match ()
(let ((a 0) (b 0))
(setf (values a b) (cl-ppcre:scan-to-strings ” ([a-z]+) ” “My text string.”))
(print (aref b 0))))
Regular Expressions: Groups
In Python, you can write the following, to capture groups of characters with regular expressions.
>>> import re
>>> print(“Match is ‘”
… + re.search(‘\\s([a-z]+)\\s’,
… ‘My text string.’).group(1) + “‘”)
Match is ‘text’
>>>