Text

I am determined to write some Scala code with Akka this weekend. Not sure what exactly though.

Text

I did the Pi tutorial from the Akka docs yesterday. It was fun.

Text

After hours of troubleshooting, I’m starting to reconsider my initial enthusiasm for Ruby metaprograming!

Text

Narrowing my focus

I spend a lot of time learning. It came apparent to me during my recent interviewing (see below) that, although I may spend a lot of time learning languages and programming theory, my demonstrable abilities appears rather light. I heard a quote (that I will surely mis-quote) today that crystalized this thought in my mind. If you do not create, you will be known for your tastes. To be known by your ability, you must create. I believe that this caution applies very much to me right now. I think that I have good taste in the tools and techniques that I choose to learn. Most of my actual coding has been for class work and for my job (developing proprietary software that can not become part of my public portfolio). I’ve used spare time for self selected study (and of course for family time). As a result I have become better known for my obscure taste than for awesome coding ability. It is time now for me to focus on my abilities and to create. I have decided to narrow my educational vision and tool emphasis for a while. I am going to limit my focus to Ruby/JRuby and Scala (with Lift, Rails, and CoffeeScript for web development). I’m not going to worry much about learning Lisp and Clojure (leave it for another day). I am also taking some time off of school, perhaps a year. I hope to spend my time creating with a narrower set of tools. I want to be remembered by my abilities and I want to excel at a few things rather than be effective in many things.

Text

Update from Steve

I’m not a very good blogger, as you can probably tell by how rarely I post. So if you are here, thank you for reading. If you know me in person, definitely friend me on Facebook where I post quite often.

Recently I switched jobs. I left Boeing where I had successfully changed careers from roles such as IT Engineer/Analyst to Software Engineer. Since starting at Boeing I learned Python, Java, Scala, and a few other languages. I also started a BSCS degree program. My new position is with BookRenter.com, a startup where we rent textbooks to college students. This is great opportunity where I am working with an amazing team of talented developers, primarily in Ruby. The new position has enabled my immediate family to relocate closer to most of our extended family. It is a big move and has required a lot from many people (particularly the kids who are in new schools), but it is a positive step for our growth as a family.

With the job change comes a programming language change. Back to Ruby. I started learning Ruby and RoR in 2005 before joining Boeing (and switching to Python). Ruby is an awesome language. For the last couple of months I have been focusing largely on relearning Ruby. I think that Ruby’s metaprogramming is almost a separate coding style. I am probably a “tool oriented” programmer, and I get a thrill learning new styles. I might classify Ruby as a hybrid of OOP, FP, and metaprogramming. Well, I’m having fun. I’m also learning a great deal about managing a large codebase and working as a part of a modern development team.

Text

Learning Clojure (and Lisp) has prompted me to second guess the object-oriented programming model. I’ve been processing it for a few weeks (code in data structures instead of in objects). Now, whenever I am stuck writing some OO boiler-plate code, I can’t help considering it. I’ll have to try doing some real Lisp-like programming (solving real problems) to know for sure. This will take a while.

Text

Got insomnia. Watching Clojure Concurrency by Rich Hickey.

Text

I got see some concurrent Clojure code while reading Programming Concurrency on the JVM. The syntax is similar to Lisp and the concurrent programming support (baked in) is very nice.

Text

Artisan Breads Every Day on Kindle

I’ve been craving good, fresh multigrain break lately. Unfortunately, I lost (lent out?) my copy of Artisan Breads Every Day by Peter Reinhart. Since I can’t live without good bread, I just went to Amazon to buy another copy of the book. To my pleasure, I found that all of Reinhart’s books are available in kindle editions. So cool. Normally I don’t want a cookbook on the Kindle (I like flipping through color photos), but I think Reinhart’s bread books are an exception. I’d read Artisan Breads Every Day cover-to-cover as there was plenty to study. This makes a kindle edition desirable, though I might actually want both digital and hardback versions.

Here is some lean bread that I made last year.

Lean Bread
Text

Method Chaining in Python with Iterators

I have been reading about Scala and learning improved coding practices along the way.  In additional to wishing that I could use such a cool language at work, I’ve also been trying to apply what I’m learning towards improving my Python code.  One of these “scalaisms” is the encouragement of method chaining.  I think I must have considered method chaining a bit too smelly to really give it a shot.  Once I saw some examples in a couple of Scala books, though, my mind opened up to the idea.  Thinking about method chaining in Python while reading about iterators and generators (reading about functional concepts in Python) led me to consider applying both concepts.  I came up with the idea of chaining iterators as a way to iterate over and filter large lists.  A nice thing about iterators is that they are lazy.  They only return their contents one item at a time.  I wanted to chain iterators that could be dynamically plugged together to filter the contents of a list lazily.  The rest of this blog post will be about my proof-of-concept code.

First, I needed something to iterate over. I decided to work with objects that represented pets in a pet store (the simplest idea I could come up with).

class Pet(object):
    @staticmethod
    def from_list(args_list):
        kwargs = dict()
        type_str = args_list[0]
        if type_str == "Dog":
            pet_clss = Dog
        elif type_str == "Cat":
            pet_clss = Cat
        else:
            raise ValueError("Unknown Pet type '%s'" % type_str)
        return pet_clss(**dict([[args_list[x], args_list[x+1]] for x in range(1, len(args_list)-1, 2)]))

    def __init__(self, **kwargs):
        self.name = kwargs['name']
        self.breed = kwargs['breed']
        self.owner = kwargs.get('owner', None)
        self.color = kwargs['color']

    def was_sold(self):
        if self.owner is not None:
            return True
        else:
            return False

class Cat(Pet):
    def __init__(self, **kwargs):
        Pet.__init__(self, **kwargs)
        self.meows = kwargs.get("meows", True)

class Dog(Pet):
    def __init__(self, **kwargs):
        Pet.__init__(self, **kwargs)
        self.barks = kwargs.get("barks", True)

I wanted to be able to load data from a CSV file, hence the from_list() constructor.  Here is a parent class called Pet which is inherited by Cat and Dog.

The rest of the code is based on FilterablePetStore.  This is an abstract class that I used to test two different approaches to implementing a filterable PetStore class.

class FilterablePetStore(object):
    def __iter__(self):
        raise NotImplementedError()

    def filter(self, filter_func):
        return PetStoreView(self, filter_func)

    def was_sold(self):
        return self.filter(lambda pet: pet.was_sold())

    def is_type(self, test_type):
        return self.filter(lambda pet: isinstance(pet, test_type))

    def is_color(self, color):
        return self.filter(lambda pet: pet.color.upper() == color.upper())

    def is_tabby(self):
        return self.filter(lambda pet: pet.color.upper().endswith("TABBY"))

    def as_list(self):
        return [pet for pet in self]

FilterablePetStore just contains some ideas of how a pet store might want to filter their list of pets.  Notice also that filter() method takes a function as its argument, and all of the other methods simple reuse the filter() method by supplying a function.

First I’ll show the simple way to implement method chaining in Python.  This does not use iterators.  Each method in the chain returns a new object which contains its own list of pets.  (I actually coded this one second, hence the ‘2’ in the name).

class PetStore2(FilterablePetStore):
    def __init__(self, list_of_pets):
        self.pets = list_of_pets

    def __iter__(self):
        return iter(self.pets)

    def filter(self, filter_func):
        return PetStore2(filter(filter_func, self.pets))

    def as_list(self):
        return self.pets

This should be pretty straight forward.  The filter() method returns a new PetStore2 instance with a new (possibly smaller) list of pets.  Additional methods are inherited from FilterablePetStore.

The more interesting implementation uses iterators.  The goal of this code was to lazily evaluate each call to next() (see iterators) all the way through the chain of methods.

class PetStore(FilterablePetStore):
    def __init__(self, list_of_pets):
        self.pets = list_of_pets

    def __getitem__(self, index):
        assert isinstance(index, int), "Index must be an integer"
        return self.pets[index]

    def __iter__(self):
        return PetStoreView(self, lambda pet: True)

class PetStoreView(FilterablePetStore):
    def __init__(self, pets, filter_func):
        self._pets = pets
        self._location = 0
        self._filter_function = filter_func

    def __iter__(self):
        return self

    def next(self):
        try:
            try:
                next_item = self._pets.next()
            except AttributeError:
                next_item = self._pets[self._location]
                self._location += 1
        except IndexError:
            raise StopIteration()
        if self._filter_function(next_item):
            return next_item
        else:
            return self.next()

Here the iterator is implemented by the PetStoreView class.  A potential drawback of this approach is that you don’t end up with a PetStore object, but since the goal was to create an iterator chain, this isn’t a big deal.  Each element is passed down the filter chain and, if it passes all of the filters, is iterated over.  Each element waits until the next iteration before being filtered; there are no intermediary lists.

So what is my conclusion?  That will have to come in another post!

Here is the code for anyone who wants to play with it.