From aca569804e661725ed38d2bc0a2ccfd165e4bad0 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 25 Apr 2007 07:30:54 +0000 Subject: [PATCH] Fixed #3964 -- Added a custom SortedDict.__repr__ so that the keys are printed in sorted order. Based on a patch from Forest Bond. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5069 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/datastructures.py | 7 +++++++ tests/regressiontests/datastructures/tests.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 2b19c06dfb..cca30b462d 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -99,6 +99,13 @@ class SortedDict(dict): obj.keyOrder = self.keyOrder return obj + def __repr__(self): + """ + Replaces the normal dict.__repr__ with a version that returns the keys + in their sorted order. + """ + return '{%s}' % ', '.join(['%r: %r' % (k, v) for k, v in self.items()]) + class MultiValueDictKeyError(KeyError): pass diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py index e008c255f1..18eb4fcccd 100644 --- a/tests/regressiontests/datastructures/tests.py +++ b/tests/regressiontests/datastructures/tests.py @@ -52,6 +52,8 @@ 'not one' >>> d.keys() == d.copy().keys() True +>>> print repr(d) +{'one': 'not one', 'two': 'two', 'three': 'three'} ### DotExpandedDict ############################################################