Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, April 8, 2009

How I learned to love hockey (and what that has to do with programming)

Growing up, I was not a sports fan in the least. I didn't enjoy watching them, I had no interest in learning the rules, and I had NO interest in playing any of them. In gym, I was so accident prone that I had to sit with the asthma kids, and I was damned happy to do so. I preferred playing SET to kicking a ball around.

I dated a few guys that were into sports. In the interest of being a couple, they'd try to teach me the rules while watching a game on TV. Liking systems, I'd try my best to learn. Football was the first attempt. I'd post some of the repartee, but honestly, it bored me so terribly, I can't even dredge up the few things that I did manage to pick up. What I came up with could probably fit on a 3x5 card, and have room for my Superbowl Dip recipe.

Fortunately, I married a guy who, while interested in sports, understood that I had no interest in watching or understanding them. I thought that would simply be something I'd never get into.

Then came Jim.

Jim, a friend of mine, has season tickets to the Washington Capitals, and invited me to join him for a game. Really, I was just interested in a night out rather than learning about hockey. For the first few games, I was just there to BS with Jim. He even joked about how I didn't know what the hell was going on.

Jim (watching me trying to look attentive): So, you know what they're doing?

Me: Oh, sure!

Jim: Okay. What team is our team?

(Long, painful pause)

Me: They're... in... white?

Jim: You know, you can ask if you don't get what's going on.

No, no, I was good. I was just there for the company and the Boardwalk Fries. I don't think I learned a damn thing that first season.

At least, that's what I thought.

The second season, I again joined Jim for weekend games, but I noticed something different. I was beginning to understand the game. I could tell when something bad happened. When the Caps were playing well. When they were having a rough night. When a player seemed 'off.'

Now, keep in mind, Jim hadn't explained any rules to me. I hadn't read anything. There wasn't anything I could think of that would have given my an understanding of the game at all. So, where had it come from?

I paid attention to the next few games, and started to get clues as to where this implicit knowledge had come from: the crowd. The crowd was filled with people who did, in fact, know hockey. They knew what a bad call was, they reacted to a team that was on fire, and a team that was tired and run down. In general, there's a buzz during games: people chatting, moving about, coughing, shuffling between seats. When things happen, that buzz turns to shouts... or silence. The changes in volume snap my attention to what was going on. I'd subconsciously replay the last few seconds in my head and try to map that to the sound.

I tested my implicit knowledge on my expert. While I was still very fuzzy, I was on the right track. With this in hand, I actually started looking up some of the rules that interested me, like penalties.

So what does this have to do with learning?

Rules are boring

They really, really are. I don't know many people who find rules interesting. Some people do become rule dictionaries, but I guarantee, the interest came first. People don't learn the rules first: they find the interest.

So when it comes to programming, why do we focus on the rules first? Loving computers and, more importantly, loving code should come first. However, every class I've ever been in has started with data structures and variables and syntax and, well, the rules.

Why not show them the buzz first? Take them to a users group in your area and let them see people talking about what they do. Most coders are more than happy to talk about their work, and are excited to talk about whatever neat thing has their attention at the moment. If you have someone you're really interested in seeing excel, see if you can get them to a conference. It's one thing to look at code and understand what an if statement is. It's another matter completely to understand the community that goes with it.

Rules are a good resource

Does this mean we should eschew rules completely? Of course not. They're a good resource. Once I was in love with hockey, I wanted to know more about the sport. While I'm no rulebook, I do know what the penalties are, the types of players, and some of the strategy behind the game. Hell, this year, I've even learned about standings, and will sometimes check up on other teams to see what their chances of overtaking us are.

As a coder learns more he/she will want to know more. They'll want to know more syntax, or how to make their code more efficient. They'll look at new frameworks and languages. They'll become an evangelist. If all we ever make are cube-rats, all we'll ever get is loveless code that does barely what it's supposed to do, and never anything more.

And by the by... Go Caps! Southeast Division Champs!

Thursday, April 2, 2009

Stage fright

I'm notorious for not getting stage fright. Since I was still in saddle shoes, I could get up in front of any number of people and spout off about pretty much anything. Impromptu, structured, slide-laden, scripted: none of it has ever been a real problem for me.

Then why do I get stage fright when it comes to posting code?

In my line of work, I write a lot of small, helpful scripts to take some of the monotony out of certain parts of my job. There aren't many functions I have left that don't have at least one py scrit associated with it.

At PyCon, I mentioned that I had written a library for Trac that printed out reports. It started out as someone wanting a listing of what had happened to my tickets over the past month. True, one can simply do a query against the database, but there's something quirky about the Trac database:

It's done in sqlite.

Sqlite SUCKS when it comes to date functions.

All dates are in unixtime. There are no functions for dates. You can't subtract a certain number of seconds from a unixtime date in a query. You can't call some cute function that gives you a date range. You have to work out the unixtime on your own and input it.

Sqlite people, I love you for making a lightweight database. I HATE you for this one oversight. Really? You didn't think people would care about time? Developers are cool, but not so cool that we've ascended the space/time continuum and live on a plane where all times are now and therefore irrelevant.

Anyway, after discovering this oversight, and getting tired of using online converters (no, I cannot do unixtime in my head), I decided to write a program that would do it for me. It hit against trac.db and got me all tickets for a month based on milestone. Even printed me a nice report.

After discovering how to do that, I got more requests for Trac reports. Over time, the library grew, function by function.

Anyway, the fellow Pythonista asked if I could share my code with him. Being the gregarious sort I am, I said sure! Upon returning home, I actually looked at the code, and began to panic.

Oh, crap. Now I knew how all those people who hated public speaking felt. Unfun. In order to stem the tide of dread, I started to analyze what had me so concerned.

Organic

Most of the need for small scripts grows organically out of a particular need. This leads to some strange code building strategies, like making functions too large, or forgetting to add docstrings. Or making functions -too- specific (I have several that make SQL queries and return the results. Surely this could be streamlined?).

It's the way of cubeland: someone catches on that you have a fast way to do one task, and you have people testing you with every variation of that task they can come up with.

Unique to me?

Is the code I write really useful to anyone else? I mean, I had a very specific need. Who else could possibly need a report that spits out all tickets that have been touched in the past ten days? Are other people really that obsessed with the Trac database? If there are, is there a support group I can join?

Can anyone else even understand this crap?

This fear came over me with my first programming class my sophomore year of high school, and has never really left. Can people understand the crap I write? Most of my work scripts are never meant to see the light of day. They sit quietly in a jumble, waiting for me to pop in and feed them some data, then get out. They're like the red-headed stepchildren of lore. Good for doing chores, but don't take them to the ball.

But it's just 300 lines!

Three hundred lines isn't a lot. I'm not sure how many lines make up the major frameworks that are about today, but it's sure as hell more than 300. Three hundred lines of code really rank their own Google code project?

At the end of the day, I decided to toss those worries aside and post the code. When I do public speaking, my last thought is often "What's the worse that could happen? Someone shoots me on stage? At least I'll end up on TV." What's the worse thing that could happen if I post some code? Someone ends up not using it? At worse, I get told what's wrong with the code, and I get a chance to learn and fix it up.

So the red-head is going to the ball. I encourage everyone else to take out their own step-children and see if they can do some other people some good.

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)