Ticket #2779: 2779.patch
File 2779.patch, 1.6 KB (added by , 18 years ago) |
---|
-
django/utils/datastructures.py
1 1 class MergeDict(object): 2 2 """ 3 A simple class for creating new "virtual" dictionaries that actual y look3 A simple class for creating new "virtual" dictionaries that actually look 4 4 up values in more than one dictionary, passed in the constructor. 5 6 >>> d1 = {'chris':'cool','camri':'cute','cotton':'adorable','tulip':'snuggable', 'twoofme':'firstone'} 7 >>> d2 = {'chris2':'cool2','camri2':'cute2','cotton2':'adorable2','tulip2':'snuggable2'} 8 >>> d3 = {'chris3':'cool3','camri3':'cute3','cotton3':'adorable3','tulip3':'snuggable3'} 9 >>> d4 = {'twoofme':'secondone'} 10 >>> md = MergeDict( d1,d2,d3 ) 11 >>> md['chris'] 12 'cool' 13 >>> md['camri'] 14 'cute' 15 >>> md['twoofme'] 16 'firstone' 17 >>> md2 = md.copy() 18 >>> md2['chris'] 19 'cool' 5 20 """ 6 21 def __init__(self, *dicts): 7 22 self.dicts = dicts … … 17 32 def __contains__(self, key): 18 33 return self.has_key(key) 19 34 35 def __copy__(self): 36 return self.__class__(*self.dicts) 37 20 38 def get(self, key, default=None): 21 39 try: 22 40 return self[key] … … 43 61 return True 44 62 return False 45 63 64 def copy(self): 65 """ returns a copy of this object""" 66 return self.__copy__() 67 46 68 class SortedDict(dict): 47 69 "A dictionary that keeps its keys in the order in which they're inserted." 48 70 def __init__(self, data=None):