Changeset 651
- Timestamp:
- 08/02/03 17:07:32 (5 years ago)
- Files:
-
- trunk/RBFoundation/RBFoundation/ChainedDict.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/RBFoundation/RBFoundation/ChainedDict.py
r645 r651 217 217 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 218 218 219 class Dict List(object):219 class DictCollection(object): 220 220 """Makes a list of dicts act like a single dict. 221 221 Any modifications only happen to the first entry.""" … … 226 226 227 227 __slots__ = ['collection'] 228 CollectionFactory = list 228 229 229 230 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 231 232 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 232 233 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) 236 236 237 237 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)) 239 239 240 240 def __eq__(self, other): … … 306 306 307 307 def clear(self): 308 self.collection = []308 self.collection = self.CollectionFactory() 309 309 310 310 def copy(self): 311 collection = self.collection[:]311 collection = list(self.collection)[:] 312 312 collection.reverse() 313 313 result = {} … … 343 343 344 344 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~362 345 #~ Protected Methods 363 346 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 364 347 365 348 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 358 ChainedDictCollection = DictCollection 373 359 374 360 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
