<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Father, CS student, and journeyman developer

With a passion for the craft of making software, I’m constantly trying to achieve a higher state of code</description><title>Stephen</title><generator>Tumblr (3.0; @stevesj76)</generator><link>http://www.stevesloan.com/</link><item><title>After hours of troubleshooting, I&amp;#8217;m starting to reconsider my initial enthusiasm for Ruby...</title><description>&lt;p&gt;After hours of troubleshooting, I&amp;#8217;m starting to reconsider my initial enthusiasm for Ruby metaprograming!&lt;/p&gt;</description><link>http://www.stevesloan.com/post/9449802901</link><guid>http://www.stevesloan.com/post/9449802901</guid><pubDate>Sat, 27 Aug 2011 01:51:50 -0700</pubDate></item><item><title>Learning Clojure (and Lisp) has prompted me to second guess the object-oriented programming model. ...</title><description>&lt;p&gt;Learning Clojure (and Lisp) has prompted me to second guess the object-oriented programming model.  I&amp;#8217;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&amp;#8217;t help considering it.  I&amp;#8217;ll have to try doing some real Lisp-like programming (solving real problems) to know for sure.  This will take a while.&lt;/p&gt;</description><link>http://www.stevesloan.com/post/5876581728</link><guid>http://www.stevesloan.com/post/5876581728</guid><pubDate>Thu, 26 May 2011 14:54:00 -0700</pubDate></item><item><title>Got insomnia.  Watching Clojure Concurrency by Rich Hickey.</title><description>&lt;p&gt;Got insomnia.  Watching &lt;a href="http://blip.tv/file/812787"&gt;Clojure Concurrency&lt;/a&gt; by Rich Hickey.&lt;/p&gt;</description><link>http://www.stevesloan.com/post/5097943894</link><guid>http://www.stevesloan.com/post/5097943894</guid><pubDate>Sun, 01 May 2011 04:47:00 -0700</pubDate><category>clojure</category></item><item><title>I got see some concurrent Clojure code while reading Programming Concurrency on the JVM.  The syntax...</title><description>&lt;p&gt;I got see some concurrent &lt;a href="http://clojure.org/"&gt;Clojure&lt;/a&gt; code while reading &lt;a href="http://www.goodreads.com/book/show/11118624-programming-concurrency-on-the-jvm"&gt;Programming Concurrency on the JVM&lt;/a&gt;.  The syntax is similar to Lisp and the &lt;a href="http://clojure.org/concurrent_programming"&gt;concurrent programming&lt;/a&gt; support (baked in) is very nice.&lt;/p&gt;</description><link>http://www.stevesloan.com/post/4994168589</link><guid>http://www.stevesloan.com/post/4994168589</guid><pubDate>Wed, 27 Apr 2011 14:55:00 -0700</pubDate></item><item><title>Method Chaining in Python with Iterators</title><description>&lt;p&gt;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&amp;#8217;ve also been trying to apply what I&amp;#8217;m learning towards improving my Python code.  One of these &amp;#8220;scalaisms&amp;#8221; is the encouragement of &lt;a title="Method Chaining" href="https://secure.wikimedia.org/wikipedia/en/wiki/Method_chaining"&gt;method chaining&lt;/a&gt;.  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 &lt;a title="Python Functional Programming How-to" href="http://docs.python.org/howto/functional.html#iterators"&gt;reading about iterators&lt;/a&gt; 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.&lt;/p&gt;
&lt;p&gt;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).&lt;/p&gt;
&lt;pre class="prettyprint lang-python"&gt;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)&lt;/pre&gt;
&lt;p&gt;I wanted to be able to load data from a CSV file, hence the &lt;em&gt;from_list()&lt;/em&gt; constructor.  Here is a parent class called &lt;strong&gt;Pet&lt;/strong&gt; which is inherited by &lt;strong&gt;Cat&lt;/strong&gt; and &lt;strong&gt;Dog&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The rest of the code is based on &lt;strong&gt;FilterablePetStore&lt;/strong&gt;.  This is an abstract class that I used to test two different approaches to implementing a filterable &lt;strong&gt;PetStore&lt;/strong&gt; class.&lt;/p&gt;
&lt;pre class="prettyprint lang-python"&gt;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]&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;FilterablePetStore&lt;/strong&gt; just contains some ideas of how a pet store might want to filter their list of pets.  Notice also that &lt;em&gt;filter()&lt;/em&gt; method takes a function as its argument, and all of the other methods simple reuse the &lt;em&gt;filter()&lt;/em&gt; method by supplying a function.&lt;/p&gt;
&lt;p&gt;First I&amp;#8217;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 &amp;#8216;2&amp;#8217; in the name).&lt;/p&gt;
&lt;pre class="prettyprint lang-python"&gt;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&lt;/pre&gt;
&lt;p&gt;This should be pretty straight forward.  The &lt;em&gt;filter()&lt;/em&gt; method returns a new &lt;strong&gt;PetStore2&lt;/strong&gt; instance with a new (possibly smaller) list of pets.  Additional methods are inherited from &lt;strong&gt;FilterablePetStore&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The more interesting implementation uses iterators.  The goal of this code was to lazily evaluate each call to &lt;em&gt;next()&lt;/em&gt; (see iterators) all the way through the chain of methods.&lt;/p&gt;
&lt;pre class="prettyprint lang-python"&gt;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()&lt;/pre&gt;
&lt;p&gt;Here the iterator is implemented by the &lt;strong&gt;PetStoreView&lt;/strong&gt; class.  A potential drawback of this approach is that you don&amp;#8217;t end up with a &lt;strong&gt;PetStore&lt;/strong&gt; object, but since the goal was to create an iterator chain, this isn&amp;#8217;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.&lt;/p&gt;
&lt;p&gt;So what is my conclusion?  That will have to come in another post!&lt;/p&gt;
&lt;p&gt;Here is &lt;a href="https://github.com/SteveSJ76/Steve-Code-Examples/blob/master/Python/IteratorFilter.py"&gt;the code&lt;/a&gt; for anyone who wants to play with it.&lt;/p&gt;</description><link>http://www.stevesloan.com/post/4599392334</link><guid>http://www.stevesloan.com/post/4599392334</guid><pubDate>Wed, 13 Apr 2011 21:01:00 -0700</pubDate><category>scala</category><category>python</category></item><item><title>High Level Concurrency with JRuby and Akka Actors</title><description>&lt;a href="https://metaphysicaldeveloper.wordpress.com/2010/12/16/high-level-concurrency-with-jruby-and-akka-actors/"&gt;High Level Concurrency with JRuby and Akka Actors&lt;/a&gt;: &lt;p&gt;I’m learning Scala (which admittedly has required more effort from me than learning Ruby or Python did) largely because I want to use Erlang like actors in an OO style language.  That original goal has been expanded since I have learned the many ways that Scala improves the programming process beyond just actors.  Still, here is another option that meets my original goal without learning Scala: JRuby with Akka actors.  This is a very compelling option for concurrent programming in a high productivity language.&lt;/p&gt;</description><link>http://www.stevesloan.com/post/2599058808</link><guid>http://www.stevesloan.com/post/2599058808</guid><pubDate>Tue, 04 Jan 2011 12:42:05 -0800</pubDate></item><item><title>Interfacing with DOORS using Python Socket</title><description>&lt;p&gt;For as long as I&amp;#8217;ve been doing DXL development, I&amp;#8217;ve desired ways to interface DOORS with languages besides DXL.  This desire was rooted in my prior experience with writing automation scripts for Rational ClearQuest.  Though quirky, I had found CQ very easy to automate using COM.  Rational DOORS, on the other hand (owing most likely to its Telelogic origins) provides very little in the way of COM automation or any other interface besides DXL for automation.  It is my opinion that this design decision is a legacy form of lock-in for the DXL language (and associated consulting charges).  The COM automation for DOORS is limited to launching DOORS, sending it DXL code, hoping that the code succeeds, and then possibly receiving some string output.  (Note that the opposite is true when DOORS is the client; automating other COM components is reliable, if tedious).&lt;/p&gt;
&lt;p&gt;In addition to this simple COM server capability, DOORS also provides a similar socket server.  It still requires automating DOORS using a fair amount of DXL code. If we accept that we are going to have to muck around with DXL code, possibly dedicating it towards providing an API that is driven by code written in another language over the socket, we can still manage to get a lot of work done.  The usage of such a system seemed obscure to me at first (the documentation didn&amp;#8217;t help a ton), but it was actually pretty easy to create a simple socket server in DXL and a socket client in Python.  This is is where Python really shines.&lt;/p&gt;
&lt;p&gt;Here is the &lt;a href="https://github.com/SteveSJ76/Steve-Code-Examples/blob/master/DXL/IPC_Server.dxl"&gt;DXL socket server code&lt;/a&gt;.  This code can be executed in the DXL interactive editor.  It will lock up the client GUI while the code loops waiting for connections.  Connecting clients can then send arbitrary DXL code or the keywords &amp;#8220;quit_&amp;#8221; (to disconnect) or &amp;#8220;shutdown_&amp;#8221; (to shutdown the DXL socket server).&lt;/p&gt;
&lt;p&gt;Here is the &lt;a href="https://github.com/SteveSJ76/Steve-Code-Examples/blob/master/DXL/IPC_Client.py"&gt;Python socket client code&lt;/a&gt;.  First, to shutdown the DXL socket server, run the client script like &amp;#8220;python IPC_Client.py &amp;#8212;shutdown&amp;#8221;.  This will unblock the DOORS GUI.  Otherwise, run the script without any arguments and the three print_* functions will send DXL code to the socket server and then print the returned results.&lt;/p&gt;</description><link>http://www.stevesloan.com/post/2425245906</link><guid>http://www.stevesloan.com/post/2425245906</guid><pubDate>Wed, 22 Dec 2010 19:29:00 -0800</pubDate><category>dxl</category><category>python</category></item></channel></rss>

