All pastes #250755 Raw Edit

Cody Brocious

public python v1 · immutable
#250755 ·published 2006-11-17 09:25 UTC
rendered paste body
#!/usr/bin/env python# encoding: utf-8"""queryableModel.pyCreated by  on 2006-11-14.Copyleft (c) 2006 Cody Brocious All rights reversed."""from interface import interfacefrom model import modelclass queryableModel(model):    @interface.public    def query(self, rule):        # Create object of the same type as self for result        result = self.__clone__()                for child in self.children:            if rule(child):                result.childAdd(child)                return result        @interface.public    def queryValue(self, rule):        # Create object of the same type as self for result        result = self.__clone__()                for child in self.children:            value = rule(child)                        if value != None and value != False:                result.childAdd(value)                return result        @interface.public    def queryPath(self, *path):        if len(path) == 1:            path = path[0]                if self.__class__ != path[0]:            return None                path = path[1:]                return self.queryPathRecursive(self, path)        def queryPathRecursive(self, object, path):        if len(path) == 1:            result = object.query(lambda child: child.__class__ == path[0])                        if len(result.children) == 0:                return None                        return result                result = None                for child in object.children:            if child.__class__ == path[0]:                value = self.queryPathRecursive(child, path[1:])                                if value == None or value == False:                    continue                                if result == None:                    result = value                else:                    result += value                if not result or len(result.children) == 0:            return None        else:            return result        @interface.public    def __clone__(self):        return self.__class__()        @interface.public    def __repr__(self):        return `self.children`