Andrew Channels Dexter Pinion

Wherein I write some stuff that you may like to read. Or not, its up to you really.

October 14, 2003

Kata 18

A while ago, I mentioned Dave Thomas' Kata. Well, I've taken the plunge in a vain attempt to stretch my brain and had a bash at his latest. My solution is in Python and doesn't (yet) use generators, but its a good start. The code is here and;

"""
A module trying out Kata Eighteen (http://www.pragprog.com/pragdave/Practices/Kata/KataEighteen.rdoc,v)

For ease of readability I've included the two classes in the same module.
"""

import unittest

class depend(object):
    def __init__(self):
        self.dependencies={}

    def add_direct(self, item, dependencies):
        self.dependencies[item]=dependencies
    def dependencies_for(self, item, results=[]):
        if self.dependencies.has_key(item):
            for dep in self.dependencies[item]:
                if dep not in results:
                    results.append(dep)
                    children=self.dependencies_for(dep, results)
        return results

class testDepend(unittest.TestCase):
    def setUp(self):
        self.mine = depend()
        self.mine.add_direct('A', 'B')
        self.mine.add_direct('B', 'C')
        self.mine.add_direct('C', 'A')

    def testA(self):
        "Make sure that A depends on B and C"
        self.assertEqual(self.mine.dependencies_for('A', []), ['B', 'C', 'A'])

if __name__ == '__main__':
    unittest.main()

Corrections and comments are, as always, welcome.

Posted by Andy Todd at October 14, 2003 11:14 PM

Comments