Code

Ticket #2779: 2779.patch

File 2779.patch, 1.6 KB (added by chris.mcavoy@…, 7 years ago)

Patch that adds shallow copy method to MergeDict

  • django/utils/datastructures.py

     
    11class MergeDict(object): 
    22    """ 
    3     A simple class for creating new "virtual" dictionaries that actualy look 
     3    A simple class for creating new "virtual" dictionaries that actually look 
    44    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' 
    520    """ 
    621    def __init__(self, *dicts): 
    722        self.dicts = dicts 
     
    1732    def __contains__(self, key): 
    1833        return self.has_key(key) 
    1934 
     35    def __copy__(self): 
     36        return self.__class__(*self.dicts) 
     37 
    2038    def get(self, key, default=None): 
    2139        try: 
    2240            return self[key] 
     
    4361                return True 
    4462        return False 
    4563 
     64    def copy(self): 
     65        """ returns a copy of this object""" 
     66        return self.__copy__() 
     67 
    4668class SortedDict(dict): 
    4769    "A dictionary that keeps its keys in the order in which they're inserted." 
    4870    def __init__(self, data=None):