All pastes #249766 Raw Edit

Cody Brocious

public python v1 · immutable
#249766 ·published 2006-11-16 03:30 UTC
rendered paste body
#!/usr/bin/env python# encoding: utf-8"""testBattery.pyCreated by  on 2006-11-14.Copyleft (c) 2006 Cody Brocious All rights reversed."""from interface import interfacefrom model import modelfrom queryableModel import queryableModelclass pet(interface):    __public_attributes__ = ('name', 'age', 'species')        def __init__(self, name, age, species):        self.name = name        self.age = age        self.species = species        def __repr__(self):        return '%s named %s is %i years old' % (`self.species`, `self.name`, self.age)def testQueries():    testObj = queryableModel()    testObj.childAdd(pet('Daeken', 18, 'Human'))    testObj.childAdd(pet('Nikashera', 18, 'Neko'))    testObj.childAdd(pet('Joe Bob', 72, 'Human'))    testObj.childAdd(pet('Dean', 76, 'Human'))    testObj.childAdd(pet('Rudy', 15, 'Dog'))    testObj.childAdd(pet('Blu', 10, 'Cat'))    testObj.childAdd(pet('S4f3_M0D3', 18, 'Human'))    testObj.childAdd(pet("S4f3's girlfriend", 12, 'Human'))    testObj.childAdd(pet('Jessica', 23, 'Neko'))    testObj.childAdd(pet('Crypto', 19, 'Dragon'))        print 'Humans:', testObj.query(lambda child: child.species == 'Human')    print '18 year olds:', testObj.query(lambda child: child.age == 18)    print 'Seniors:', testObj.query(lambda child: child.age > 65)    print 'Humans under 20:', testObj.query(lambda child: child.species == 'Human' and child.age < 20)    print 'Humans under 20 (alternative):', testObj.query(lambda child: child.species == 'Human').query(lambda child: child.age < 20)    print 'Minors:', testObj.query(lambda child: child.age < 18)class attrTree(queryableModel):    __public_attributes__ = ('name', )        def __init__(self, name):        model.__init__(self)                self.name = name        @interface.public    def __repr__(self):        ret = 'Tree %s:\n' % str(self.name)                for child in self.children:            temp = `child`.split('\n')                        if len(temp) == 1:                ret += '\t%s\n' % temp[0]            else:                temp = ['\t' + line for line in temp]                ret += '\n'.join(temp) + '\n'                return ret.strip()def findStrings(child):    result = child.query(findStrings)        return resultdef testTreeQueries():    base = attrTree('Base')        foo = attrTree('foo')    bar = attrTree('bar')    foo.childAdd(bar)    base.childAdd(foo)        omg = attrTree('omg')    wtf = attrTree('wtf')    hax = attrTree('hax')    hax.childAdd('someTestString')    wtf.childAdd(hax)    omg.childAdd(wtf)    base.childAdd(omg)        # testQueries()testTreeQueries()