Package basics

Packages form the basic building blocks of software components in many programming languages. For example, in Java you use the package statement to define the package for a class as follows.

package a.b;
public class C {
    public static void main(String[] args) {
System.out.println(“hello world”);
    }
}

The dot is used in package names to create sub-packages. However, in Java you have to create a directory tree to mirror your package hierarchy. So the file containing the example class C must be in a directory b that has a parent directory a. In the parent directory of a, you can start the compiler and interpreter as follows.

~/data/code/java/> javac a/b/C.java
~/data/code/java/> java a.b.C
hello world

Please note that you have to use slashes for the compiler and dots for the interpreter. Of course, an IDE takes care of this for you. Further information about Java packages can be found in the Java tutorial.

Python packages are somewhat similar to Java packages in that the package hierarchy is mirrored as a directory tree. But the package directories need special marker files __init__.py for Python to recognize them as package directories. So the following directory tree creates a simple package hierarchy.

a/
    __init__.py
    b/
        __init__.py
        c.py

The __init__.py files can be empty or contain package initialization code. If file c.py contains the following code

def main():
    print “hello world”

you can use the function main() as follows

>>> import a.b.c
>>> a.b.c.main()
hello world

Further information about Python packages can be found in the Python tutorial.

In C# you don’t need to mirror your package hierarchy with directories. You just create a file wherever you want and use the namespace statement to create packages.

namespace a.b {
    public class C {
        static void Main() {
            System.Console.WriteLine(“hello world”);
        }
    }
}

Again you can use the dot to create sub-packages. There’s no need to create a directory tree or take care of the package when compiling.

C:\Data\Code\C-Sharp\> csc c.cs
C:\Data\Code\C-Sharp\> c.exe
hello world

Further information about namespaces can be found at MSDN.

In Lisp, using packages is a bit different. First you define the package, then you change your current package to the defined package and then you define your functions or whatever you need in that package.

(defpackage :mhnfoo
  (:use :common-lisp)
  (:export #:printadd))
  
(in-package :mhnfoo)

(defun printadd (x y) (print (+ x y)))

This works when you save the commands in a file or in the REPL.

CL-USER> (mhnfoo:printadd 1 2)


3

Further information about packages can be found in the Common Lisp HyperSpec.

Unit testing in Lisp

There are some frameworks and libraries available for doing unit tests in Common Lisp. You’ll find a list for example in Wikipedia.

I have decided to have a look at lisp-unit from Chris Riesbeck, Associate Professor in the Department of Electrical Engineering and Computer Science at Northwestern University.

It is very easy to use lisp-unit. You define a package for your tests and use the lisp-unit package to have access to the defined assertion forms. In your defined package you define your tests that use the functions you want to test. lisp-unit supports test-driven development, so in your tests, you can use functions that are not defined yet. The following will test a function named ‘add’.

(defpackage :mhnfoo-tests
  (:use :common-lisp :lisp-unit :mhnfoo))

(in-package :mhnfoo-tests)

(define-test mhnfoo
  (assert-true (eql 3 (add 1 2))))

Here is a very simple implementation of the function ‘add’.

(defpackage :mhnfoo
  (:use :common-lisp)
  (:export #:add))

(in-package :mhnfoo)

(defun add (x y) (+ x y))

Now you can run all the tests in your test package as follows. I’m using CLISP on Microsoft Windows 7 for this example. The ‘[6]>’ and ‘[7]>’ are the prompts of CLISP.

[6]> (lisp-unit:run-all-tests :mhnfoo-tests)
MHNFOO: 1 assertions passed, 0 failed.

[7]>

As you see, it is very easy to do unit testing in Common Lisp. And lisp-unit is just one file to load.

Unit testing in Java and C#

Unit testing has become a cornerstone of modern software development. Some of the most popular frameworks for unit testing are collectively known as xUnit. Originally developed for Smalltalk, there are now xUnit frameworks available for many programming languages. The current version 4 of the popular JUnit framework for the Java programming language uses annotations to mark test methods. A very simple test might look as follows.

public class SimpleTest {
    @Test
    public void testSimpleClass() {
        SimpleClass s = new SimpleClass();
        Assert.assertEquals(“hello, world”, s.getGreeting());
    }
}
public class SimpleClass {
    public String getGreeting() {
        return “hello, world”;
    }
}

JUnit provides a simple test runner to run your tests. You can run tests at the command line as follows.

neifer@linux-2qs4:~/code/java> java -cp .:junit-4.11.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore junit.SimpleTest
JUnit version 4.11
.
Time: 0,005

OK (1 test)

Assert.assertEquals gives an exception, if something is wrong. Most modern IDEs have build-in support for JUnit, of course. If you work with the Microsoft .NET platform, you might use the NUnit framework for unit tests. The annotations are called attributes here, but usage is very similar.

[TestFixture] 
class SimpleTest {
    [Test]
    public void testSimpleClass() {
        SimpleClass s = new SimpleClass();
        Assert.AreEqual(“hello, world”, s.getGreeting());
    }
}
class SimpleClass {
    internal string getGreeting() {
        return “hello, world”;
    }
}

NUnit too provides a simple test runner to run your tests at the command line. A run looks like this.

C:\NUnit>NUnit-2.6.3\bin\nunit-console.exe simpleTest.dll
NUnit-Console version 2.6.3.13283
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment – 
   OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1
  CLR Version: 2.0.50727.5477 ( Net 3.5 )

ProcessModel: Default    DomainUsage: Single
Execution Runtime: net-3.5
.
Tests run: 1, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0,0570119997477751 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

Some notes on lists in Lisp

As you have seen in my previous post, working with lists is easy if lists are build into a programming language. But what if the language itself is made of lists? The Lisp programming language is made of lists. Let’s have a look at what it’s like to work with lists in Lisp. I use the SBCL implementation of ANSI Common Lisp on openSUSE Linux for the examples.

You can create a list as follows.

CL-USER> (defparameter *mylist* (list ‘foo ‘bar ‘baz))
*MYLIST*

“CL-USER>” is the prompt of SBCL. The list is created with “(list )”. “*MYLIST*” is the return value of the call to “list”. You can ask for the length, access elements by index, lookup elements, ask for a subset, iterate over the list or change elements by index as follows.

CL-USER> (length *mylist*)
3
CL-USER> (nth 0 *mylist*)
FOO
CL-USER> (nth 2 *mylist*)
BAZ
CL-USER> (position ‘bar *mylist*)
1
CL-USER> (defparameter *sublist* (subseq *mylist* 0 2))
*SUBLIST*
CL-USER> (loop for i in *sublist* do (print i))

FOO 
BAR 
NIL
CL-USER> (setf (nth 1 *sublist*) ‘baz)
BAZ
CL-USER> (loop for i in *sublist* do (print i))

FOO 
BAZ 
NIL

Please note that the Lisp environment always gives a return value and returns “NIL” if there is nothing to return. A more Lisp-like version of the above would avoid creating global variables with “defparameter” and use “let” as follows.

(let ((mylist (list ‘foo ‘bar ‘baz)))
  (print (length mylist))
  (print (nth 0 mylist))
  (print (nth 2 mylist))
  (print (position ‘bar mylist))
  (let ((sublist (subseq mylist 0 2)))
    (loop for i
in sublist
do (print i))
    (setf (nth 1 sublist) ‘baz)
    (loop for i
in sublist
do (print i))))

Using lists in a language made of lists can be tricky but it’s worth to think about it for some time because this means that both, data and code are lists that can be manipulated in the same way.

Some notes on lists in Java and Python

Java and Python both have collection types (or compound data types). For a general introduction into collection types, you might want to have a look at Wikipedia.

One of the most basic collection types is a list. In Java, you use the interface java.util.List to work with a list. This interface has several implementations like ArrayList and LinkedList, both in package java.util. The interface and implementations are part of the extensive collections framework. The Java Tutorial has a nice introduction into the collections framework.

You can create a list as follows.

List stringList = new ArrayList();
stringList.add("foo");
stringList.add("bar");
stringList.add("baz");

Note: before JDK 7, you had to use new ArrayList(). JDK 7 introduced the diamond operator .

The List interface provides all the functionality you would expect. You can ask for the length (or size), access elements by index, lookup elements, ask for a subset, iterate over the list or change elements by index.

System.out.println(stringList.size());

System.out.println(stringList.get(0));
System.out.println(stringList.get(2));

System.out.println(stringList.indexOf(“bar”));

List subList = stringList.subList(0, 2);

for (String s : subList) {
System.out.println(s);
}
subList.set(1, “baz”);

for (String s : subList) {
System.out.println(s);
}

In Python, lists are built into the language. You create a list using square brackets.

>>> mylist = ['spam', 'eggs', 'bacon']

The available operations are very similar to what you can do with java.util.List. Ask for the length, access elements by index, lookup elements, ask for a subset, iterate over the list or change elements by index.

>>> len(mylist)
3
>>> mylist[0]
'spam'
>>> mylist[2]
'bacon'
>>> mylist.index('eggs')
1
>>> sublist = mylist[0:2]
>>> for s in sublist:
...     print(s)
... 
spam
eggs
>>> sublist[1] = 'bacon'
>>> for s in sublist:
...     print(s)
... 
spam
bacon
>>>

Of course, there’s much more that can be done with lists. I suggest that you have a look at the Python documentation on data structures.

About coding conventions

Coding conventions are an important part of software development.

Contrary to popular belief, it is not only the functionality of the code that matters, what the code does, but also the readability, what the code looks like. That is because code is read much more often than it is written. If, within a team or organization, everybody formats his code differently or uses different naming conventions, that is a recipe for disaster.

People have debated for ages about using tabs versus spaces and how many of them, but what is more important is consistency. When existing code uses tabs, keep it this way. When you start from scratch, you have a choice.

Code Conventions for the Java Programming Language can be found at Oracle Technology Network. Python has a Python Enhancement Proposal (PEP) for this: PEP 8 — Style Guide for Python Code.

Developers should be familiar with these best practises and try to follow the rules. There are tools to help you with this. For example, Pylint for Python and Checkstyle for Java. However, as PEP 8 points out, it is equally important to know when not to follow the rules. To be consistent with existing code, for example.

A good starting point is the Wikipedia article about coding conventions.

Writing shell scripts in Python

After browsing the web for some time, your download folder might be filled with a lot of files of different type. PDF files, image files and more. Or maybe you collected a lot of files in a temp folder and now you want to have a look at them by file type. Let’s say you work with Linux and have the following Bash script to clean up the mess.

CWD=”`pwd`/”

echo “Distributing files in $CWD :”

for e in $( ls ); do
   if [ `expr index “$e” .` -ne 0 ]
   then
      EXTDIR=”$CWD${e##*.}”
      if ! [ -e $EXTDIR ]
      then
         mkdir $EXTDIR
      fi
      echo “Moving $e to $EXTDIR …”
      mv “$CWD$e” $EXTDIR
   fi
done

echo “Done.”

This will move all files with an extension to a folder with the name of the extension. The output will look something like the following.

neifer@linux-asb4:~/tmp> ~/code/bash/distfiles.sh 
Distributing files in /home/neifer/tmp/ :
Moving fibonacci.py to /home/neifer/tmp/py …
Moving ListSample.java to /home/neifer/tmp/java …
Moving meWinter_.jpg to /home/neifer/tmp/jpg …
Moving slime.1.pdf to /home/neifer/tmp/pdf …
Moving svn-book.pdf to /home/neifer/tmp/pdf …
Done.

Now you wonder how this could be done in Python. The following script does the trick.

import os
import os.path
import shutil

cwd = os.getcwd() + os.sep

print “Distributing files in”, cwd, “:”

for e in os.listdir(cwd):
    if ‘.’ in e:
        extdir = cwd + os.path.splitext(e)[1][1:]
        if not os.path.exists(extdir):
            os.mkdir(extdir)
        print “Moving”, e, “to”, extdir, “…”
        shutil.move(cwd + e, extdir)


print “Done.”

As you can see, it is not that different. However, the Python version might be easier to read for the inexperienced user.
 

How do I write and read plain text files in Python?

Writing and reading plain text files is easy in Python. Look at the following functions.

def write_file(txt, filename):
    with open(filename, ‘w’) as f:
        f.write(txt)

def read_file(filename):
    with open(filename, ‘r’) as f:
        return f.read()

def test_text_io():
    txt = ‘text’
    write_file(txt, ‘spam.txt’)
    s = read_file(‘spam.txt’)
    print s

IF you define the functions and then call test_text_io(), it should create a file called ‘spam.txt’ in your current working directory and print ‘text’ in your console.

Can I add elements to a java.util.List that is backed by an array?

It seems that you can add elements to a java.util.List that is backed by an array. For example, you could try this:

import java.util.Arrays;
import java.util.List;

public class ListSample {
    public static void main(String[] args) {
String[] a = {“foo”, “bar”};
List l = Arrays.asList(a);
l.add(“baz”);
System.out.println(l.get(0));
System.out.println(l.get(1));
    }
}

This compiles just fine (OpenJDK 1.7 on openSUSE). However, if you run the sample, an exception will be thrown.
 
neifer@linux-asb4:~/tmp> javac ListSample.java
neifer@linux-asb4:~/tmp> java ListSample
Exception in thread “main” java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at ListSample.main(ListSample.java:8)
neifer@linux-asb4:~/tmp> 
Read the API documentation for List.add() and you will find out that it is an optional operation not all implementations provide and that it might throw an UnsupportedOperationException.