Fixed #6050 -- Handled edge-case of duplicate keys being passed when
initialising SortedDict. Patch from Collin Grady and SmileyChris. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6751 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
8f97d96238
commit
caa0523cb8
|
@ -60,7 +60,10 @@ class SortedDict(dict):
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
self.keyOrder = data.keys()
|
self.keyOrder = data.keys()
|
||||||
else:
|
else:
|
||||||
self.keyOrder = [key for key, value in data]
|
self.keyOrder = []
|
||||||
|
for key, value in data:
|
||||||
|
if key not in self.keyOrder:
|
||||||
|
self.keyOrder.append(key)
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
"""
|
||||||
|
>>> from django.utils.datastructures import SortedDict
|
||||||
|
|
||||||
|
>>> d = SortedDict()
|
||||||
|
>>> d[7] = 'seven'
|
||||||
|
>>> d[1] = 'one'
|
||||||
|
>>> d[9] = 'nine'
|
||||||
|
>>> d.keys()
|
||||||
|
[7, 1, 9]
|
||||||
|
>>> d.values()
|
||||||
|
['seven', 'one', 'nine']
|
||||||
|
>>> d.items()
|
||||||
|
[(7, 'seven'), (1, 'one'), (9, 'nine')]
|
||||||
|
|
||||||
|
# Overwriting an item keeps it's place.
|
||||||
|
>>> d[1] = 'ONE'
|
||||||
|
>>> d.values()
|
||||||
|
['seven', 'ONE', 'nine']
|
||||||
|
|
||||||
|
# New items go to the end.
|
||||||
|
>>> d[0] = 'nil'
|
||||||
|
>>> d.keys()
|
||||||
|
[7, 1, 9, 0]
|
||||||
|
|
||||||
|
# Deleting an item, then inserting the same key again will place it at the end.
|
||||||
|
>>> del d[7]
|
||||||
|
>>> d.keys()
|
||||||
|
[1, 9, 0]
|
||||||
|
>>> d[7] = 'lucky number 7'
|
||||||
|
>>> d.keys()
|
||||||
|
[1, 9, 0, 7]
|
||||||
|
|
||||||
|
# Changing the keys won't do anything, it's only a copy of the keys dict.
|
||||||
|
>>> k = d.keys()
|
||||||
|
>>> k.remove(9)
|
||||||
|
>>> d.keys()
|
||||||
|
[1, 9, 0, 7]
|
||||||
|
|
||||||
|
# Initialising a SortedDict with two keys will just take the first one. A real
|
||||||
|
# dict will actually take the second value so we will too, but we'll keep the
|
||||||
|
# ordering from the first key found.
|
||||||
|
>>> tuples = ((2, 'two'), (1, 'one'), (2, 'second-two'))
|
||||||
|
>>> d = SortedDict(tuples)
|
||||||
|
>>> d.keys()
|
||||||
|
[2, 1]
|
||||||
|
>>> real_dict = dict(tuples)
|
||||||
|
>>> real_dict.values()
|
||||||
|
['one', 'second-two']
|
||||||
|
>>> d.values()
|
||||||
|
['second-two', 'one']
|
||||||
|
"""
|
|
@ -6,7 +6,14 @@ from unittest import TestCase
|
||||||
|
|
||||||
from django.utils import html, checksums
|
from django.utils import html, checksums
|
||||||
|
|
||||||
from timesince import timesince_tests
|
import timesince
|
||||||
|
import datastructures
|
||||||
|
|
||||||
|
# Extra tests
|
||||||
|
__test__ = {
|
||||||
|
'timesince': timesince,
|
||||||
|
'datastructures': datastructures,
|
||||||
|
}
|
||||||
|
|
||||||
class TestUtilsHtml(TestCase):
|
class TestUtilsHtml(TestCase):
|
||||||
|
|
||||||
|
@ -142,10 +149,6 @@ class TestUtilsChecksums(TestCase):
|
||||||
for value, output in items:
|
for value, output in items:
|
||||||
self.check_output(f, value, output)
|
self.check_output(f, value, output)
|
||||||
|
|
||||||
__test__ = {
|
|
||||||
'timesince_tests': timesince_tests,
|
|
||||||
}
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
timesince_tests = """
|
"""
|
||||||
>>> from datetime import datetime, timedelta
|
>>> from datetime import datetime, timedelta
|
||||||
>>> from django.utils.timesince import timesince
|
>>> from django.utils.timesince import timesince
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue