PageRenderTime 33ms CodeModel.GetById 2ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

/pod/dict.py

http://pickled-object-database.googlecode.com/
Python | 108 lines | 80 code | 27 blank | 1 comment | 23 complexity | 16cff4f9583846e456fa1a003108f434 MD5 | raw file
  1import exceptions
  2
  3import core
  4import typed
  5import query
  6import fn
  7
  8class PodDictError(exceptions.BaseException):
  9    pass
 10                                          
 11class Dict(core.Object):
 12    
 13    def __init__(self, dict = None, **kwargs):
 14        core.Object.__init__(self, **kwargs)
 15        if dict:
 16            self.update(other = dict)
 17
 18    def __len__(self):
 19        get_one = query.RawQuery(select = fn.count(DictItem.id) >> 'count', where = DictItem.parent == self).get_one()
 20        if get_one:
 21            return get_one.count
 22
 23    def __getitem__(self, key, return_node = False):
 24        item = core.Query(select = DictItem.value, where = (DictItem.parent == self) & (DictItem.key == key)).get_one()
 25        if item:
 26            return item.value
 27        else:
 28            raise KeyError                
 29    
 30    def __setitem__(self, key, value):
 31        item = ((DictItem.parent == self) & (DictItem.key == key)).get_one()
 32        if item:
 33            item.value = value
 34        else:
 35            DictItem(parent = self, key = key, value = value)
 36
 37    def __delitem__(self, key):
 38        item = core.Query(select = DictItem.value, where = (DictItem.parent == self) & (DictItem.key == key)).get_one()
 39        if item:
 40            return item.delete()
 41        else:
 42            raise KeyError 
 43
 44    def __contains__(self, key):
 45        return True if ((DictItem.parent == self) & (DictItem.key == key)).get_one() else False
 46
 47    def __iter__(self):
 48        return self.iterkeys()
 49
 50    def update(self, other):        
 51        for key,value in other.iteritems():
 52            self[key] = value
 53    
 54    def clear(self):
 55        (DictItem.parent == self).delete()
 56
 57    def copy(self):
 58        return dict([(key,value) for key,value in self.iteritems()])
 59
 60    def get(self, key, default = None):
 61        try:
 62            return query.Query(select = DictItem.value, where = (DictItem.parent == self) & (DictItem.key == key)).get_one().value
 63        except AttributeError:
 64            return default
 65    
 66    def setdefault(self, key, default):
 67        try:
 68            return query.Query(select = DictItem.value, where = (DictItem.parent == self) & (DictItem.key == key)).get_one().value
 69        except AttributeError:
 70            self.__setitem__(key, default)
 71            return default
 72        
 73    def keys(self):
 74        return [item.key for item in core.Query(select = DictItem.key, where = DictItem.parent == self)]
 75    
 76    def iterkeys(self):
 77        return (item.key for item in core.Query(select = DictItem.key, where = DictItem.parent == self))
 78
 79    def values(self):
 80        return [item.value for item in core.Query(select = DictItem.value, where = DictItem.parent == self)]
 81    
 82    def itervalues(self):
 83        return (item.value for item in core.Query(select = DictItem.value, where = DictItem.parent == self))
 84    
 85    def items(self):
 86        return [(item.key,item.value) for item in core.Query(select = DictItem.key | DictItem.value, where = DictItem.parent == self)]
 87    
 88    def iteritems(self):
 89        return ((item.key,item.value) for item in core.Query(select = DictItem.key | DictItem.value, where = DictItem.parent == self))
 90
 91    """ Not Supported """
 92    def fromkeys(self):
 93        raise PodDictError, "method fromkeys not implemented . . ."
 94
 95    def has_key(self):
 96        raise PodDictError, "method has_key not implemented, use 'in' or __contains__ instead . . ."
 97    
 98    def pop(self, key, *args, **kwargs):
 99        raise PodDictError, "method pop is not implemented . . ."
100
101    def popitem(self):
102        raise PodDictError, "method popitem is not implemented . . ."
103        
104class DictItem(core.Object):
105    
106    parent = typed.PodObject(index = True)
107    key    = typed.Object(index = True)
108    value  = typed.Object(index = False)