Manchester Raspberry Jam

I’ve now run two Raspberry Pi events in Manchester, affectionately known as the Raspberry Jam. The first in June, which was the first Raspberry Jam in the UK, and which featured on the Raspberry Pi Foundation’s website where we gained recognition for getting people together to share ideas, demonstrate what we’ve been doing with the Pi, and getting kids interested in building games and writing code as well as inspiring people all over the UK (and the world) to set up their own groups. Continuing the success of the Jam, our second event took place a month later in July, which was equally enjoyable. The July event took place on the same day as the first Cambridge Raspberry Jam – a huge event attended by 300 people and hosted by the Raspberry Pi Foundation at Cambridge University. A number of the attendees from the first Manchester Jam went along to the Cambridge for this, so we look forward to hearing from them about what went on there.

I opened the first event with an introductory talk about my experience with the various computer systems in use in education as I progressed through school. This covered me playing on Granny’s Garden on the BBC Micro and word processing on an Acorn Computer before moving on to Windows 95 PCs, battling with this ‘Internet‘ thing we’d begun to hear about – wondering what the word ‘Yahoo‘ that often appeared was supposed to mean, and my first delivered tech solution at age 11 involving a series of coloured floppy disks.

I kicked things off by initialising a barcamp-style talks grid, and encouraged people to give talks or demos – we had a Python tutorial, talks on Linux for beginners and 3D printing a RasPi case, and a demo of a Raspberry Pi / Arduino hybrid implementation of the game ‘Operation‘ using a live-size manikin! Talks were optional and there was plenty else going on alongside this – with people demonstrating the likes of XBMC on the Pi, chatter over the distributions in use, people learning Python, and most notably Dan Hett from Manchester Game Jam teaching 13 year old girl geek Amy how to get started building games using Scratch – before long she had drawn up some characters for her game, implemented them as sprites in her Pacman clone and started using the drag-and-drop constructs in Scratch to control the actions in the game – it was really impressive how she was thinking like a professional developer – understanding the problems that arose with laying out the behaviour of the objects and their interactions during gameplay. Not only did she understand what these problems were and why they were problems, but she actually thought hard about how she could overcome them, without giving up on it being too difficult. Within a couple of hours or so she had built a Pacman clone with her own drawings as characters and built another game involving flying bats – which I noticed she had instructed to keep track of high scores!

Amy showed me how she had done all this in Scratch (I have not used this before) and it looked to me like lines of programming code – just with each construct in different coloured blocks. I told her if she could read that she would find Python no problem so I asked her if she wanted to watch the Python tutorial – she watched it quietly for about 15 mins, then walked off when the adults started debating over Python 2.7 vs. Python 3.2, and just went back to her Pi and started typing away. She’d remembered all the things shown in the tutorial, and tried to recreate the examples shown. With a little help from me (syntax only) she managed to build a command line based ‘game’ making use of user input and randomly generated numbers. These kids have serious potential. When given the opportunity, they make awesome stuff.

Once we packed everything away I got together with the guys from the Blackpool Linux User Group and we began recording for the Full Circle Podcast – generally covering the events of the day, discussing the potential of the Pi and the future of such events.

The second event was much like the first, except I introduced an idea I saw at another event I attended at MadLab recently – LAMP & Beyond – run by Jeremy of PHPNW / Magma Digital, whereby attendees were asked to contribute post-it notes of skills they had to share and skills they wanted to attain – with the intention of matching groups of people up with a person to lead them in learning a new skill, be it language, development tool, source control package or the latest hipster technology! This worked really well and everyone got a lot out of it. With the Raspberry Jam I hoped this would allow beginners to get started, others to share ideas and help each other with a leg-up without having to figure everything out for themselves and overcome initial issues and get on with something cool. A few people wanted to learn Python so I tutored a few people through some basic constructs of the language compared to what they’re used to in their own languages. Some people needed an OS image loaded on to their SD cards so we had a few people helping them with that, others were making games again (Amy returned to continue with her projects) and some others had the Pi running on an old serial terminal! Towards the end I finally got chance to do something for myself – I got my Pi talking to a breadboard using the PyPi GPIO library! We figured out which pins were which and hooked up an LED with a resistor and controlled its on/off status with a Python command – followed by a few functions (let_there_be_light() and such) which could be called to turn the light on or have it flash. A pretty cool start to external hardware controlled by the Pi. The interesting thing about this ability is that it would be possible to program the Pi to do something and have it do it without use of an external display – just listen to events happen and do things according to the state of those inputs – anything from controlling water sprinkler systems based on soil moisture to … well, anything!

I think it’s fair to say that everyone who comes to one of these events finds something out about the Pi they didn’t know before – the skills sharing nature of the Jam is to be praised and I would encourage anyone of any age or experience to attend one – or start their own!

Update: a few pictures from Jams I & II:

Photos courtesy of Les Pounder [flickr] and Dan Hett

ASCII Bar Charts for Quick & Easy Visualisation

So you have some data. Let’s say it’s a record of the number of instances of some things. Let’s say it’s the number of movies you own, grouped by the year they were released.

Let’s say you have those data in the form of a dictionary in Python, like so:

years = {2000: 2, 2001: 9, 2002: 10, 2003: 9, 2004: 14, 2005: 11, 2006: 8, 2007: 10, 2008: 14, 2009: 19, 2010: 16, 2011: 17}

The following loop will print out an ASCII bar chart for a quick & easy visualisation of these data:

Which looks like this:

Note I used the ‘pipe’ character in this example. First I used ‘o’, which worked well, but I tried a few others ('O','x','X','*','@',':','/','#','[]','+','-','=','_',':)',…) and liked this the most.

That’s the end of what I wanted the blog post to show, but I may as well throw in how I got my data in this case. I have movies saved in a folder, and by convention I name them with the year in brackets at the end so I used glob to loop through the files in this folder, extract the year, and increment the counter in my years dictionary. I have another blog post in draft about using glob to edit filenames in batch. Coming soon.

PHP 5.4 Released

So the other day, Rasmus tweeted that PHP 5.4 was fully released (following several release candidates):

 

There are some great additions, the highlights (other than a huge increase in speed, apparently) being square bracket notation for arrays, array dereferencing and the ability to use traits.

I’m quite excited (sadly) about the use of square brackets to initialise an array, and to be able to code up their contents in this way:

<?php

// PHP 5.3
$arr = array();
$arr2 = array('a','b','c');
$arr3 = array('a' => 2, 'b' => 4, 'c' => 8);
$arr4 = array(array('abc','def'), array(2,4,8), 16);

// PHP 5.4
$arr = [];
$arr2 = ['a','b','c'];
$arr3 = ['a' => 2, 'b' => 4, 'c' => 8];
$arr4 = [['abc','def'], [2,4,8], 16]];

This is similar to the way we do lists in Python.

Array dereferencing means we can now access particular elements within an array upon creating it:

<?php

function makeArray() {
    return [1,2,3];
}

// The PHP 5.3 way
$arr = makeArray();
echo $arr[2];

// The PHP 5.4 way
echo makeArray()[2]; // returns 3

This works the same way with Objects:

There’s now something called Traits, which is a concept brought in to PHP 5.4:

This allows a trait to be reused by any objects which refer to it in this way. It’s to save on copy and pasting blocks of code. The compiler now does that for us!

Also, up to and including PHP 5.3 we could attempt to echo an array without a notice given, but the word ‘Array’ would be echoed instead of any of the array’s content. Now in PHP 5.4 a notice is given:

Although it does still echo the word ‘Array’.

Here’s a great video (the keynote at PHPUK Conference in London) of Rasmus talking about the PHP project from the beginning, and about PHP 5.4:

Ternary Operator and Other Shorthand Code

The ternary operator is a shorthand way of writing an if/else statement where a particular action occurs in both cases, but the value associated with that action depends on the condition stated.

For example, the traditional if/else construct (C/Java/JavScript syntax):

if (a > b) {
   result = x;
}
else {
   result = y;
}

can be rewitten as:

This in itself is a huge benefit to clean, concise code. I use it wherever possible. Here’s an example in PHP:

A particularly cool Python example utilising the idea of a function of a comprehended list:


If you want to return/echo true or false depending on the condition, there is no need for the ternary operator as a shorter operator is available: simply echo the boolean result of the condition, i.e. rather than:

This will produce the same output:

There are various other implementations of this idea in different languages, but the reason for this blog post is because while talking about these with my colleague Mike and I came up with an interesting manipulation of this on the train to work the other day. I had a program which incremented a value by 1 if and only if a condition was true:

In my opinion this is good because it’s on one line, but bad because the else 0 should be unnecessary. Unfortunately Python requires an else here. The obvious alternative doesn’t use a +0 but requires 2 lines:

Anyway, the thing we thought of was to increment by the integer value of the boolean, i.e. 1 if True, 0 if False:

Evaluating a condition, say x>0, returns True or False, which when added to an integer is equal to 1. Another implementation of this is to multiply the value of the condition by a scaling factor: