Tuesday, July 15, 2008

Why you should learn to write code

Yes, you.

There hasn't been a job yet in my repertoire that wasn't improved by a bit of code. Oddly enough (or sensibly enough, as you'll see) the parts that weren't improved weren't the more cognitively demanding areas of these jobs. It was the dull parts where ten lines of script helped me out beyond belief.

Real life example: we have to upload our code for every project into a repository that resides offsite. Due to a few business rules, we can't just ftp files over, or give them SVN checkout permission to our servers. Not a huge issue, really, since code bases are usually pretty small.

Cue the day I handed over a product that, due to images, video, and flash, was over a gig.

  1. It belonged to the project with monthly releases.
  2. And each release had to have it's own directory in the server.
  3. Did I mention that there was a file size limit for uploads?

I really thought the code manager was going to cry.

We came to the compromise that I would do deltas for him. Each month, I'd bring over only the files that had changed, with file structure preserved. That way, if they did have to pull the code out and redeploy it, he could just cut and paste the latest delta over the first release and hand it over. Doing this by hand? A bitch. Doing it with python and subversion?

A snap.

I had SVN create a file called diff.txt that gave me a list of every file that had changed between this release and our first. Then I wrote a python script that either copied a file into a new folder if it had been added or modified, or made a note in a file called deleted.txt if it had been deleted.

Writing it took me a few hours. Running it takes minutes. Every release, it saves me god knows how many hours of cutting and pasting.

You don't need to make megapiles of script for code to be useful. My useful script, including a probably over done open and read file function, was under 50 lines.

I started off with the advantage of having learned how to program in high school and a bit in college, but I do believe anyone can learn to code. Python is a great place to start. It's light, fast, free, and has a strong community. A few places to start:


I'm a lady of my word, so here's the code:
import os
import shutil

def openFile (filename):
"""Given a filename, read the contents of that file.
"""
f = open(filename)
line = ''
a = []

while 1==1:
line = f.readline()
if line != '':
if line != '\n':
a.append(line)
else:
break
f.close()
return a

def moveFiles (array, base_path):
'''For each different file:
1. Make sure that the directory structure is there.
2. Copy the file over.
'''
deleted_files = []
for line in array:
files = line.rsplit()
if files[0] != "D":
orgfile = base + files[1]
files[1] = "deltas/" + files[1]
newfile = files[1]
newfile = newfile.rsplit("/")
newfile.pop()
dir = ""
for folder in newfile:
if dir == "":
dir = folder + "/"
else:
dir = dir + folder + "/"
try:
os.makedirs(dir)

1 comment:

Anonymous said...

The openFile function can be reduced to the following list comprehension. (It's a little different from the function, in that other types whitespace only lines are excluded from the list, such as tabs.)

[x for x in open(filename) if not x.isspace()]