Templating using Python and Preppy

By Andreas Schickedanz Jun 10, 2013

A few days ago I received my new Beaglebone Black. Since then I spend much time on playing around with it. I installed a Teamspeak server, connected it with my old Beaglebone and my Raspberry Pi in a grid structure and wrote a simple service to monitor some sensors connected to the Beaglebone.

During the development of the sensor service, I had to face the problem of providing the sensor data in different formats, so I could access the data from different platforms using a very basic REST interface. The simplest solution to this problem is using the Preppy library.

Flashing Ubuntu 13.04 or Debian Wheezy to the BeagleBone Black eMMC

By Andreas Schickedanz May 31, 2013

Yesterday my new Beaglebone Black arrived. Since I had plenty of time to play around with my old, now somewhat shabby looking, Beaglebone (White), I have so many great ideas what to do with it. It was really really hard to wait for it. The new distinctly more powerful setup with a AM335x 1GHz ARM© Cortex-A8 processor, 512MB DDR3 ram and the 2GB on-board eMMC storage, opens up new areas of application. So I instantly started to setup the new Board so I could replace my old one.

I decided to flash the new Debian Wheezy (7.0) instead of Ubuntu Raring Ringtail (13.04), because I thought about why I should take the caffeine pill instead of drinking a coffee, since Ubuntu is based on Debian. I tried to flash an image of Robert Nelson following his instructions at the elinux.org wiki. I was able to flash an image to an micro SD-card, but I was not able to figure out how to flash one of the eMMC images to the internal eMMC storage.

However the instruction below are based on Robert’s instructions and show you how to flash the image using a system running on a micro SD-card. They work for both the Debian and the Ubuntu image. So to get started decide which OS you would like to use and download the latest image provided by Robert Nelson’s homepage at https://rcn-ee.net/rootfs/:

wget https://rcn-ee.net/rootfs/wheezy/debian-7.0.0-console-armhf-2013-05-29.tar.xz

Lindenmayer Systems - A Python Adventure

By Andreas Schickedanz May 29, 2013

A simple fractal based on Lindenmayer systems

In my last article about Langton’s Ant and Monty’s Python, I talked about how to simulate Langton’s Ant using Python’s Turtle library. The turtle library reminded me of another interesting application for such a library, that I got to know in a lecture about formal systems, the Lindenmayer Systems.

Introduction

A Lindenmayer system (or L-system) is a formal grammar, which consists of an Alphabet A, a set of projection rules P, a starting axiom S and a set of terminals that could be translated into geometric structures. The basic idea of Lindenmayer systems is the successively rewriting of a string by replacing each nonterminal (right side of projection rules) with a new substring (left side of projection rules), which could consist of any combination of terminals and non-terminals. This refinement process makes it possible to generate very complex geometrical objects by applying simple rules to the starting axiom.

Langton's Ant and Monty's Python

By Andreas Schickedanz May 24, 2013

The Langton Ant program

The last view days were full of work and so I enjoyed the few free hours yesterday. And because I had no time for coding and blogging, I decided to implement a problem that a friend introduced to me last weekend - Langton’s Ant.

Langton’s Ant is a very simple problem invented by Chris Langton in 1986. It basically covers a set of rules which together determine the behaviour of an ant that colors the ground while moving. Even though the rules used are very simple, the resulting behavior of the ant is very complex. The most interesting is, that the ant’s movement results in a recurring structure after a couple of steps (about 10000). The idea of this kind of simulation has been extended in many different ways. The most popular extended version of Langton’s Ant are Turmites which basically extend the coloring rule of Langton’s Ant.

Lexical analysis using Python

By Andreas Schickedanz May 10, 2013

I was always very facinated of everything about compilers, interpreters, parsers and lexers. Now I was wondering how I could implement such things using Python. I decided to write a simple lexer for algebraic expressions that converts a sequence of characters into a sequence of tokens. Till now I just implemented such things using C or C++ and so I started the simplest way as I learned by reading the Dragon Book.

First of all I created a simple enum type that allows me to create enums as used to from languages like C++ and Java. Then I implemented a Token class that holds all information about a token and enables me to easily print a token using the Python’s magic str method. Finally I added a Lexer class that processes character by character and returns the tokens.

Typed text animation with Python & Gimp

By Andreas Schickedanz Apr 25, 2013

Typed text animation using Python and Gimp

As I mentioned in my last post “Developing Gimp plugins using Python”, I was trying to create a typed text animation using Gimp, which should be integrated into a screencast intro I am currently working on. One problem I had to face was the renaming of the image layers, that allows me to control the frame rate and overlay behavior.

Developing Gimp plugins using Python

By Andreas Schickedanz Apr 21, 2013

Dialog window of the Gimp plugin

One topic of my last post “Converting gif animations to mpq files on Ubuntu” was my decision to create my own text animation using Gimp. My goal was to create a nice looking animation of text typed in a terminal. A very simple task. You just have to create a layer for each new letter that should be displayed. Finally you append the time in milliseconds, the image should be displayed and the replace flag, which forces gimp to replace the previous layer if the new one should be displayed. Finally your layer stack should look like this:

Layer #n (300ms) (replace)
Layer #n-1 (25ms) (replace)
...
Layer #2 (25ms) (replace)
Layer #1 (25ms) (replace)

However, you might want to change the time a layer is displayed, which is not really a problem as you just have to change the layer name, but in my case I had to rename about 100 layers. That is really time consuming and in most cases I had to change the display time very often until I had the animation speed I was happy with. That sucks! But there are good news: Python rocks and it is very easy to build Gimp plugins using Python!

Converting gif animations to mpq files on Ubuntu

By Andreas Schickedanz Apr 19, 2013

I am planing to record some screencasts about coding, hardware hacking and gaming. But as you all know a good screencast does not only consist of a nice recording, but also a nice intro and a nice outtro (also called “extro”). So I created a nice looking wallpaper and a friend of mine composed a really cool song for me. Thanks Fabi! Using the OpenShot video editor it was very easy to combine these two and then I just had to add some animated text.

However, all text animations offered by OpenShot did not meet my needs, so I decided to create my own using the Gimp. After about two hours of work, several coffees and much copy ‘n paste I had a nice animation of typed text. I exported my Gimp project as animated gif file and added it as a new track to my OpenShot project. Nothing happend. The gif was added but when I viewed the preview of my intro I just saw the first image of my gif animation. Just a short lookup and everything was clear - OpenShot is not able to handle animated gif files. Two hours of wasted time.

MySql based session mangagement in PHP

By Andreas Schickedanz Apr 10, 2013

Today I would like to show you how to implement and use a class that allows you to store session data within an arbitrary database instead of storing the data in files. At the end of this tutorial I will show you how to use this class in practice.

Session management

If we are talking about a session, we always talk about an established connection between a client and a host. Because users could not be unequivocally identified within the world wide web, since it is based on stateless protocols such as http, it is necessary to implement such an identification within the application layer (OSI model). PHP uses therfore the so called session id, which is transfered from the client to the server. This id allows the unambiguous allocation of fragmented session data to one user.

Session data are stored at the server side and are deleted automatically after a fixed delay!

Typical examples for sessions are systems, which handle information that have to remain state between otherwise stateless transactions between different web pages. Typical applications which require this behavior are forums or online shops.

Let’s stay with the example of an online shop. Every online shop offers a cart in which the products are placed in, that should be ordered. This articles and the number of products, that the customer will ultimately source, have to remain state between different web page calls and have to be unambiguous allocated to the correct user.

Some of you may have noticed that in many web shops a user does not have to be authenticated to use the cart, which means that a session could also be established without prior authentication. This also allows us to track the user’s movements when he visits our web page.

Generating Ascii tables using python

By Andreas Schickedanz Apr 2, 2013

Today I would like to show you how to use masks to format your output data. Since I often have to monitor some log changes or processes I decided to show you how to monitor MySQL processes using python.

Caution! The following script uses an old syntax to format output data. The example works also with Python 3, but there is a new way to do this. For more details see the Format String Syntax page of the python 3 documentation.

#!/usr/bin/python
import MySQLdb as mdl;
import sys;
mdo = None;

try:
	# Setup the database connection ...
	mdo = mdl.connect(host='host', port=3306, user='username', passwd='secret', db='database');

	# ... and fetch a cursor.
	cur = mdo.cursor();

	# Initialize the cursor with the process list ...
	cur.execute("SHOW FULL PROCESSLIST");

	# ... and fetch the results.
	rows = cur.fetchall();

	# Setup the mask to format the process list ...
	procMask = "| %8.8s | %16.16s | %16.16s | %16.16s | %16.16s | %16.16s |\n";

	# ... and initialize the ascii process table.
	asciiTable = procMask % ("Id", "User", "Host", "Database", "Command", "Time");
	asciiTable += "".rjust(len(asciiTable) - 1, '-') + "\n";

	# Add an entry to the ascii process table.
	for proc in rows:
		asciiTable += procMask % (proc[0], proc[1], proc[2], proc[3], proc[4], proc[5]);

	print asciiTable;
except mdl.Error, e:
	print "Error %d: %s" % (e.args[0], e.args[1]);
	sys.exit(1);
finally:
	if mdo:
		mdo.close();

I used this old formatting style, because I could not figure out how to limit the length of a string. I was just able to pad the output to a certain length, but if the input string was longer then the specified width, python did not care. So I would be most pleased to have your hints.

asciiTable += "| {0:>8} | {1:>16} | {2:>16} | {3:>16} | {4:>16} | {5:>16} |\n".format(
	proc[0], proc[1], proc[2], proc[3], proc[4], proc[5]);

Also Python 3 uses a new formatting style, there is one advantage of this example. This fashion to format output using a mask is also used in many other languages like C/C++ and PHP, it is very easy to adopt this script. I would be happy to see some of your own implementations in your preferred programming language.

So until next time, happy coding!


is a Computer Science MSc. interested in hardware hacking, embedded Linux, compilers, etc.