#!/usr/bin/env python # encoding: utf-8 """ testBattery.py Created by on 2006-11-14. Copyleft (c) 2006 Cody Brocious All rights reversed. """ from interface import interface from model import model from queryableModel import queryableModel class 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')) testObj.childAdd(pet('Sincere' , 25, 'Human')) 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 = None): model.__init__(self) self.name = name @interface.public def __clone__(self): return self.__class__(self.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): if child.__class__ == attrTree: child = child.queryValue(findStrings) if child.childCount() == 0: return None return child else: return child def 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) print 'Object tree:', base print 'Query result:', base.queryValue(findStrings) class human(pet): pass class neko(pet): pass def testPathQueries(): base = attrTree('base') humans = attrTree('humans') nekos = attrTree('neko') daeken = human('Daeken', 18, 'Human') nika = neko('Nikashera', 18, 'Neko') humans.childAdd(daeken) nekos.childAdd(nika) base.childAdd(humans) base.childAdd(nekos) print 'Humans:', base.queryPath(attrTree, attrTree, human) print 'Nekos:', base.queryPath(attrTree, attrTree, neko) # testQueries() # testTreeQueries() testPathQueries()