Tuesday, November 29, 2011

Heroku and Ubuntu

So, I've just spent an hour or two trying to make a new heroku app on my ubuntu machine, and I was getting nowhere.
$git push heroku master
Agent admitted failure to sign using the key.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
I'd followed all the usual help on stackoverflow and heroku's excellent help section but was making no progress. Until I discovered this. The link to the bug report didn't work for me, but it's clear that I needed to set this SSH_AUTH_SOCK=0 environment variable first:
$export SSH_AUTH_SOCK=0
$git push heroku master
And now we're off!

Sunday, October 9, 2011

The Sieve of Eratosthenes in Python

Whilst working on the 10 Io one liners to impress your friends post I felt I needed to turn to python to complete number 10 - the Sieve of Eratosthenes. My intention was to understand it in python as best I could, then simplify the python code until I had one line I could try to translate into Io. This post is about that attempt.
We start off by trying to translate the description in the wikipedia article into python line by line. Which gives us the following.
def esieve(n):
    primes = range(2, n+1)
    p = 2
    while p < n:
        for i in range(p, n+1):
            if p*i in primes:
                primes.remove(p*i)
        p += 1
    return primes
9 Lines isn't bad for a start, but that if statement can be cleaned up. What if instead of looking for items in our list one at a time. We make a new list of items to be removed, and remove them all at the end? We could use a set to hold our lists. This lets us use the minus (-) operator to give us a new set of items not in our marked set.
def shorter_esieve(n):
    marked = set()
    p = 2
    while p < n:
        for i in range(p, n+1):
            marked.add(p*i)
        p += 1
    return sorted(set(range(2, n+1)) - marked)
We only removed one line in that last attempt. Not great. But it looks like we're using a while loop and incrementing each step. Why don't we just do a for?
def shorter_esieve(n):
    marked = set()
    for p in range(2, n+1):
        for i in range(p, n+1):
            marked.add(p*i)
    return sorted(set(range(2, n+1)) - marked)
6 lines, getting better. Now here is the magic. We're using two for loops to generate a set of values. So we can just use a list comprehension to build our list, which we then use to make our marked set.
def shorter_esieve(n):
    marked = set([p* i for p in range(2, n+1) for i in range(p, n+1)])
    return sorted(set(range(2, n+1)) - marked)
And moving the assignment inline
def much_shorter_esieve(n):
    return sorted(set(range(2, n+1)) - set([p*i for p in range(2, n+1) for i in range(p, n+1)]))
And there we have it. The Sieve of Eratosthenes in one line of python. If you'd rather watch the refactoring happening step by step. Here's a video, set to suitable music.

Saturday, October 8, 2011

10 Io one liners to impress your friends

It's been ages since I've done anything with the Io language. But after seeing this spate of 10 [language] one liners to impress your friends in Davide Varvello's post I thought I would spend the evening trying it with Io. Here's the gist: According to Davide's post the others in this category so far are:
Ruby
Scala
CoffeeScript
Haskell
Clojure
Python
Groovy
I'd love to see more. It's a great little activity to get started with a language.

Monday, September 26, 2011

Chicken Scheme on OS X 10.7 (Lion)

I'm making another attempt at going through SICP doing all of the exercises. I've decided that chicken scheme is going to be the way I will do it. But to get it going on Lion there are some hoops to jump through thanks largely to a new version of gcc.

The method I have found that works is.

1) Download the source from the chicken scheme website.

2) Following the advice here you should pass new compiler options to make:
make C_COMPILER=gcc-4.2 PLATFORM=macosx

3) To install the additional eggs you will need to pass new options to csc like so:
CSC_OPTIONS='-cc gcc-4.2' chicken-install

Sunday, September 25, 2011

Software Craftsmanship & The Second Chasm

I've just been reading William Pietri's post Agile's second chasm in which he discusses why ideas, such as agile get watered down as they get taken up by the mainstream. I think there's some stuff hear that applies to the craftsmanship movement. You could probably argue whether or not the craftsmanship movement has passed the first chasm yet, But for the moment lets suppose that it has. What are the chances that we might fall into the second one that William describes in his post?

A Common theme

First off, both ideas have a manifesto - this in itself isn't a bad thing, manifestos are great at getting like minded people talking. The problem is it allows people to 'sign up' to an idea that they have no intention of fulfilling. I've seen CVs that boast about being signatories of the agile manifesto, but have never been part of an agile team. I've been invited to "daily scrum" meetings that were 2-hour-sit-down-and-go-through-spreadsheets-and-gantt-charts meetings. It seems to me that Craftsmanship also has the same "difficult to test" quality to it. Craftsmanship isn't so much a title - as a way of doing things. So you won't really know if someone is a craftsman unless you watch them doing their job over time. I wonder how long it is before I see a CV with a "Software Craftsman" certification, but is willing to implement a mess in order to meet a schedule?

A Difference

The way I see it, there is one important difference with the craftsmanship movement. Its focus is on individuals and their interactions, not teams. Ok, I know words like 'partnership' and 'community' are used in the manifesto, But the point is that it's up to the individual to better themselves and the code they work on, productive customer partnerships are something the individual should seek because it helps them better understand what it is they are being asked to do. The community is all about individuals meeting other individuals to help themselves improve. So how do I think this helps? Because it operates at a completely different level.

Agile has continuous improvement built into it, and of course, if you're doing it properly you will always change and adapt. But it has a core set of practices that are easy to get started with that allow you to call yourself an agile team. But Craftsmanship doesn't have this (at the moment). Try to do a quick google search for what software craftsmanship is, and you'll struggle for a firm definition. The best I've found so far is Uncle Bob's but even that doesn't have any specifics. Really, the only idea behind software craftsmanship is: "We will write the best code we can by constantly reviewing how to write the best code we can". It's a continuous learning process (one that is built into the medical profession by law). Continuous improvement on a personal level is not something that's so easy to fake

Wednesday, September 14, 2011

Python Shelving

I discovered the cool shelve module earlier. Sometimes it's nice to be able to store python objects using the pickle module. The shelve module builds on this by providing a dictionary that you can put objects into and access later. Check out this gist

Tuesday, September 13, 2011

Dojos and Katas

I recently got back from a very successful Socrates 2011 (Software craftsmanship and testing camp) where I spent just over 2 days hanging around with a bunch of smart people and spent the whole time either writing code, or talking about code. I was involved in a lot of discussions about software craftsmanship as well as a few coding dojos. Dojos and Katas were always something I've done when I've felt like it, but never as part of a habit. Following the conference I started using Stefan Roock's great site codersdojo. It allows you to work on a kata, and upload it to the site when you're done. It provides statistics about how long each step took as well as when the tests were passing or failing: Thanks to Stefan for a great site and helping me add Haskell Support! Take a look at my haskell fizzbuzz kata below and please checkout Stefan's site.