PageRenderTime 264ms CodeModel.GetById 141ms app.highlight 16ms RepoModel.GetById 105ms app.codeStats 0ms

/pod/set.py

http://pickled-object-database.googlecode.com/
Python | 156 lines | 95 code | 37 blank | 24 comment | 33 complexity | 6f5d44b3e3ce89b8493afa6806f6b9c3 MD5 | raw file
  1import exceptions
  2
  3import core
  4import typed
  5import query
  6import fn
  7
  8class PodSetError(exceptions.BaseException):
  9    pass
 10
 11class Set(core.Object):
 12    
 13    def __init__(self, set = None, **kwargs):
 14        core.Object.__init__(self, **kwargs)
 15        if set:
 16            self.update(set)
 17        
 18    def __len__(self):
 19        # Return the cardinality of set s.

 20        get_one = query.RawQuery(select = fn.count(SetItem.id) >> 'count', where = SetItem.parent == self).get_one()
 21        if get_one:
 22            return get_one.count
 23
 24    def __contains__(self, value):
 25        #Test x for membership in s. 

 26        return True if ((SetItem.parent == self) & (SetItem.value == value)).get_one() else False
 27
 28    def __iter__(self):
 29        return (item.value for item in core.Query(select = SetItem.value, where = SetItem.parent == self))
 30
 31    def copy(self):
 32        return set([item.value for item in core.Query(select = SetItem.value, where = SetItem.parent == self)])
 33
 34    def update(self, other):
 35        for elem in other:
 36            self.add(elem = elem)
 37    
 38    def add(self, elem):
 39        item = core.Query(where = (SetItem.parent == self) & (SetItem.value == elem)).get_one()
 40        if item:
 41            item.value = elem
 42        else:
 43            SetItem(parent = self, value = elem)     
 44
 45    def remove(self, elem):
 46        item = core.Query(where = (SetItem.parent == self) & (SetItem.value == elem)).get_one()
 47        if item:
 48            item.delete()
 49        else:
 50            raise KeyError
 51
 52    def discard(self,elem):
 53        item = core.Query(where = (SetItem.parent == self) & (SetItem.value == elem)).get_one()
 54        if item:
 55            item.delete()
 56
 57    def pop(self):
 58        item = core.Query(select = SetItem.value, where = SetItem.parent == self, limit = 1).get_one()
 59        if item:
 60            value = item.value
 61            item.delete()
 62            return value
 63        else:
 64            raise KeyError
 65
 66    def clear(self):        
 67        (SetItem.parent == self).delete()
 68
 69
 70    """ <= """
 71    def __le__(self, other):
 72        return self.issubset(other)
 73    
 74    def issubset(self, other):
 75        # set <= other.  Test whether every element in the set is in other.

 76        return self.copy().issubset(other.copy() if isinstance(other, Set) else other)
 77    
 78    """ < """   
 79    def __lt__(self, other):
 80        #set < other.  Test whether the set is a true subset of other, that is, set <= other and set != other.

 81        return self.copy().__lt__(other.copy() if isinstance(other, Set) else other)
 82    
 83        
 84    """ >= """   
 85    def __ge__(self, other):
 86        return self.issuperset(other)
 87
 88    def issuperset(self, other):
 89        # set >= other.  Test whether every element in other is in the set.

 90        super = True
 91        for elem in (other.copy() if isinstance(other, Set) else other):
 92            item = core.Query(where = (SetItem.parent == self) & (SetItem.value == elem)).get_one()
 93            if item is None:
 94                super = False
 95        return super
 96    
 97    """ > """   
 98    def __gt__(self, other):
 99        # set > other.  Test whether the set is a true superset of other, that is, set >= other and set != other.

100        return self.copy().__gt__(other.copy() if isinstance(other, Set) else other)
101        
102    """ | """  
103    def __or__(self, other):
104        return self.union(other)
105    
106    def union(self, other):
107        #union(other, ...) set | other | ... Return a new set with elements from the set and all others.

108        # Changed in version 2.6: Accepts multiple input iterables.

109        return self.copy().union(other.copy() if isinstance(other, Set) else other)
110
111    """ & """                
112    def __and__(self, other):
113        return self.intersection(other)
114    
115    def intersection(self, other):
116        #set & other & ... Return a new set with elements common to the set and all others.

117        # Changed in version 2.6: Accepts multiple input iterables.

118        return self.copy().intersection(other.copy() if isinstance(other, Set) else other)
119
120    """ - """                        
121    def __sub__(self, other):
122        return self.difference(other)
123    
124    def difference(self, other):
125        # difference(other, ...) set - other - ... Return a new set with elements in the set that are not in the others.

126        # Changed in version 2.6: Accepts multiple input iterables.

127        return self.copy().difference(other.copy() if isinstance(other, Set) else other)
128
129    """ ^ """                        
130    def __xor__(self, other):
131        return self.symmetric_difference(other)
132    
133    def symmetric_difference(self, other):
134        #set ^ other

135        #Return a new set with elements in either the set or other but not both.

136        return self.copy().symmetric_difference(other.copy() if isinstance(other, Set) else other)
137
138    """ SET OPERATIONS """
139    def isdisjoint(self, other):
140        #Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.

141        raise PodSetError, "method isdisjoint is not implemented at this time . . ."
142
143    def intersection_update(self, other):
144        raise PodSetError, "method intersection_update is not implemented at this time . . ."
145    
146    def difference_update(self, other):
147        raise PodSetError, "method difference_update is not implemented at this time . . ."
148    
149    def symmetric_difference_update(self, other):
150        raise PodSetError, "method symmetric_difference_update is not implemented at this time . . ."
151
152class SetItem(core.Object):
153    
154    parent = typed.PodObject(index = True)
155    value  = typed.Object(index = True)
156