April 20, 2011 4 Comments
Well, so far we’ve learnt about storing data (either “flat” in file, or as an object which Python does in pickles which are also data in files, but with some structure) but we don’t know anything about storing the code we are typing. This means that we need to go through the tedium of typing stuff in all the time (or cutting and pasting I guess). It is especially tedious when we mistype something.
In order to do some more complex things in Python we really need to be able to store our code somewhere so that typing mistakes we make don’t mean we have to re-type the whole kit and kaboodle. We could, if we really wanted to, open a file from within our Python prompt and write code to the file (as a flat file):
>>> f = open('textfile.py','w') >>> f.write("print 'This is some python code stored in a file'") >>> f.close() >>> import textfile This is some python code stored in a file
What has happened here is that we’ve open()ed a file called ‘textfile.py’ (again, we’ve taken the chance that there isn’t already such a file, because open()ing it would delete the existing contents). We’ve then written the following to the file: print ‘This is some python code stored in a file’
We can tell what’s in the file by opening it and reading it back:
>>> f = open('textfile.py','r') >>> print f.read() print 'This is some python code stored in a file' >>> f.close()
Then, when we imported the file, the contents of the file were executed . We can execute this file from the command line (not the Python prompt) using the python command:
>python textfile.py This is some python code stored in a file
Note only one >, not three.
Why did we put double quotes? See note 2 below.
Now, using Python to write Python code to a file would be incredibly tedious. Moreover, it wouldn’t solve the problem we have with the ease of editing. So now we’re going to talk about text editors. You may need a responsible adult to help you here.
Text editors are programs which are designed to, well, edit text. They are generally nothing like word processors, and, for our purposes, you can’t use a word processor as a text editor (see note ). There are way heaps lots of text editors. Everyone is welcome to list their favourite in comments if they feel inclined.
When you use a text editor, there is a different cycle of work. To date, feedback has been instant because our code has been typed directly into the Python interpreter. However, when using a text editor your code must first be saved to a file, then you need to execute the file using the python command. However, once you’ve saved a file you can easily modify it and re-run it. If you mistyped a single character, then you can use the text editor to navigate to that character and change just it, then save the file and run it through the Python interpreter again.
For the following tutes you need to complete these exercises:
Exercise: See if a text editor is already installed on your computer (try Notepad on Windows, Linux systems should have Gedit and/or Kate)
Exercise: If you don’t have a text editor installed, get the permission of a responsible adult, go find a text editor and install it on your computer.
Exercise: Learn how to use it to open files (usually ctrl+O), and save files (usually ctrl+S).
Exercise: using your new found text editor skills, open a new file and save the following to it:
print "Hello world!"
Then, open a command line, cd to the directory in which you saved the file, then run the file by typing python <name of file>. Find a responsible adult to help if you can’t work out the cd command.
A text editor that I like is called Kate. Kate needs a windowing system to run, but is available on all popular operating systems. One of the neat things about Kate is that it will do Python syntax highlighting for you. That is, it will use different colours to show (eg) what are strings, what are commands etc. Kate also has a command line within the editor, which may make life a little easier for you. Kate will also automatically indent your code. This is a useful feature to have and saves you typing in tab characters or spaces to line up code blocks. This is what the file we saved earlier looks like in Kate, showing code highlighting (the red part shows a string, the black part a command):
A basic editor which works on the command line is called nano.
If you want to make life hard for yourself, but want to be able to swagger as if you were a Real Programmer(tm) try emacs or vi. Warning: don’t try emacs or vi unless you are brave, neither is for the faint hearted.
There is a text editor which often accompanies Python distributions (it is called idle). However, at the moment I don’t want to use idle, because it may cause problems down the track.
 Python has its own function to execute strings as if they were instructions. It’s called exec(), and that’s happened. Example:
>>> a = "print 'This is some python code stored in a file'" >>> a "print 'This is some python code stored in a file'" >>> exec(a) This is some python code stored in a file
Warning: there are security issues with using exec() don’t try this at home.
 That is a bit subtle. We had to put what was written into a string in order to write it. We’ve had to use double quotes to make the command we want stored into a string. Had we left the double quotes out we would have had a problem:
>>> f = open('textfile.py','w') >>> f.write(print 'This is some python code stored in a file') File "<stdin>", line 1 f.write(print 'This is some python code stored in a file') ^ SyntaxError: invalid syntax
When we put the double quotes in, it’s not a command anymore, it’s just a string, and we can write strings to files. However, we wanted to write an actual command to the file – the print command, and for that we needed to use single quotes. We needed to use double quotes so that the single quotes would be made part of the string.
 If you are careful to use “save as” and always choose “plain text” as the save format, then you can use a word processor, but they are best avoided.