Wednesday, May 7, 2008

File formats

Let's talk about two aspects of file names and formatting relevant to what I'm doing here. The first is the file extension. The files I use for programming are plain text files. For typical use, I put a '.py' extension on the end of program file names, so that Smultron or BBEdit will do the syntax coloring for Python. It doesn't matter to Python. I can do 'python program.txt' in Terminal and Python will run the script just fine. I could even define '.txt' in Smultron's Preferences so that it would be colored for Python syntax, but that is distracting if a file isn't Python code.

Blogger won't let me upload text files, as far as I can tell, so I am putting my example code files on the .Mac server and linking to them. If I link to a '.py' file, when I click on the link the file is automatically downloaded to my computer. I (and I bet you also) find that behavior disturbing, unless the link advertises itself (say as 'pdf'). My solution is to change the file extension to '.txt'. That makes the linked file appear in the browser window. It's not perfect---the files appear to have 8 spaces rather than 4 for the indents. But it works well enough and the script should run for you without modification.

Previously, I showed code in html files, either with commentary or not. I adopted a strategy I saw Peter Norvig use, where the code is in a colored block, like in my very first post. It looks nice, but it's extra work, and you need to copy and paste before you can run it.

The second issue is new lines. As you probably know, there are three conventions in the world: Windows, Mac (old style), and Unix (including OS X). I use Unix NL exclusively (technically, it's called LF, line feed, 0x0A or '\n'). Here is a program to show that:

And, as you've just seen, when I want to emphasize a particular section of code, I'll paste a screenshot as a '.png' image. One last peculiarity, I've gotten in the habit of using single quotes, simply because I don't have to shift---I get fewer typing errors. Python lets you use either single or double quotes around strings, as long as they match.