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.

Simple bar plot
Simple bar plot

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

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.

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

Maybe I’m wrong but I think that you don’t need programming experience to have an idea what is going on here. Let’s have a look at the same decision in Lisp.
CL-USER> (if (< 1 2) ‘smaller ‘bigger)
SMALLER
CL-USER> 
I’m not sure if everybody can immediately tell how it works. The code is very concise and clear. But it doesn’t help in this case. I don’t think the parentheses are the problem. Maybe it is because the ‘else’ is missing and you need the additional information about how else is handled here.
On the other hand, if the code is more descriptive, it can be a problem too. Let’s have a look at our decision in Squeak Smalltalk.
b := (1 < 2) ifTrue: [‘smaller’] ifFalse: [‘bigger’].
Transcript show: b; cr.
Maybe it is because of the message chaining that I need a bit more time to fully understand the code snippet. Or maybe I just did too much programming in C-style languages.

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))))

As you can see, using the library is straightforward in this case. I’ll try to find some free time and create some more examples.

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’
>>> 

This is quite straightforward. In C#, you can write something similar.
using System.Text.RegularExpressions;
namespace MhNeifer.Samples.CSharp {
    public class MyRegex {
        static void Main() {
            Regex rgx = new Regex(@”\s([a-z]+)\s”);
            System.Console.WriteLine(“Match is ‘”
                           + rgx.Matches(“My text string.”)[0].Groups[1].Value
                           + “‘”);
        }
    }
}
If you ignore that C# is more wordy in general (namespace and class definition and all this), this is straightforward as well.
I thought that in Java it would be straightforward too. But it seems that there’s a catch. Or I’m too dumb to see the simple solution. I found the following.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(“.*\\s([a-z]+)\\s.*”);
        Matcher m = p.matcher(“My text string.”);
        m.matches();
        System.out.println(“Match is ‘” + m.group(1) + “‘”);
    }
}
While it looks straightforward, it is not. You have to call Matcher.matches() before Matcher.group(), or you get an exception. I was surprised by this. Please note the ‘.*’ at the beginning and the end of the regular expression. You have to write a regular expression that matches the whole string. For me, this took a while to remember.