Code

Ticket #3508: doc_MergeDict.diff

File doc_MergeDict.diff, 1.0 KB (added by thebanana, 7 years ago)

patch to add str and repr to MergeDict

Line 
1=== django/utils/datastructures.py
2==================================================================
3--- django/utils/datastructures.py      (revision 15931)
4+++ django/utils/datastructures.py      (local)
5@@ -43,6 +43,27 @@
6                 return True
7         return False
8 
9+    def __str__(self):
10+        '''
11+        Returns something like
12+
13+            "{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}"
14+
15+        instead of the generic "<object meta-data>" inherited from object.
16+        '''
17+        return str(dict(self.items()))
18+
19+    def __repr__(self):
20+        '''
21+        Returns something like
22+
23+            MergeDict({'key1': 'val1', 'key2': 'val2'}, {'key3': 'val3'})
24+
25+        instead of generic "<object meta-data>" inherited from object.
26+        '''
27+        dictreprs = ', '.join(repr(d) for d in self.dicts)
28+        return '%s(%s)' % (self.__class__.__name__, dictreprs)
29+
30 class SortedDict(dict):
31     "A dictionary that keeps its keys in the order in which they're inserted."
32     def __init__(self, data=None):