2007-02-28 08:35:50 +08:00
|
|
|
"""
|
|
|
|
# Tests for stuff in django.utils.datastructures.
|
|
|
|
|
2008-08-25 12:00:15 +08:00
|
|
|
>>> import pickle
|
2007-02-28 08:35:50 +08:00
|
|
|
>>> 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'
|
|
|
|
|
2008-02-03 10:02:41 +08:00
|
|
|
MergeDict can merge MultiValueDicts
|
|
|
|
>>> multi1 = MultiValueDict({'key1': ['value1'], 'key2': ['value2', 'value3']})
|
|
|
|
>>> multi2 = MultiValueDict({'key2': ['value4'], 'key4': ['value5', 'value6']})
|
|
|
|
>>> mm = MergeDict(multi1, multi2)
|
|
|
|
|
|
|
|
# Although 'key2' appears in both dictionaries, only the first value is used.
|
|
|
|
>>> mm.getlist('key2')
|
|
|
|
['value2', 'value3']
|
|
|
|
>>> mm.getlist('key4')
|
|
|
|
['value5', 'value6']
|
|
|
|
>>> mm.getlist('undefined')
|
|
|
|
[]
|
|
|
|
|
2007-02-28 08:35:50 +08:00
|
|
|
### MultiValueDict ##########################################################
|
|
|
|
|
|
|
|
>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
|
|
|
|
>>> d['name']
|
|
|
|
'Simon'
|
2007-11-26 02:16:40 +08:00
|
|
|
>>> d.get('name')
|
|
|
|
'Simon'
|
2007-02-28 08:35:50 +08:00
|
|
|
>>> d.getlist('name')
|
|
|
|
['Adrian', 'Simon']
|
2008-08-16 10:17:55 +08:00
|
|
|
>>> list(d.iteritems())
|
|
|
|
[('position', 'Developer'), ('name', 'Simon')]
|
2009-03-31 14:30:07 +08:00
|
|
|
>>> list(d.iterlists())
|
|
|
|
[('position', ['Developer']), ('name', ['Adrian', 'Simon'])]
|
2007-11-26 02:16:40 +08:00
|
|
|
>>> d['lastname']
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
MultiValueDictKeyError: "Key 'lastname' not found in <MultiValueDict: {'position': ['Developer'], 'name': ['Adrian', 'Simon']}>"
|
|
|
|
>>> d.get('lastname')
|
|
|
|
|
2007-02-28 08:35:50 +08:00
|
|
|
>>> d.get('lastname', 'nonexistent')
|
|
|
|
'nonexistent'
|
2007-11-26 02:16:40 +08:00
|
|
|
>>> d.getlist('lastname')
|
|
|
|
[]
|
2007-02-28 08:35:50 +08:00
|
|
|
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
|
2007-11-26 02:16:40 +08:00
|
|
|
>>> d.getlist('lastname')
|
|
|
|
['Holovaty', 'Willison']
|
2010-01-03 14:54:56 +08:00
|
|
|
>>> d.values()
|
2009-03-31 14:30:07 +08:00
|
|
|
['Developer', 'Simon', 'Willison']
|
2010-01-03 14:54:56 +08:00
|
|
|
>>> list(d.itervalues())
|
2009-03-31 14:30:07 +08:00
|
|
|
['Developer', 'Simon', 'Willison']
|
2007-02-28 08:35:50 +08:00
|
|
|
|
2007-03-08 17:28:45 +08:00
|
|
|
### SortedDict #################################################################
|
|
|
|
|
|
|
|
>>> d = SortedDict()
|
|
|
|
>>> d['one'] = 'one'
|
|
|
|
>>> d['two'] = 'two'
|
|
|
|
>>> d['three'] = 'three'
|
|
|
|
>>> d['one']
|
|
|
|
'one'
|
|
|
|
>>> d['two']
|
|
|
|
'two'
|
|
|
|
>>> d['three']
|
|
|
|
'three'
|
|
|
|
>>> d.keys()
|
|
|
|
['one', 'two', 'three']
|
|
|
|
>>> d.values()
|
|
|
|
['one', 'two', 'three']
|
|
|
|
>>> d['one'] = 'not one'
|
|
|
|
>>> d['one']
|
|
|
|
'not one'
|
2007-03-09 13:34:42 +08:00
|
|
|
>>> d.keys() == d.copy().keys()
|
|
|
|
True
|
2008-02-19 07:08:51 +08:00
|
|
|
>>> d2 = d.copy()
|
|
|
|
>>> d2['four'] = 'four'
|
2007-04-25 15:30:54 +08:00
|
|
|
>>> print repr(d)
|
|
|
|
{'one': 'not one', 'two': 'two', 'three': 'three'}
|
2007-10-22 08:52:54 +08:00
|
|
|
>>> d.pop('one', 'missing')
|
|
|
|
'not one'
|
|
|
|
>>> d.pop('one', 'missing')
|
|
|
|
'missing'
|
|
|
|
|
2010-01-03 14:54:56 +08:00
|
|
|
>>> SortedDict((i, i) for i in xrange(3))
|
|
|
|
{0: 0, 1: 1, 2: 2}
|
|
|
|
|
2007-10-22 08:52:54 +08:00
|
|
|
We don't know which item will be popped in popitem(), so we'll just check that
|
|
|
|
the number of keys has decreased.
|
|
|
|
>>> l = len(d)
|
|
|
|
>>> _ = d.popitem()
|
|
|
|
>>> l - len(d)
|
|
|
|
1
|
2007-03-08 17:28:45 +08:00
|
|
|
|
2007-10-14 12:17:02 +08:00
|
|
|
Init from sequence of tuples
|
|
|
|
>>> d = SortedDict((
|
|
|
|
... (1, "one"),
|
|
|
|
... (0, "zero"),
|
|
|
|
... (2, "two")))
|
|
|
|
>>> print repr(d)
|
|
|
|
{1: 'one', 0: 'zero', 2: 'two'}
|
|
|
|
|
2008-08-25 12:00:15 +08:00
|
|
|
>>> pickle.loads(pickle.dumps(d, 2))
|
|
|
|
{1: 'one', 0: 'zero', 2: 'two'}
|
|
|
|
|
2008-02-22 05:27:44 +08:00
|
|
|
>>> d.clear()
|
|
|
|
>>> d
|
|
|
|
{}
|
|
|
|
>>> d.keyOrder
|
|
|
|
[]
|
|
|
|
|
2008-08-25 12:00:15 +08:00
|
|
|
### DotExpandedDict ##########################################################
|
2007-03-08 17:28:45 +08:00
|
|
|
|
|
|
|
>>> d = DotExpandedDict({'person.1.firstname': ['Simon'], 'person.1.lastname': ['Willison'], 'person.2.firstname': ['Adrian'], 'person.2.lastname': ['Holovaty']})
|
|
|
|
>>> d['person']['1']['lastname']
|
|
|
|
['Willison']
|
|
|
|
>>> d['person']['2']['lastname']
|
|
|
|
['Holovaty']
|
|
|
|
>>> d['person']['2']['firstname']
|
|
|
|
['Adrian']
|
2007-08-12 20:02:08 +08:00
|
|
|
|
2008-08-25 12:00:15 +08:00
|
|
|
### ImmutableList ############################################################
|
2008-07-01 23:10:51 +08:00
|
|
|
>>> d = ImmutableList(range(10))
|
|
|
|
>>> d.sort()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
File "/var/lib/python-support/python2.5/django/utils/datastructures.py", line 359, in complain
|
|
|
|
raise AttributeError, self.warning
|
|
|
|
AttributeError: ImmutableList object is immutable.
|
2007-08-12 20:02:08 +08:00
|
|
|
>>> repr(d)
|
2008-07-01 23:10:51 +08:00
|
|
|
'(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)'
|
|
|
|
>>> d = ImmutableList(range(10), warning="Object is immutable!")
|
|
|
|
>>> d[1]
|
|
|
|
1
|
|
|
|
>>> d[1] = 'test'
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
File "/var/lib/python-support/python2.5/django/utils/datastructures.py", line 359, in complain
|
|
|
|
raise AttributeError, self.warning
|
|
|
|
AttributeError: Object is immutable!
|
2008-06-26 11:16:55 +08:00
|
|
|
|
|
|
|
### DictWrapper #############################################################
|
|
|
|
|
|
|
|
>>> f = lambda x: "*%s" % x
|
|
|
|
>>> d = DictWrapper({'a': 'a'}, f, 'xx_')
|
|
|
|
>>> "Normal: %(a)s. Modified: %(xx_a)s" % d
|
|
|
|
'Normal: a. Modified: *a'
|
|
|
|
|
2007-03-08 17:28:45 +08:00
|
|
|
"""
|