<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Integrated Development Blogging</title>
	<atom:link href="http://bourdenas.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bourdenas.wordpress.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Feb 2009 14:47:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='bourdenas.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Integrated Development Blogging</title>
		<link>http://bourdenas.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://bourdenas.wordpress.com/osd.xml" title="Integrated Development Blogging" />
	<atom:link rel='hub' href='http://bourdenas.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Object Composition in Python</title>
		<link>http://bourdenas.wordpress.com/2009/02/19/object-composition-in-python/</link>
		<comments>http://bourdenas.wordpress.com/2009/02/19/object-composition-in-python/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 00:02:51 +0000</pubDate>
		<dc:creator>bourdenas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://bourdenas.wordpress.com/?p=11</guid>
		<description><![CDATA[Background Lately I needed to write some simulation scripts for my work that required intensive mathematical computations. At the beginning, I picked up MATLAB as it is very well known for easy matrix operations and supplies a big collection of libraries that provide &#8220;everything&#8221; for you. Apart from matrix operation, the language has little to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bourdenas.wordpress.com&amp;blog=6641174&amp;post=11&amp;subd=bourdenas&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Background</strong></p>
<p><strong></strong> Lately I needed to write some simulation scripts for my work that required intensive mathematical computations. At the beginning, I picked up MATLAB as it is very well known for easy matrix operations and supplies a big collection of libraries that provide &#8220;everything&#8221; for you. Apart from matrix operation, the language has little to offer, being quite inflexible. Coding in Matlab made me feel like I&#8217;m back to the days of BASIC and Pascal. Even worse when it comes down to OS operations like splitting processes for parallel execution you are left alone. Maybe I could spend a bit more time looking at such matters in Matlab, but soon I discovered three libraries; <a href="http://numpy.org/">numpy</a>, <a href="http://scipy.org">scipy</a> and <a href="http://matplotlib.sourceforge.net/">matplotlib</a> for Python that solved my problems. More or less they are an alternative to Matlab&#8217;s libraries for use in Python.</p>
<p>In this post, I&#8217;ll give my approach on solving an object composition problem in Python based on the decorator pattern, that I doubt I would have been able to solve in Matlab as elegantly. The decorator pattern is defined, in the <a title="GoF Book" href="http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612/ref=pd_bbs_1?ie=UTF8&amp;s=books&amp;qid=1235007522&amp;sr=8-1">GoF book</a>, as an alternative to sub-classing for dynamically extending functionality of objects instead of classes.</p>
<p> </p>
<p><strong>Motivation</strong></p>
<p><strong></strong> As part of the simulations, I needed to code event detectors that were operating on some traces. Each detector works on a stream of input data, using a specific set of features extracted from the incoming stream. While, I wanted to explore and test what would be the most appropriate configuration of detectors, I needed to experiment with different setups. Avoiding getting into too much detail, I wanted to add/remove functionality to my detectors.</p>
<p>A few examples of detector setups would better illustrate the problem. In the simplest case, I wanted to collect some features from a data-set and work separately, logging the results independently. You can consider each rectangle an instance of a class while the arrows denote collaboration between objects. The direction is from the outer objects towards the inner ones, while dashed arrows imply that the object has access to the data.</p>
<p> </p>
<div id="attachment_21" class="wp-caption aligncenter" style="width: 460px"><img class="size-full wp-image-21" title="example1" src="http://bourdenas.files.wordpress.com/2009/02/dec1.png?w=455" alt="Simple decoration of detectors"   /><p class="wp-caption-text">Simple decoration of detectors</p></div>
<p> </p>
<p>In another experiment, I wanted to exploit the correlation of events occurring concurrently in different sources. The setup is similar to the previous one, but instead of logging result independently as in the previous example, I funnel them through a correlation procedure to increase my accuracy of detection. The final aggregate result is then forwarded for logging.</p>
<p> </p>
<div id="attachment_27" class="wp-caption aligncenter" style="width: 460px"><img class="size-full wp-image-27" title="dec2" src="http://bourdenas.files.wordpress.com/2009/02/dec2.png?w=455" alt="Correlating detector outputs"   /><p class="wp-caption-text">Correlating detector outputs</p></div>
<p> </p>
<p>The third picture presents a more complex setup. There are three different detectors, each requiring different features, while a specific detector requires a pair of features. Furthermore, events detected by detectors A and B are correlated. Consequently, feedback from each detector gives insight on the monitored event and increases the decision belief. A detector is placed there performs exactly this procedure by having as input the two originals. Finally, the aggregate result of the detection mechanism is logged.</p>
<p> </p>
<div id="attachment_28" class="wp-caption aligncenter" style="width: 460px"><img class="size-full wp-image-28" title="dec3" src="http://bourdenas.files.wordpress.com/2009/02/dec3.png?w=455" alt="A more complex composition of detectors"   /><p class="wp-caption-text">A more complex composition of detectors</p></div>
<p> </p>
<p>Consequently, a generic scheme was needed for composing objects, while keeping the composition transparent in order to enable client code access interfaces of inner objects. The first example follows exactly the definition of the decorator pattern. However, the rest examples involve more complex compositions of objects where the decoration chain is transformed into a directed acyclic graph (DAG). Considering that decoration is a dynamic alternative for inheritance at object-level the branching can be considered similar to multiple inheritance.</p>
<p> </p>
<p><strong>Why not Inheritance?</strong></p>
<p>Apparently, creating a different detector class for each setup is not optimal. It requires creation of as many fat classes as the number of setups, where code reuse is restricted to copy-paste, hence, none at all. But why not inheritance? The composition looks like a hierarchy tree growing sideways on the graphs. First of all, this is semantically wrong. Inheritance denotes an isA relationship between two classes and in this case this is not true. While it could be a feasible implementation, it would be an abuse of the inheritance mechanism.</p>
<p>Inheritance would also require the definition of a class for each setup required so that an instance can later be created. Finally, composition of objects using inheritance is possible only at compile-time, thus, all possible combinations should be provided as classes for the execution to pick during run-time. It is preferable to delegate composition of objects during run-time, constructing only the exact number of composites that are required.</p>
<p> </p>
<p><strong>Implementation</strong></p>
<p>The decorator pattern is defined as a dynamic technique for adding/removing functionality to an object instance, instead of a class. Dynamic refers to the time of behaviour modification, in our case during run-time. Below is a generic implementation of the decorator pattern in Python 2.6.</p>
<pre>class decorator (attribute_forwarder):
  def __init__(self, *objs):
    self.__inners = []
    self.decorate(*objs)

  def decorate(self, *objs):
   self.__inners += [obj for obj in objs]

  def undecorate(self, *objs):
    if len(objs) == 0:
      self.__inners = []
    else:
      for obj in objs:
        self.__inners.remove(obj)

  def peel(self):
    if len(self.__inners == 1): return self.__inners[0]
    else: return decorator(*self.__inners)</pre>
<p>A class that wants to decorate an instance has just to inherit from the class decorator and define the functionality it needs to provide. The class constructor takes as parameters the objects that are going to be decorated, while decorate() and undecorate() functions can modify the instance&#8217;s behaviour during its lifetime. These functions use the argument packing/unpacking feature of Python to support variable length for argument lists to remain generic. I&#8217;m not convinced where the undecorate() function might be usefull, as it eventually yields a hollow decorator instance, but I include it for the sake of completeness and symmetry.</p>
<p>The function that returns an undecorated version of the inner objects is peel(). In essence, it allows removing the outer layer of functionality and reference to the inner objects. You may have noticed that peel() actually returns a decorator in case of multiple inner objects. This may look like not removing the external layer, but it does actually remove the user decorator. While someone would expect it to return maybe a list of the previously decorated objects it will soon be apparent why a trivial decorator is preferred. Essentially, a trivial decorator can only forward attribute requests to its inner objects.</p>
<p>There is one piece missing though from the implementation; attribute forwarding from the outer towards the inner layers of the composite object. We extend the decorator class to implement this functionality.</p>
<pre>class decorator:
  ...

  def __getattr__(self, fname):
    class callablelist(list):
      def __call__(self, *args):
        return [callable(*args) for callable in self]

    if len(self.__inners) == 1: return getattr(self.__inners[0], fname)
    else: return callablelist(
      [getattr(obj, fname) for obj in self.__inners if hasattr(obj, fname)]
    )</pre>
<p>The original decorator pattern illustrates only chains of decorated objects. However, in this case we deal with graphs that spread and converge. The __getattr__() function is a special function in python that is called when a requested attribute of an object can not be found. In this case, the decorator forwards the attribute request to its inner classes. In case of multiple inner classes, it collects and returns a list with the requested attributes. The inner class callablelist is a subclass of the original python list, which supports the implementation of a generic, transparent decorator object that does not hide the interfaces of its inner objects. Class callablelist is a normal python list that defines the __call__ method, the handler function for calling an object in python, i.e. overloading calling operator. The call behaviour of the callable list is that it calls all its elements and returns a list with their respective return values.</p>
<p> </p>
<p><strong>Examples</strong></p>
<p>So let&#8217;s see how the decorator class helps us with the aforementioned problem of object composition.</p>
<pre>class F1:
  def __call__(self, x):
    return x &gt; 5

class F2:
  def __call__(self, x):
    return x == 2*x-7

class F3:
  def __call__(self, x):
    return x % 2 == 0

class D1(decorator):
  def __init__(self, *objs):
    decorator.__init__(self, *objs)
  def __call__(self, *args):
    retval = self.peel()(*args)
    # decorator processing here
    return retval

class D2(decorator):
  def __init__(self, *objs):
    decorator.__init__(self, *objs)
  def __call__(self, *args):
    retval = self.peel()(*args)
    return reduce(lambda x,y: x^y, retval)

class D3(decorator):
  def __init__(self, *objs):
    decorator.__init__(self, *objs)
  def __call__(self, *args):
    retval = self.peel()(*args)
    # decorator processing here
    return retval

class D4(decorator):
  def __init__(self, *objs):
    decorator.__init__(self, *objs)
  def __call__(self, *args):
    retval = self.peel()(*args)
    return reduce(lambda x,y: x or y, retval)

class Corr(decorator):
  def __init__(self, *objs):
    decorator.__init__(self, *objs)
  def __call__(self, *args):
    retval = self.peel()(*args)
    return reduce(lambda x,y: x and y, retval)

class Log(decorator):
  def __init__(self, *objs):
    decorator.__init__(self, *objs)
  def __call__(self, *args):
    print self.peel()(*args)</pre>
<p>The code presents mock definitions of the classes mentioned in the motivation section. The core classes are the features (F1, F2 and F3) that yield some kind of binary feature based on their input. The decorated function in this case is going to be the __call__(), i.e. overloading call operator. D1 and D3 are two detectors that derive from the decorator class and simply return the input of the inner object. Apparently, being decorators, they do add functionality in their implementation that I omit for simplicity. D2 and D4 detectors are expecting input from one or more inner objects and reduce the output with some technique to a single value. The former does a XOR operation on the return values while the latter performs OR. Similarly, Corr class can be considered as another detector that reduces values using the AND operation. Finally, the Log class prints the return value of its inner objects.</p>
<p>You may have noticed that decorators use peel() internally to propagate the call to inner objects <span>uniformly</span> whether they encapsulate one or several objects. This is achieved by the __getattr__() function defined in decorator in combination to the callablelist class.</p>
<p>Not we can easily compose all the setups we want by assembling small units of computation. I define the three initial examples while for the first instead of logging each detector&#8217;s output individually, as the diagram suggests, I just aggregate them in a single Log class.</p>
<pre>x = Log( D1(F1()), D1(F2()), D1(F3()), )

y = Log(
  Corr(
    D1(F1()),
    D1(F2()),
    D1(F3()),
  )
)

z = Log(
  D4(
    D1(F1()),
    D2(F1(), F2()),
  ),
  D3(F2()),
)

x(7), y(7), z(7)</pre>
<p>The output of the program (in the case where D1 and D3 don&#8217;t change the inner retval) is:</p>
<pre>[True, True, False]
False
[True, True]</pre>
<p> </p>
<p><strong>Limitations</strong></p>
<p>That is a fairly expressive object composition tool. It leads to decomposition of the problem in small tasks that can be composed and adapted during run-time. This is actually a consequence of the decorator pattern. There are some limitations, however, with this implementation. Arguably, the most important one is the fact that decorators have to inherit from the decorator class, so it&#8217;s kind of intrusive to the class. A side-effect of this is also that decorate(), undecorate() and peel() are reserved methods and should not be redefined by decorator classes for with different semantics.</p>
<p>Moreover, with the current implementation it is possible to create circles on object composition, that will eventually lead to a stack overflow exception because of recursion. This is easily implementable by defining a function isPart() that would search recursively through a composite object to check if an instance is part of the composite. It is adequate to assert the non existence of the instance inside the decorate() as a precondition. It is left out from the implementation for simplicity.</p>
<br /> Tagged: design patterns, python <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bourdenas.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bourdenas.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bourdenas.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bourdenas.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bourdenas.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bourdenas.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bourdenas.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bourdenas.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bourdenas.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bourdenas.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bourdenas.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bourdenas.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bourdenas.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bourdenas.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bourdenas.wordpress.com&amp;blog=6641174&amp;post=11&amp;subd=bourdenas&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bourdenas.wordpress.com/2009/02/19/object-composition-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/02b074b41af6e80e888fbf44c5f41878?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=X" medium="image">
			<media:title type="html">bourdenas</media:title>
		</media:content>

		<media:content url="http://bourdenas.files.wordpress.com/2009/02/dec1.png" medium="image">
			<media:title type="html">example1</media:title>
		</media:content>

		<media:content url="http://bourdenas.files.wordpress.com/2009/02/dec2.png" medium="image">
			<media:title type="html">dec2</media:title>
		</media:content>

		<media:content url="http://bourdenas.files.wordpress.com/2009/02/dec3.png" medium="image">
			<media:title type="html">dec3</media:title>
		</media:content>
	</item>
	</channel>
</rss>
