From 6ca7930dd4c874ba304f4ce6c934aca8c9777edd Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 28 Feb 2007 00:35:50 +0000 Subject: [PATCH] Fixed #2779: added a copy() method to MergeDict, along with some new tests for django.utils.datastructures. Thanks, Chris McAvoy. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4640 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/datastructures.py | 7 ++++ .../datastructures/__init__.py | 0 .../regressiontests/datastructures/models.py | 0 tests/regressiontests/datastructures/tests.py | 34 +++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 tests/regressiontests/datastructures/__init__.py create mode 100644 tests/regressiontests/datastructures/models.py create mode 100644 tests/regressiontests/datastructures/tests.py diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index c20da05144..94ab76c483 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -16,6 +16,9 @@ class MergeDict(object): def __contains__(self, key): return self.has_key(key) + + def __copy__(self): + return self.__class__(*self.dicts) def get(self, key, default=None): try: @@ -42,6 +45,10 @@ class MergeDict(object): if dict.has_key(key): return True return False + + def copy(self): + """ returns a copy of this object""" + return self.__copy__() class SortedDict(dict): "A dictionary that keeps its keys in the order in which they're inserted." diff --git a/tests/regressiontests/datastructures/__init__.py b/tests/regressiontests/datastructures/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/datastructures/models.py b/tests/regressiontests/datastructures/models.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py new file mode 100644 index 0000000000..624e7a50bf --- /dev/null +++ b/tests/regressiontests/datastructures/tests.py @@ -0,0 +1,34 @@ +""" +# Tests for stuff in django.utils.datastructures. + +>>> from django.utils.datastructures import * + +### MergeDict ################################################################# + +>>> d1 = {'chris':'cool','camri':'cute','cotton':'adorable','tulip':'snuggable', 'twoofme':'firstone'} +>>> d2 = {'chris2':'cool2','camri2':'cute2','cotton2':'adorable2','tulip2':'snuggable2'} +>>> d3 = {'chris3':'cool3','camri3':'cute3','cotton3':'adorable3','tulip3':'snuggable3'} +>>> d4 = {'twoofme':'secondone'} +>>> md = MergeDict( d1,d2,d3 ) +>>> md['chris'] +'cool' +>>> md['camri'] +'cute' +>>> md['twoofme'] +'firstone' +>>> md2 = md.copy() +>>> md2['chris'] +'cool' + +### MultiValueDict ########################################################## + +>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']}) +>>> d['name'] +'Simon' +>>> d.getlist('name') +['Adrian', 'Simon'] +>>> d.get('lastname', 'nonexistent') +'nonexistent' +>>> d.setlist('lastname', ['Holovaty', 'Willison']) + +""" \ No newline at end of file