Cookbook: PureTransformer

A number of components have a common format - specifically they take every piece of data they receive, transform it somehow, and pass the data on. Take for example this exampe from the HTTPClient page:
import feedparser
import pprint
import Axon
from Kamaelia.Chassis.Pipeline import Pipeline
from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer
from Kamaelia.Protocol.HTTP.HTTPClient import SimpleHTTPClient

class Feedparser(Axon.Component.component):
def main(self):
while 1:
while self.dataReady("inbox"):
data = self.recv("inbox")
parseddata =
feedparser.parse(data)
self.send(parseddata, "outbox")
if not self.anyReady():
self.pause()
yield 1

class
PrettyPrinter(Axon.Component.component):
def main(self):
while 1:
while self.dataReady("inbox"):
data = self.recv("inbox")
prettified =
pprint.pformat(data)
self.send(prettified, "outbox")
if not self.anyReady():
self.pause()
yield 1


Pipeline(
ConsoleReader(eol=""),
SimpleHTTPClient(),
Feedparser(),
PrettyPrinter(),
ConsoleEchoer(),
).run()

In the example above, the important part of the example is highlighted red the common parts of the components are highlight in green. Clearly this amount of boiler plate code actually hides what's actually going on here. As a result the PureTransformer component exists.

To show how this works, we'll leave the important part of the above example red, but show the shorter version here:

import feedparser
import pprint
import Axon
from Kamaelia.Chassis.Pipeline import Pipeline
from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer
from Kamaelia.Protocol.HTTP.HTTPClient import SimpleHTTPClient
from Kamaelia.Util.PureTransformer import PureTransformer

Pipeline(
ConsoleReader(eol=""),
SimpleHTTPClient(),
PureTransformer(feedparser.parse), # Feedparser
PureTransformer(pprint.pformat), # PrettyPrinter
ConsoleEchoer(),
).run()
The biggest downside of the PureTransformer is that you sometimes lose the intent behind a component due to the reused name. As a result, creating a prefab is sometimes a nice idea. What's a prefab? It's simply a function !

Using a Prefab to show Intent

import feedparser
import pprint
import Axon
from Kamaelia.Chassis.Pipeline import Pipeline
from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer
from Kamaelia.Protocol.HTTP.HTTPClient import SimpleHTTPClient
from Kamaelia.Util.PureTransformer import PureTransformer

def Feedparser(): return PureTransformer(feedparser.parse)
def PrettyPrinter(): return PureTransformer(pprint.pformat)
 
Pipeline(
ConsoleReader(eol=""),
SimpleHTTPClient(),
Feedparser(),
PrettyPrinter(),
ConsoleEchoer(),
).run()

The nice thing here is we now have the best of both worlds - the code in the pipeline shows the intent, whereas the code performing the transforms now doesn't contain any repetition. The other nice thing about doing things this way is that you gain correct shutdown handling for free at the same time! :-)

-- Michael Sparks, December 2006

 

Kamaelia is an open source project originated from and guided by BBC Research. For more information browse the site or get in contact.

This is an ongoing community based development site. As a result the contents of this page is the opinions of the contributors of the pages involved not the organisations involved. Specificially, this page may contain personal views which are not the views of the BBC. (the site is powered by a wiki engine)

(C) Copyright 2008 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved

This web site is powered by the same code created for the bicker manor project. For more details, contact Michael Sparks at BBC Research directly (cf contact)