Changeset 651

Show
Ignore:
Timestamp:
08/02/03 17:07:32 (5 years ago)
Author:
sholloway
Message:

*** empty log message ***

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBFoundation/RBFoundation/ChainedDict.py

    r645 r651  
    217217#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    218218 
    219 class DictList(object): 
     219class DictCollection(object): 
    220220    """Makes a list of dicts act like a single dict.   
    221221    Any modifications only happen to the first entry.""" 
     
    226226 
    227227    __slots__ = ['collection'] 
     228    CollectionFactory = list 
    228229 
    229230    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    231232    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    232233 
    233     def __init__(self, *args, **kw): 
    234         self.collection = list(args) 
    235         self.collection.extend(list(kw.get('collection', []))) 
     234    def __init__(self, *args): 
     235        self.collection = self.CollectionFactory(args) 
    236236 
    237237    def __repr__(self):  
    238         return "%s(%s)" % (self.__class__.__name__, repr(self.collection)[1:-1]
     238        return "%s(*%r)" % (self.__class__.__name__, repr(self.collection)
    239239 
    240240    def __eq__(self, other): 
     
    306306 
    307307    def clear(self): 
    308         self.collection = [] 
     308        self.collection = self.CollectionFactory() 
    309309     
    310310    def copy(self):  
    311         collection = self.collection[:] 
     311        collection = list(self.collection)[:] 
    312312        collection.reverse() 
    313313        result = {} 
     
    343343 
    344344    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    345  
    346     def pushfront(self, newdict): 
    347         self.collection.insert(0, newdict) 
    348  
    349     def pushback(self, newdict): 
    350         self.collection.append(newdict) 
    351  
    352     def pop(self, idx=-1): 
    353         return self.collection.pop(idx) 
    354  
    355     def popback(self): 
    356         return self.pop(-1) 
    357  
    358     def popfront(self): 
    359         return self.pop(0) 
    360  
    361     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    362345    #~ Protected Methods  
    363346    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    364347 
    365348    def _defaultdict(self): 
    366         try: 
    367             return self.collection[0] 
    368         except IndexError: 
    369             self.collection = [{}] 
    370             return self.collection[0] 
    371  
    372 ChainedDictList = DictList 
     349        # this should just return the equivalent of collection[0], without the getitem 
     350        for each in self.collection: 
     351            return each  
     352        else: 
     353            # or create the default on the fly 
     354            result = {} 
     355            self.collection = self.CollectionFactory([result]) 
     356            return result 
     357 
     358ChainedDictCollection = DictCollection 
    373359 
    374360#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~