Loading django/utils/datastructures.py +0 −32 Original line number Diff line number Diff line Loading @@ -412,38 +412,6 @@ class MultiValueDict(dict): """ return dict((key, self[key]) for key in self) class DotExpandedDict(dict): """ A special dictionary constructor that takes a dictionary in which the keys may contain dots to specify inner dictionaries. It's confusing, but this example should make sense. >>> d = DotExpandedDict({'person.1.firstname': ['Simon'], \ 'person.1.lastname': ['Willison'], \ 'person.2.firstname': ['Adrian'], \ 'person.2.lastname': ['Holovaty']}) >>> d {'person': {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}} >>> d['person'] {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}} >>> d['person']['1'] {'lastname': ['Willison'], 'firstname': ['Simon']} # Gotcha: Results are unpredictable if the dots are "uneven": >>> DotExpandedDict({'c.1': 2, 'c.2': 3, 'c': 1}) {'c': 1} """ def __init__(self, key_to_list_mapping): for k, v in key_to_list_mapping.items(): current = self bits = k.split('.') for bit in bits[:-1]: current = current.setdefault(bit, {}) # Now assign value to current position try: current[bits[-1]] = v except TypeError: # Special-case if current isn't a dict. current = {bits[-1]: v} class ImmutableList(tuple): """ Loading tests/regressiontests/utils/datastructures.py +2 −16 Original line number Diff line number Diff line Loading @@ -6,8 +6,8 @@ import copy import pickle from django.test import SimpleTestCase from django.utils.datastructures import (DictWrapper, DotExpandedDict, ImmutableList, MultiValueDict, MultiValueDictKeyError, MergeDict, SortedDict) from django.utils.datastructures import (DictWrapper, ImmutableList, MultiValueDict, MultiValueDictKeyError, MergeDict, SortedDict) class SortedDictTests(SimpleTestCase): Loading Loading @@ -251,20 +251,6 @@ class MultiValueDictTests(SimpleTestCase): self.assertEqual({}, MultiValueDict().dict()) class DotExpandedDictTests(SimpleTestCase): def test_dotexpandeddict(self): d = DotExpandedDict({'person.1.firstname': ['Simon'], 'person.1.lastname': ['Willison'], 'person.2.firstname': ['Adrian'], 'person.2.lastname': ['Holovaty']}) self.assertEqual(d['person']['1']['lastname'], ['Willison']) self.assertEqual(d['person']['2']['lastname'], ['Holovaty']) self.assertEqual(d['person']['2']['firstname'], ['Adrian']) class ImmutableListTests(SimpleTestCase): def test_sort(self): Loading tests/regressiontests/utils/tests.py +1 −1 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ from .decorators import DecoratorFromMiddlewareTests from .functional import FunctionalTestCase from .timesince import TimesinceTests from .datastructures import (MultiValueDictTests, SortedDictTests, DictWrapperTests, ImmutableListTests, DotExpandedDictTests, MergeDictTests) DictWrapperTests, ImmutableListTests, MergeDictTests) from .tzinfo import TzinfoTests from .datetime_safe import DatetimeTests from .baseconv import TestBaseConv Loading Loading
django/utils/datastructures.py +0 −32 Original line number Diff line number Diff line Loading @@ -412,38 +412,6 @@ class MultiValueDict(dict): """ return dict((key, self[key]) for key in self) class DotExpandedDict(dict): """ A special dictionary constructor that takes a dictionary in which the keys may contain dots to specify inner dictionaries. It's confusing, but this example should make sense. >>> d = DotExpandedDict({'person.1.firstname': ['Simon'], \ 'person.1.lastname': ['Willison'], \ 'person.2.firstname': ['Adrian'], \ 'person.2.lastname': ['Holovaty']}) >>> d {'person': {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}} >>> d['person'] {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}} >>> d['person']['1'] {'lastname': ['Willison'], 'firstname': ['Simon']} # Gotcha: Results are unpredictable if the dots are "uneven": >>> DotExpandedDict({'c.1': 2, 'c.2': 3, 'c': 1}) {'c': 1} """ def __init__(self, key_to_list_mapping): for k, v in key_to_list_mapping.items(): current = self bits = k.split('.') for bit in bits[:-1]: current = current.setdefault(bit, {}) # Now assign value to current position try: current[bits[-1]] = v except TypeError: # Special-case if current isn't a dict. current = {bits[-1]: v} class ImmutableList(tuple): """ Loading
tests/regressiontests/utils/datastructures.py +2 −16 Original line number Diff line number Diff line Loading @@ -6,8 +6,8 @@ import copy import pickle from django.test import SimpleTestCase from django.utils.datastructures import (DictWrapper, DotExpandedDict, ImmutableList, MultiValueDict, MultiValueDictKeyError, MergeDict, SortedDict) from django.utils.datastructures import (DictWrapper, ImmutableList, MultiValueDict, MultiValueDictKeyError, MergeDict, SortedDict) class SortedDictTests(SimpleTestCase): Loading Loading @@ -251,20 +251,6 @@ class MultiValueDictTests(SimpleTestCase): self.assertEqual({}, MultiValueDict().dict()) class DotExpandedDictTests(SimpleTestCase): def test_dotexpandeddict(self): d = DotExpandedDict({'person.1.firstname': ['Simon'], 'person.1.lastname': ['Willison'], 'person.2.firstname': ['Adrian'], 'person.2.lastname': ['Holovaty']}) self.assertEqual(d['person']['1']['lastname'], ['Willison']) self.assertEqual(d['person']['2']['lastname'], ['Holovaty']) self.assertEqual(d['person']['2']['firstname'], ['Adrian']) class ImmutableListTests(SimpleTestCase): def test_sort(self): Loading
tests/regressiontests/utils/tests.py +1 −1 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ from .decorators import DecoratorFromMiddlewareTests from .functional import FunctionalTestCase from .timesince import TimesinceTests from .datastructures import (MultiValueDictTests, SortedDictTests, DictWrapperTests, ImmutableListTests, DotExpandedDictTests, MergeDictTests) DictWrapperTests, ImmutableListTests, MergeDictTests) from .tzinfo import TzinfoTests from .datetime_safe import DatetimeTests from .baseconv import TestBaseConv Loading