From 2d06c971fa121ee1bcf433e82eefae92084d46c1 Mon Sep 17 00:00:00 2001
From: Gisle Aas <gisle@aas.no>
Date: Mon, 22 Feb 2010 11:25:57 -0500
Subject: [PATCH] Ticket #12820: fix iteration over MergeDict
---
django/utils/datastructures.py | 16 +++++++++++++++-
tests/regressiontests/datastructures/tests.py | 11 +++++++++++
2 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
index d6fc9dc..c5a9fdc 100644
a
|
b
|
class MergeDict(object):
|
39 | 39 | |
40 | 40 | def items(self): |
41 | 41 | item_list = [] |
| 42 | seen = set() |
42 | 43 | for dict_ in self.dicts: |
43 | | item_list.extend(dict_.items()) |
| 44 | for item in dict_.items(): |
| 45 | if item[0] in seen: |
| 46 | continue |
| 47 | item_list.append(item) |
| 48 | seen.add(item[0]) |
44 | 49 | return item_list |
45 | 50 | |
| 51 | def keys(self): |
| 52 | return [item[0] for item in self.items()] |
| 53 | |
| 54 | def values(self): |
| 55 | return [item[1] for item in self.items()] |
| 56 | |
| 57 | def __iter__(self): |
| 58 | return iter(self.keys()) |
| 59 | |
46 | 60 | def has_key(self, key): |
47 | 61 | for dict_ in self.dicts: |
48 | 62 | if key in dict_: |
diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py
index b7885e6..a858e24 100644
a
|
b
|
MergeDict can merge MultiValueDicts
|
34 | 34 | >>> mm.getlist('undefined') |
35 | 35 | [] |
36 | 36 | |
| 37 | >>> sorted(mm.keys()) |
| 38 | ['key1', 'key2', 'key4'] |
| 39 | >>> len(mm.values()) |
| 40 | 3 |
| 41 | >>> "value1" in mm.values() |
| 42 | True |
| 43 | >>> sorted(mm.items(), key=lambda k: k[0]) |
| 44 | [('key1', 'value1'), ('key2', 'value3'), ('key4', 'value6')] |
| 45 | >>> [(k,mm.getlist(k)) for k in sorted(mm)] |
| 46 | [('key1', ['value1']), ('key2', ['value2', 'value3']), ('key4', ['value5', 'value6'])] |
| 47 | |
37 | 48 | ### MultiValueDict ########################################################## |
38 | 49 | |
39 | 50 | >>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']}) |