Category : Code


An Introduction to Python Programming #2

Welcome to my second python programming post. In the first post we looked at the print statement used to print to the screen, and we also looked at basic user input via the raw_input() function. We also looked at basic variable declaration and useage. In this post we will be taking a look at some more variables and also some basic flow control; flow control is the art of controlling your program based upon rules and conditionals. For example, a program that just has statements and funtions but no flow control will probably be pretty boring; both to write and to use, whereas a program that is correctly controlled can not only be a lot more useful for the user but also they can be extremely fun to write.

So without further ado, lets begin with multiple variable declaration. What this is, is declaring more then one variable in one statement/line. If we wanted to declare 3 variables in the way we learned in my first post, we would be doing something similar to this

var1 = "foo"
var2 = "bar"
var3 = "ubuntu"

Now, this is all well and good, but it unnecesarily uses 3 lines. We could instead, declare all of these variables together by doing the following.

var1, var2, var3 = "foo", "bar", "ubuntu"

as you can see, by doing things this wy, we have eliminated 2 = signs and 2 carriage returns, meaning we are only using 1 line.

Now, there are a few reasons why you wouldnt use this sort of variable declaration. First off, it is unlikely that at the beginning of writing your program you are going to know every variable that you will need. Secondly there is the issue of global and local variable scopes but we wont go into that now.

The next thing we are going to look at is flow control, in the form of the if, elif, else statements. These ataements are the building blocks of almost every program and we shall take a look why this is so. Consider the following, A user enters his/her age into your program, you wish to output a line wich; depending on the age, says “Wow, your old!” or “Your very young” or “Nice middle age”. To do this we would use the if, elif, else statements. Lets take a look at each one. The if statement is the first statement you use, usually the if statement is used to compare two or more values for equality, returning “True” for an equal comparison or “False” for a non-equal comparison. If the statement returns “True” then it runs a block of code, if it does not return “True” then it will then check for an elif or else statement. elif statements are run before else statements and after if statements.

Now, before I talk about how to implement conditional statements I need to quickly mention syntax. Python is extremely sensitive to what is known as whitespace, whitespace is, as you probably expected, spaces, tabs and carriage returns. In python code blocks such as those to be run upon a successful conditional comparison, are seperated from the rest of the code by indentation. If you get the indentation wrong on a block of code, when you come to run the program you will be presented with a syntax error. Usually, indentation should be either 4 spaces, or 8. Although the debate on which is better is an ongoing heated discussion but in general either is fine. The one thing both sides agree on is that indentation should never be created via tabs because a lot of people use different sizes of tabs and if you accidently mix tabs and spaces then someone else who tries to edit and run your code could be riddled with syntax errors.

So, now we have the formalities out of the way lets take a look at the implementation of conditional statements. The following code prompts the user for their name, age and sex. It then prints out a specific message dependant on the users entry.

name = raw_input("What is your name?: ")
age = raw_input("How old are you?: ")
sex = raw_input("Are you male (m) or female (f)?: ")

if sex == 'm':
    print "Hello %s, you are a %s year old %s. Thats a good manly name." % (name, age, sex)

elif sex == 'f':
    print "Hello %s, you are a %s year old %s. You have a very feminine name." % (name, age, sex)

    print "Hmm, thats odd, it seems", name, "you failed to enter 'm' or 'f'."

Now, lets take a look at this code. The first 3 lines just take user input as discussed in my first post. The next section, the if clause checks for comparison between the variable sex and the string ‘m’. If this comparison returns “True” then the string beneath the statement will be printed, the ‘%s’ signs in the string corraspond to the variable names in the tuple at the end of the line. If the comparison returns “False” then the elif clause is checked, same as before, if “True” it runs its code block, if “False” it runs the else clause.

So, to wind up, your program should look like the following.

#! /usr/bin/env python

name = raw_input("What is your name?: ")
age = raw_input("How old are you?: ")
sex = raw_input("Are you male (m) or female (f)?: ")

if sex == 'm':
    print "Hello %s, you are a %s year old %s. Thats a good manly name." % (name, age, sex)

elif sex == 'f':
    print "Hello %s, you are a %s year old %s. You have a very feminine name." % (name, age, sex)

    print "Hmm, thats odd, it seems", name, "you failed to enter 'm' or 'f'."

I hope this post has been informative for you, and I want to thank you for reading. To finish I would like to offer you a challenge. With your new knowledge, create a program that prompts the user for 2 of their friends name, age and sex and prints out the same information as we have done in our program here. For extra brwnie points collect al the information, then use a single print statement to print out all the information. A few hints for you would be use “\n” in your print statement to enter a new line, and also use variables in conjunction with the conditionals instead of print statements to store the strings first.

Happy coding!

Read More

An Introduction to Python Programming #1

Python is a high level programming language useful for, but not limited to, everyday tasks. Python is quick to pick up and easy to understand. Many of python’s statements are very English like so there is not much confusion. Lets take a look at pythons basic print statement, we are going to be writing the traditional Hello World program first, and then we will take a look at some other things.

First off, you will need to install python. Using your preferred method of installation install the package python. Now fire up your favourite editor and lets get started.

Now, the obligatory Hello World program just prints the words Hello, World! to stdout which is usually the terminal.

To print to stdout python uses a print statement which takes the syntax of

print string

a string is just a bunch of characters. Now, to be able to run python programs directly you need to tell the computer what type of program you have written. We do this using what is called a ‘shebang’ line, named because the first two characters of the shebang line are #! the shebang line for a python program looks like this

#! /usr/bin/env python

this tells the computer where the interpreter for your program, is stored. Go ahead and add that as the first line in your file. From this point on and in any subsequent python tuts I write here, I will assume that the shebang line has already been entered.

Now we get to the business part of our first program, the print statement, which should look like this.

print "Hello, World!"

Thats it, its that simple. Go ahead and add that line to your file, on a seperate line from the shebang line. Now save the file as “” (remember this is case sensitive) then, in the terminal, cd to the directory where you saved the file and run this command


You should now see “Hello, World!” printed on the terminal, if you do, well done you have successfully written your first python program. If you dont see those words or if you get an error message, re read what I have written and see if you made a mistake, if you didnt just leave a comment with the error message and whatever was in your file and I’l try and help get it sorted.

Now that we have had a look at printing a few words to the screen, lets look at printing variables. A variable is like a container that stores whatever you tell it to. Lets rewrite our Hello World program to use a variable. When you need to make a variable in python you use the = operator to assign it a value like this

hello = "Hello, World!"

This stores the string “Hello, World!” in the variable hello. Now we can simply reference the variable name without any quotes in our print statement and we will get the contents of the variable printed, so construct your print statement like so

print hello

Save the file under and run it in the same way as you did before (using the new filename). With any luck you should see the same output as before which is great! Well done!

Now you may be thinking that using a variable has added an extra line to our program unnecessarily but all will become aparent by the end of this post. So we now know a bit about the print statement and variables, now lets look at user input using the raw_input() function. This function takes user input and can store it in a variable for printing/operating on. The raw_input() function takes the following syntax

variable = raw_input("Prompt to be displayed: ")

then, whatever the user enters gets stored in the variable. So lets rewrite out Hello World program to use this method of printing a string. Enter the following into a file named

string = raw_input("Input a string please: ")
print "Your input was: %s" % string

Now, allow me to quickly explain a few things about that print statement that we have not seen before. The %s means that we want a variable of type string to be entered at that location. After the end quote we tell python that we want to tell it about an interpolated variable by using the % on its own followed by the variable name that we want entered.

With a bit of luck, you should now have written your first 3 python programs. In my next few python posts il be talking about loops and conditionals which really help structure your programs. I hope this was helpful to you, thanks for reading.

Read More

New WordPress Freelance Project: JDMNagoyaAutoParts

I have been working on a lot of WordPress freelance projects lately, that’s I why I have been lazy posting. One of the most successful one is for sure JDMNagoyaAutoParts, a showcase website for Japanese cars in Canada. I say it was a success because the project consisted on a lot of out-of-the-box theme development techniques and a lot of features that turn this site into a really flexible, scalable, time-saving CMS.


I began developing this theme with three objectives in mind – I wanted to built a powerful, easy-to-use, automated CMS with WordPress. I can proudly say I fulfilled those objectives the best way possible. This approach required a set of wonderful features to meet the objectives. I am going to share with you each of them below.

Custom Write Panels

custom-write-panels-wordpressTo make things easier, I developed a set of custom write panels for the client. Can you imagine how much time would it require to use the default custom field functionality to enter something like 30 fields for a post (the cars details)? That would take forever. This is why I implemented custom write panels, so that the time-saving and easy-to-use objectives get met. However, this is not all.

Automated Image Placement

This is a great feature that does not come by default with WordPress themes. It is sometimes tricky to make it work it right. But, using this feature on your site, after several months you will realize that his has not saved you hours, but whole days and I have no shade of doubt when I say that. Here is why. My client uses about 10 images per post that go to the gallery, one of the images must go the the homepage and another in thumbnail size must go the the category. Normally, that would mean 12 more custom fields for image links. You get what I am saying, right?

Galleria Integration

gallery-wordpressGalleria is one of the best JavaScript image galleries out there, written in jQuery. I have implemented it in many WordPress driven sites and I am very pleased with its results. As I pointed out in the previews point, the gallery is completely automatic – just upload the images in the media library of the post you are writing and they are at the same time at the gallery. It cannot be easier or more time saving.

Glider Integration

glider-sliding-script-in-wordpressGlider is a great script that makes it easy to create a sliding component in a website. I love that script and I have been using it in several other projects too. It is fully automated for sure. In the site I am talking about it show up the first image the latest post of six car categories I have set to. It is a great effect to the site, in my opinion.

Suckerfish Menu Integration

suckerfish-dropdown-menu-in-wordpressSuckerfish drop down menu is a complete XHTML/CSS drop down menu invention, except for IE6 which needs to use a small piece of JavaScript to make it work right. It is quite well known now on the web right now. What makes it special in the site is the fact that it makes use of categories and pages at the same time, it has rounded corners and I find it cool.

Open Source Works

The last but not the least, what I cannot forget to say is that this site is completely developed with and on open source. It is built on Ubuntu, designed in Inkscape, developed on Bluefish and run on WordPress, my lovely combination of Open Source Web Development tools.

Have to Say

I would love to hear what you are thinking about what you just read. Your opinion is very much appreciated.

Read More

That’s Awksome!

Have you ever been doing something on the terminal and realise that most of the information thats being reported is useless to you? Ever thought, “Hey it would be really cool if it would only show me ‘just that bit’”?

Awk could be the answer. Awk is a DSL or Domain Specific Language aimed at text processing, which is used by many command line guru’s. Although awk has an extremely large amount of uses, I will only be detailing its print statement in use with a pipe.

The awk print statement is used, obviously, to print things, not that spectacular but the real magic comes with awk’s variables. When you pass a stream to awk it counts the number of columns, or parts of the stream separated by the input field separator, which by default is whitespace, it then assigns each section a variable $num. So in the following line

This is a line.

$1, $2, $3, $4 = This, is, a, line.

So you may be able to see how it is useful, but you may not be able to see how practical this is.

Lets do an example run using ls. If I run

ls -lh

in a terminal I get the following output:

drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo
-rw-r--r-- 1 bod bod 149M 2009-06-22 07:50 archlinux-2009.02-ftp-i686.iso
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Bend It Like Beckham (2002) DVDRip XviD []
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Bride Wars[2009]DvDrip[Eng]-FXG
drwxr-xr-x 2 bod bod 4.0K 2009-06-23 13:46 Desktop
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 downloads
drwxr-xr-x 6 bod bod 4.0K 2009-06-22 07:46 Hes.Just.Not.That.Into.You.DVDRip.XviD-DiAMOND
drwxr-xr-x 3 bod bod 4.0K 2009-06-22 07:46 Hitch[2005]DVDrip[Eng]AC3[5.1]-Atlas47
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Ice Road Truckers.S
-rw-r--r-- 1 bod bod 348M 2009-06-22 08:17 Ice.Road.Truckers.S03E03.Canadian.Invasion.DSR.XviD-KRS.avi
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 livecd-i686-installer-2008.0-r1
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 lose a guy torrent
-rw-r--r-- 1 bod bod  303 2009-05-29 19:03 not_found.html
-rw-r--r-- 1 bod bod  303 2009-05-29 19:03 not_found.html.1
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Ocean's.12[2004]DvDrip[Eng]-aXXo
drwxr-xr-x 2 bod bod 4.0K 2009-06-23 08:46 perl
drwxr-xr-x 2 bod bod 4.0K 2009-06-25 17:51 python
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:52 Relapse
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Spore-RELOADED
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 The.Accidental.Husband[2008]DvDrip-aXXo
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 The.Illusionist[2006]DvDrip[Eng]-aXXo
-rw-r--r-- 1 bod bod 2.4K 2008-12-11 21:54 t_skariah.asc.gpg
-rw-r--r-- 1 bod bod 3.0K 2009-06-23 13:59 tuto.txt
-rw-r--r-- 1 bod bod  575 2009-06-23 08:45 tuto.txt~
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Underworld Rise of the Lycans[2009]DvDrip[Eng]-FXG
drwxr-xr-x 2 bod bod 4.0K 2009-06-22 07:46 Yes.Man.2008.DvDRip-FxM

Now lets say, I only wanted to know about the name and the permissions. I could use awk to only give me those sections. Note: The use of the ls flag –quoting-style=locale makes ls quote filenames meaning awk will print filenames with spaces as one column instead of splitting them.

ls -lh --quoting-style=locale | grep -v "total " | awk '{print $1 "\t" $8}'

That would output this:

drwxr-xr-x	`Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo'
-rw-r--r--	`archlinux-2009.02-ftp-i686.iso'
drwxr-xr-x	`Bend
drwxr-xr-x	`Bride
drwxr-xr-x	`Desktop'
drwxr-xr-x	`downloads'
drwxr-xr-x	`Hes.Just.Not.That.Into.You.DVDRip.XviD-DiAMOND'
drwxr-xr-x	`Hitch[2005]DVDrip[Eng]AC3[5.1]-Atlas47'
drwxr-xr-x	`Ice
-rw-r--r--	`Ice.Road.Truckers.S03E03.Canadian.Invasion.DSR.XviD-KRS.avi'
drwxr-xr-x	`livecd-i686-installer-2008.0-r1'
drwxr-xr-x	`lose
-rw-r--r--	`not_found.html'
-rw-r--r--	`not_found.html.1'
drwxr-xr-x	`Ocean\'s.12[2004]DvDrip[Eng]-aXXo'
drwxr-xr-x	`perl'
drwxr-xr-x	`python'
drwxr-xr-x	`Relapse'
drwxr-xr-x	`Spore-RELOADED'
drwxr-xr-x	`The.Accidental.Husband[2008]DvDrip-aXXo'
drwxr-xr-x	`The.Illusionist[2006]DvDrip[Eng]-aXXo'
-rw-r--r--	`t_skariah.asc.gpg'
-rw-r--r--	`tuto.txt'
-rw-r--r--	`tuto.txt~'
drwxr-xr-x	`Underworld
drwxr-xr-x	`Yes.Man.2008.DvDRip-FxM'

Pretty cool yeah? So, thats all well and good, but I want to add my own spin to the output, the following is a bit different but shows awk’s brilliance even more.

ls -lh --quoting-style=locale | grep -v "total " | awk '{print "Size is ", $5 "\tfor ", $8}'

This will print out the following:

Size is  4.0K	for  `Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo'
Size is  149M	for  `archlinux-2009.02-ftp-i686.iso'
Size is  4.0K	for  `Bend
Size is  4.0K	for  `Bride
Size is  4.0K	for  `Desktop'
Size is  4.0K	for  `downloads'
Size is  4.0K	for  `Hes.Just.Not.That.Into.You.DVDRip.XviD-DiAMOND'
Size is  4.0K	for  `Hitch[2005]DVDrip[Eng]AC3[5.1]-Atlas47'
Size is  4.0K	for  `Ice
Size is  348M	for  `Ice.Road.Truckers.S03E03.Canadian.Invasion.DSR.XviD-KRS.avi'
Size is  4.0K	for  `livecd-i686-installer-2008.0-r1'
Size is  4.0K	for  `lose
Size is  303	for  `not_found.html'
Size is  303	for  `not_found.html.1'
Size is  4.0K	for  `Ocean\'s.12[2004]DvDrip[Eng]-aXXo'
Size is  4.0K	for  `perl'
Size is  4.0K	for  `python'
Size is  4.0K	for  `Relapse'
Size is  4.0K	for  `Spore-RELOADED'
Size is  4.0K	for  `The.Accidental.Husband[2008]DvDrip-aXXo'
Size is  4.0K	for  `The.Illusionist[2006]DvDrip[Eng]-aXXo'
Size is  2.4K	for  `t_skariah.asc.gpg'
Size is  3.0K	for  `tuto.txt'
Size is  575	for  `tuto.txt~'
Size is  4.0K	for  `Underworld
Size is  4.0K	for  `Yes.Man.2008.DvDRip-FxM'

Now, lets pick that command apart. The first section:

ls -lh --quoting-style=locale

Gives us a listing of the current working directory in a human readable long listing style. Meaning it prints the file sizes with K/M/G/T on the end, long listing means it gives you loads of extra info, like permissions, group user ownership, timestamps etc.

The next section:

grep -v "total "

Means, before we pass the text to awk, get rid of any line which contains “total “, ls -lh would usually print “total ” as the first line, which would muck up our awk command.

The final section:

awk '{print "Size is ", $5 "\tfor ", $8}'

Means, print the words “Size is” followed by column 5 then the word “for” then print column 8. The ‘\t’ is an escape character which means tab, it tells awk to insert a tab there. The various ‘,’ and whitespace in the quotes are just for presentation, you’ll have to experiment with them yourselves.

In case you didn’t know, the pipes


send the output of the command preceding the pipe, to the input of the command succeeding the pipe.

Hopefully now you can see the power of awk. This is by no means whatsoever a complete guide, It barely covers the basics. If you wnt to find out more, check out chapter 6 of the bash beginners guide.

Thanks for reading.

PS: Angus,Thongs.And.Perfect.Snogging[2008]DvDrip-aXXo is not porn, its a film my girlfriend bought :)

Read More

How to Install XAMPP on Ubuntu


If you are a Linux (or Ubuntu) web developer or even a web designer with interest in CMS systems like WordPress, Drupal, Joomla etc., you are for sure in need of a web development environment on your work computer to test websites or themes that you built for any CMS. If you have not fund a solution yet, the good news is that there is one – it is XAMPP.

Read More
1 3 4 5 6 7 8