-
Notifications
You must be signed in to change notification settings - Fork 11
/
MultiDict.py
497 lines (423 loc) · 14.2 KB
/
MultiDict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""Dictionary-like objects which allow multiple keys
Python dictionaries map a key to a value. Duplicate keys are not
allowed, and new entries replace old ones with the same key. Order is
not otherwise preserved, so there's no way to get the items in the
order they were added to a dictionary.
Some types of data is best stored in dictionary-like object which
allow multiple values per key. Some of these need the input order
strongly preserved, so the items can be retrieved in the same order as
they were added to the dictionary. That is the OrderedMultiDict.
Others need a weaker ordering guarantee where the order of values for
a given key is preserved but the order between the keys is not. That
is UnorderedMultiDict. (Because strong ordering isn't needed, it's
faster delete from an UnorderedMultiDict.)
To create a MultiDict, pass in an object which implements the
'allitems' method and returns a list of (key, value) pairs, or
pass in the list of (key, value) pairs directly.
The two MultiDict classes implement the following dictionary methods
d["lookup"],
d["key"] = value
del d[key]
d.get("key", default = None)
d1 == d2, d1 != d2, len(d), iter(d), str(d)
d.keys(), d.values(), d.items()
The new methods are:
d.getall(key)
d.allkeys()
d.allvalues()
d.allitems()
>>> import MultiDict
>>> od = MultiDict.OrderedMultiDict()
>>> od["Name"] = "Andrew"
>>> od["Color"] = "BLUE"
>>> od["Name"] = "Dalke"
>>> od["Color"] = "Green"
>>> od[3] = 9
>>> len(od)
3
>>> od["Name"]
'Dalke'
>>> od.getall("Name")
['Andrew', 'Dalke']
>>> for k, v in od.allitems():
... print "%r == %r" % (k, v)
...
'Name' == 'Andrew'
'Color' == 'BLUE'
'Name' == 'Dalke'
'Color' == 'Green'
3 == 9
>>> del od["Name"]
>>> len(od)
2
>>> for k, v in od.allitems():
... print "%r == %r" % (k, v)
...
'Color' == 'BLUE'
'Color' == 'Green'
3 == 9
>>>
The latest version of this code can be found at
http://www.dalkescientific.com/Python/
"""
# Written in 2003 by Andrew Dalke, Dalke Scientific Software, LLC.
# This software has been released to the public domain. No
# copyright is asserted.
from __future__ import generators
# Implementation inheritence -- not asserting a class hierarchy here
#
# If there is a class hierarchy, OrderedMultiDict is a child of
# UnorderedMultiDict because it makes stronger but not different
# guarantees on how the data works, at least data-wise.
# Performance-wise, Ordered has a slower (O(n)) than Unordered (O(1)).
# Convince me otherwise and I'll change. Besides, hierarchies are
# overrated.
class _BaseMultiDict:
def __str__(self):
"""shows contents as if this is a dictionary
If multiple values exist for a given key, use the last
one added.
"""
d = {}
for k in self.data:
d[k] = self.data[k][-1]
return str(d)
def __len__(self):
"""the number of unique keys"""
return len(self.data)
def __getitem__(self, key):
"""value for a given key
If more than one value exists for the key, use one added most recently
"""
return self.data[key][-1]
def get(self, key, default=None):
"""value for the given key; default = None if not present
If more than one value exists for the key, use the one added
most recently.
"""
return self.data.get(key, [default])[-1]
def __contains__(self, key):
"""check if the key exists"""
return key in self.data
def keys(self):
"""unordered list of unique keys"""
return self.data.keys()
def values(self):
"""unordered list of values
If more than one value exists for a given key, use the value
added most recently.
"""
return [x[-1] for x in self.data.values()]
def items(self):
"""unordered list of key/value pairs
If more than one value exists for a given key, use the value
added most recently.
"""
return [(k, v[-1]) for k, v in self.data.items()]
def getall(self, key):
"""Get all values for a given key
Multiple values are returned in input order.
If the key does not exists, returns an empty list.
"""
return self.data[key]
def __iter__(self):
"""iterate through the list of unique keys"""
return iter(self.data)
class OrderedMultiDict(_BaseMultiDict):
"""Store key/value mappings.
Acts like a standard dictionary with the following features:
- duplicate keys are allowed;
- input order is preserved for all key/value pairs.
>>> od = OrderedMultiDict([("Food", "Spam"), ("Color", "Blue"),
... ("Food", "Eggs"), ("Color", "Green")])
>>> od["Food"]
'Eggs'
>>> od.getall("Food")
['Spam', 'Eggs']
>>> list(od.allkeys())
['Food', 'Color', 'Food', 'Color']
>>>
The order of keys and values(eg, od.allkeys() and od.allitems())
preserves input order.
Can also pass in an object to the constructor which has an
allitems() method that returns a list of key/value pairs.
"""
def __init__(self, multidict=None):
self.data = {}
self.order_data = []
if multidict is not None:
if hasattr(multidict, "allitems"):
multidict = multidict.allitems()
for k, v in multidict:
self[k] = v
def __eq__(self, other):
"""Does this OrderedMultiDict have the same contents and order as another?"""
return self.order_data == other.order_data
def __ne__(self, other):
"""Does this OrderedMultiDict have different contents or order as another?"""
return self.order_data != other.order_data
def __repr__(self):
return "<OrderedMultiDict %s>" % (self.order_data,)
def __setitem__(self, key, value):
"""Add a new key/value pair
If the key already exists, replaces the existing value
so that d[key] is the new value and not the old one.
To get all values for a given key, use d.getall(key).
"""
self.order_data.append((key, value))
self.data.setdefault(key, []).append(value)
def __delitem__(self, key):
"""Remove all values for the given key"""
del self.data[key]
self.order_data[:] = [x for x in self.order_data if x[0] != key]
def allkeys(self):
"""iterate over all keys in input order"""
for x in self.order_data:
yield x[0]
def allvalues(self):
"""iterate over all values in input order"""
for x in self.order_data:
yield x[1]
def allitems(self):
"""iterate over all key/value pairs in input order"""
return iter(self.order_data)
class UnorderedMultiDict(_BaseMultiDict):
"""Store key/value mappings.
Acts like a standard dictionary with the following features:
- duplicate keys are allowed;
- input order is preserved for all values of a given
key but not between different keys.
>>> ud = UnorderedMultiDict([("Food", "Spam"), ("Color", "Blue"),
... ("Food", "Eggs"), ("Color", "Green")])
>>> ud["Food"]
'Eggs'
>>> ud.getall("Food")
['Spam', 'Eggs']
>>>
The order of values from a given key (as from ud.getall("Food"))
is guaranteed but the order between keys (as from od.allkeys()
and od.allitems()) is not.
Can also pass in an object to the constructor which has an
allitems() method that returns a list of key/value pairs.
"""
def __init__(self, multidict=None):
self.data = {}
if multidict is not None:
if hasattr(multidict, "allitems"):
multidict = multidict.allitems()
for k, v in multidict:
self[k] = v
def __eq__(self, other):
"""Does this UnorderedMultiDict have the same keys, with values in the same order, as another?"""
return self.data == other.data
def __ne__(self, other):
"""Does this UnorderedMultiDict NOT have the same keys, with values in the same order, as another?"""
return self.data != other.data
def __repr__(self):
return "<UnorderedMultiDict %s>" % (self.data,)
def __setitem__(self, key, value):
"""Add a new key/value pair
If the key already exists, replaces the existing value
so that d[key] is the new value and not the old one.
To get all values for a given key, use d.getall(key).
"""
self.data.setdefault(key, []).append(value)
def __delitem__(self, key):
"""Remove all values for the given key"""
del self.data[key]
def allkeys(self):
"""iterate over all keys in arbitrary order"""
for k, v in self.data.iteritems():
for x in v:
yield k
def allvalues(self):
"""iterate over all values in arbitrary order"""
for v in self.data.itervalues():
for x in v:
yield x
def allitems(self):
"""iterate over all key/value pairs, in arbitrary order
Actually, the keys are iterated in arbitrary order but all
values for that key are iterated at sequence of addition
to the UnorderedMultiDict.
"""
for k, v in self.data.iteritems():
for x in v:
yield (k, x)
__test__ = {
"test_ordered_multidict": """
>>> od = OrderedMultiDict()
>>> od["Name"] = "Andrew"
>>> od["Color"] = "BLUE"
>>> od["Name"] = "Dalke"
>>> od["Color"] = "Green"
>>> od[3] = 9
>>> len(od)
3
>>> len(od.keys())
3
>>> len(od.values())
3
>>> len(od.items())
3
>>> od.keys()
['Color', 3, 'Name']
>>> "Name" in od and "Name" in od.keys() and "Name" in od.allkeys()
1
>>> "Color" in od and "Color" in od.keys() and "Color" in od.allkeys()
1
>>> 3 in od and 3 in od.keys() and 3 in od.allkeys()
1
>>> od == od
1
>>> od != OrderedMultiDict() # line 25
1
>>> list(od.allkeys())
['Name', 'Color', 'Name', 'Color', 3]
>>> list(od.allvalues())
['Andrew', 'BLUE', 'Dalke', 'Green', 9]
>>> list(od.allitems())
[('Name', 'Andrew'), ('Color', 'BLUE'), ('Name', 'Dalke'), ('Color', 'Green'), (3, 9)]
>>> len(list(od))
3
>>> od["invalid"]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "MultiDict.py", line 33, in __getitem__
return self.data[key]
KeyError: invalid
>>> od["Color"]
'Green'
>>> od.getall("Color")
['BLUE', 'Green']
>>> od2 = OrderedMultiDict(od)
>>> list(od2.allitems())
[('Name', 'Andrew'), ('Color', 'BLUE'), ('Name', 'Dalke'), ('Color', 'Green'), (3, 9)]
>>> od == od2
1
>>> od2 == od # line 53
1
>>> od2 != od
0
>>> del od["Color"]
>>> od["Color"]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "MultiDict.py", line 33, in __getitem__
return self.data[key]
KeyError: Color
>>> list(od.allitems())
[('Name', 'Andrew'), ('Name', 'Dalke'), (3, 9)]
>>> list(od2.allkeys())
['Name', 'Color', 'Name', 'Color', 3]
>>> od2["Color"]
'Green'
>>> od == od2
0
>>>
>>> s = str(od2)
>>> s = repr(od2)
""",
"test_unordered_multidict": """
>>> ud = UnorderedMultiDict()
>>> ud["Name"] = "Andrew"
>>> ud["Color"] = "BLUE"
>>> ud["Name"] = "Dalke"
>>> ud["Color"] = "GREEN"
>>> ud[3] = 9
>>> ud[3]
9
>>> ud["Name"]
'Dalke'
>>> ud["Color"] # line 11
'GREEN'
>>> ud[3]
9
>>> len(ud)
3
>>> len(list(ud)), len(ud.keys()), len(ud.values()), len(ud.items())
(3, 3, 3, 3)
>>> ud["invalid"]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "MultiDict.py", line 105, in __getitem__
return self.data[key][-1]
KeyError: invalid
>>> ud.get("invalid")
>>> ud.get("invalid") is None
1
>>> ud.get("invalid", "red")
'red'
>>> "Color" in ud
1
>>> "Color" in ud.keys() # line 32
1
>>> "invalid" in ud
0
>>> "invalid" in ud.keys()
0
>>> ud.get("Color", "red")
'GREEN'
>>> "Andrew" in ud.values()
0
>>> "Dalke" in ud.values()
1
>>> ud.getall("Color") # line 44
['BLUE', 'GREEN']
>>> ud.getall("invalid")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "MultiDict.py", line 126, in __getitem__
return self.data[key]
KeyError: invalid
>>> len(list(ud.allkeys())), len(list(ud.allvalues())), len(list(ud.allitems()))
(5, 5, 5)
>>> ("Color", "BLUE") in ud.allitems()
1
>>> ("Color", "GREEN") in ud.allitems()
1
>>> ("Name", "Andrew") in ud.allitems() # line 58
1
>>> ("Name", "Dalke") in ud.allitems()
1
>>> (3, 9) in ud.allitems()
1
>>> x = list(ud.allkeys())
>>> x.sort()
>>> x
[3, 'Color', 'Color', 'Name', 'Name']
>>> x = list(ud.allvalues())
>>> x.sort()
>>> x
[9, 'Andrew', 'BLUE', 'Dalke', 'GREEN']
>>> x = list(ud)
>>> x.sort()
>>> x
[3, 'Color', 'Name']
>>> ud2 = UnorderedMultiDict(ud) # line 76
>>> ud == ud2
1
>>> ud != ud
0
>>> del ud["Color"]
>>> ud == ud2
0
>>> ud != ud2
1
>>> len(ud)
2
>>> "Color" in ud
0
>>> "Color" in ud2 # line 90
1
>>> s = str(ud2)
>>> s = repr(ud2)
""",
"__doc__": __doc__,
}
def _test():
import doctest
import MultiDict
return doctest.testmod(MultiDict)
if __name__ == "__main__":
_test()