2006-06-08 13:00:13 +08:00
|
|
|
class MergeDict(object):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
A simple class for creating new "virtual" dictionaries that actualy look
|
|
|
|
up values in more than one dictionary, passed in the constructor.
|
|
|
|
"""
|
|
|
|
def __init__(self, *dicts):
|
|
|
|
self.dicts = dicts
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2007-11-26 02:07:57 +08:00
|
|
|
return dict_[key]
|
2005-07-13 09:25:57 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
raise KeyError
|
|
|
|
|
2007-04-25 15:25:22 +08:00
|
|
|
def __copy__(self):
|
|
|
|
return self.__class__(*self.dicts)
|
2006-09-23 21:55:04 +08:00
|
|
|
|
2007-02-26 00:37:31 +08:00
|
|
|
def get(self, key, default=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
|
|
|
def getlist(self, key):
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2007-11-26 02:07:57 +08:00
|
|
|
return dict_.getlist(key)
|
2005-07-13 09:25:57 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
item_list = []
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
|
|
|
item_list.extend(dict_.items())
|
2005-07-13 09:25:57 +08:00
|
|
|
return item_list
|
|
|
|
|
|
|
|
def has_key(self, key):
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
|
|
|
if key in dict_:
|
2005-07-13 09:25:57 +08:00
|
|
|
return True
|
|
|
|
return False
|
2007-10-19 09:26:09 +08:00
|
|
|
|
2007-09-11 22:04:40 +08:00
|
|
|
__contains__ = has_key
|
2007-04-25 15:25:22 +08:00
|
|
|
|
|
|
|
def copy(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns a copy of this object."""
|
2007-02-28 08:35:50 +08:00
|
|
|
return self.__copy__()
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class SortedDict(dict):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""
|
|
|
|
A dictionary that keeps its keys in the order in which they're inserted.
|
|
|
|
"""
|
2006-06-03 21:37:34 +08:00
|
|
|
def __init__(self, data=None):
|
2007-10-19 09:26:09 +08:00
|
|
|
if data is None:
|
|
|
|
data = {}
|
2007-11-26 02:10:45 +08:00
|
|
|
super(SortedDict, self).__init__(data)
|
2007-10-14 12:17:02 +08:00
|
|
|
if isinstance(data, dict):
|
|
|
|
self.keyOrder = data.keys()
|
|
|
|
else:
|
2007-10-19 09:26:09 +08:00
|
|
|
self.keyOrder = [key for key, value in data]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-11-12 11:12:47 +08:00
|
|
|
def __deepcopy__(self, memo):
|
2007-10-22 08:52:54 +08:00
|
|
|
from copy import deepcopy
|
2007-11-12 11:12:47 +08:00
|
|
|
return self.__class__([(key, deepcopy(value, memo))
|
|
|
|
for key, value in self.iteritems()])
|
2007-10-22 08:52:54 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __setitem__(self, key, value):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(SortedDict, self).__setitem__(key, value)
|
2006-05-02 09:31:56 +08:00
|
|
|
if key not in self.keyOrder:
|
|
|
|
self.keyOrder.append(key)
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(SortedDict, self).__delitem__(key)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.keyOrder.remove(key)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for k in self.keyOrder:
|
|
|
|
yield k
|
|
|
|
|
2007-10-22 08:52:54 +08:00
|
|
|
def pop(self, k, *args):
|
2007-11-26 02:10:45 +08:00
|
|
|
result = super(SortedDict, self).pop(k, *args)
|
2007-10-22 08:52:54 +08:00
|
|
|
try:
|
|
|
|
self.keyOrder.remove(k)
|
|
|
|
except ValueError:
|
|
|
|
# Key wasn't in the dictionary in the first place. No problem.
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
|
|
|
|
def popitem(self):
|
2007-11-26 02:10:45 +08:00
|
|
|
result = super(SortedDict, self).popitem()
|
2007-10-22 08:52:54 +08:00
|
|
|
self.keyOrder.remove(result[0])
|
|
|
|
return result
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def items(self):
|
|
|
|
return zip(self.keyOrder, self.values())
|
|
|
|
|
2007-09-16 11:27:38 +08:00
|
|
|
def iteritems(self):
|
|
|
|
for key in self.keyOrder:
|
2007-11-26 02:10:45 +08:00
|
|
|
yield key, super(SortedDict, self).__getitem__(key)
|
2007-09-16 11:27:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def keys(self):
|
|
|
|
return self.keyOrder[:]
|
|
|
|
|
2007-09-16 11:27:38 +08:00
|
|
|
def iterkeys(self):
|
|
|
|
return iter(self.keyOrder)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def values(self):
|
2007-11-26 02:10:45 +08:00
|
|
|
return [super(SortedDict, self).__getitem__(k) for k in self.keyOrder]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-09-16 11:27:38 +08:00
|
|
|
def itervalues(self):
|
|
|
|
for key in self.keyOrder:
|
2007-11-26 02:10:45 +08:00
|
|
|
yield super(SortedDict, self).__getitem__(key)
|
2007-09-16 11:27:38 +08:00
|
|
|
|
2007-11-26 02:07:57 +08:00
|
|
|
def update(self, dict_):
|
|
|
|
for k, v in dict_.items():
|
2006-05-02 09:31:56 +08:00
|
|
|
self.__setitem__(k, v)
|
|
|
|
|
|
|
|
def setdefault(self, key, default):
|
|
|
|
if key not in self.keyOrder:
|
|
|
|
self.keyOrder.append(key)
|
2007-11-26 02:10:45 +08:00
|
|
|
return super(SortedDict, self).setdefault(key, default)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-01-21 08:06:55 +08:00
|
|
|
def value_for_index(self, index):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns the value of the item at the given zero-based index."""
|
2007-01-21 08:06:55 +08:00
|
|
|
return self[self.keyOrder[index]]
|
|
|
|
|
2007-09-16 11:27:38 +08:00
|
|
|
def insert(self, index, key, value):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Inserts the key, value pair before the item with the given index."""
|
2007-09-16 11:27:38 +08:00
|
|
|
if key in self.keyOrder:
|
|
|
|
n = self.keyOrder.index(key)
|
|
|
|
del self.keyOrder[n]
|
2007-10-19 09:26:09 +08:00
|
|
|
if n < index:
|
|
|
|
index -= 1
|
2007-09-16 11:27:38 +08:00
|
|
|
self.keyOrder.insert(index, key)
|
2007-11-26 02:10:45 +08:00
|
|
|
super(SortedDict, self).__setitem__(key, value)
|
2007-09-16 11:27:38 +08:00
|
|
|
|
2007-03-09 13:34:42 +08:00
|
|
|
def copy(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns a copy of this object."""
|
2007-03-16 23:10:48 +08:00
|
|
|
# This way of initializing the copy means it works for subclasses, too.
|
2007-03-09 13:34:42 +08:00
|
|
|
obj = self.__class__(self)
|
|
|
|
obj.keyOrder = self.keyOrder
|
|
|
|
return obj
|
|
|
|
|
2007-04-25 15:30:54 +08:00
|
|
|
def __repr__(self):
|
|
|
|
"""
|
|
|
|
Replaces the normal dict.__repr__ with a version that returns the keys
|
|
|
|
in their sorted order.
|
|
|
|
"""
|
|
|
|
return '{%s}' % ', '.join(['%r: %r' % (k, v) for k, v in self.items()])
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
class MultiValueDictKeyError(KeyError):
|
|
|
|
pass
|
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
class MultiValueDict(dict):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
A subclass of dictionary customized to handle multiple values for the
|
|
|
|
same key.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
>>> 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'])
|
|
|
|
|
|
|
|
This class exists to solve the irritating problem raised by cgi.parse_qs,
|
|
|
|
which returns a list for every key, even though most Web forms submit
|
|
|
|
single name-value pairs.
|
|
|
|
"""
|
2005-11-30 12:08:46 +08:00
|
|
|
def __init__(self, key_to_list_mapping=()):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(MultiValueDict, self).__init__(key_to_list_mapping)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-12-29 07:11:07 +08:00
|
|
|
def __repr__(self):
|
2007-11-26 02:10:45 +08:00
|
|
|
return "<%s: %s>" % (self.__class__.__name__,
|
|
|
|
super(MultiValueDict, self).__repr__())
|
2005-12-29 07:11:07 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __getitem__(self, key):
|
2005-11-30 12:08:46 +08:00
|
|
|
"""
|
|
|
|
Returns the last data value for this key, or [] if it's an empty list;
|
|
|
|
raises KeyError if not found.
|
|
|
|
"""
|
|
|
|
try:
|
2007-11-26 02:10:45 +08:00
|
|
|
list_ = super(MultiValueDict, self).__getitem__(key)
|
2005-11-30 12:08:46 +08:00
|
|
|
except KeyError:
|
2006-01-05 01:09:26 +08:00
|
|
|
raise MultiValueDictKeyError, "Key %r not found in %r" % (key, self)
|
2005-11-30 12:08:46 +08:00
|
|
|
try:
|
|
|
|
return list_[-1]
|
|
|
|
except IndexError:
|
|
|
|
return []
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-03-29 01:31:04 +08:00
|
|
|
def __setitem__(self, key, value):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(MultiValueDict, self).__setitem__(key, [value])
|
2006-03-29 01:31:04 +08:00
|
|
|
|
|
|
|
def __copy__(self):
|
2007-11-26 02:10:45 +08:00
|
|
|
return self.__class__(super(MultiValueDict, self).items())
|
2006-03-29 01:31:04 +08:00
|
|
|
|
2006-06-05 08:46:18 +08:00
|
|
|
def __deepcopy__(self, memo=None):
|
2006-03-29 01:31:04 +08:00
|
|
|
import copy
|
2007-10-19 09:26:09 +08:00
|
|
|
if memo is None:
|
|
|
|
memo = {}
|
2006-03-29 01:31:04 +08:00
|
|
|
result = self.__class__()
|
|
|
|
memo[id(self)] = result
|
|
|
|
for key, value in dict.items(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
dict.__setitem__(result, copy.deepcopy(key, memo),
|
|
|
|
copy.deepcopy(value, memo))
|
2006-03-29 01:31:04 +08:00
|
|
|
return result
|
2005-11-30 08:19:15 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def get(self, key, default=None):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns the default value if the requested data doesn't exist."""
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
val = self[key]
|
2005-11-30 12:08:46 +08:00
|
|
|
except KeyError:
|
2005-07-13 09:25:57 +08:00
|
|
|
return default
|
|
|
|
if val == []:
|
|
|
|
return default
|
|
|
|
return val
|
|
|
|
|
|
|
|
def getlist(self, key):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns an empty list if the requested data doesn't exist."""
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2007-11-26 02:10:45 +08:00
|
|
|
return super(MultiValueDict, self).__getitem__(key)
|
2005-07-13 09:25:57 +08:00
|
|
|
except KeyError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def setlist(self, key, list_):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(MultiValueDict, self).__setitem__(key, list_)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def setdefault(self, key, default=None):
|
|
|
|
if key not in self:
|
|
|
|
self[key] = default
|
|
|
|
return self[key]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def setlistdefault(self, key, default_list=()):
|
|
|
|
if key not in self:
|
|
|
|
self.setlist(key, default_list)
|
|
|
|
return self.getlist(key)
|
|
|
|
|
|
|
|
def appendlist(self, key, value):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Appends an item to the internal list associated with key."""
|
2005-11-30 12:08:46 +08:00
|
|
|
self.setlistdefault(key, [])
|
2007-11-26 02:10:45 +08:00
|
|
|
super(MultiValueDict, self).__setitem__(key, self.getlist(key) + [value])
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def items(self):
|
2005-11-30 12:08:46 +08:00
|
|
|
"""
|
|
|
|
Returns a list of (key, value) pairs, where value is the last item in
|
|
|
|
the list associated with the key.
|
|
|
|
"""
|
|
|
|
return [(key, self[key]) for key in self.keys()]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def lists(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns a list of (key, list) pairs."""
|
2007-11-26 02:10:45 +08:00
|
|
|
return super(MultiValueDict, self).items()
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def values(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns a list of the last value on every key list."""
|
2005-11-30 12:08:46 +08:00
|
|
|
return [self[key] for key in self.keys()]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def copy(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns a copy of this object."""
|
2006-03-29 01:31:04 +08:00
|
|
|
return self.__deepcopy__()
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-09-01 05:51:19 +08:00
|
|
|
def update(self, *args, **kwargs):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""
|
|
|
|
update() extends rather than replaces existing key lists.
|
|
|
|
Also accepts keyword args.
|
|
|
|
"""
|
2006-09-01 05:51:19 +08:00
|
|
|
if len(args) > 1:
|
2007-04-23 09:55:45 +08:00
|
|
|
raise TypeError, "update expected at most 1 arguments, got %d" % len(args)
|
2006-09-01 05:51:19 +08:00
|
|
|
if args:
|
|
|
|
other_dict = args[0]
|
|
|
|
if isinstance(other_dict, MultiValueDict):
|
|
|
|
for key, value_list in other_dict.lists():
|
|
|
|
self.setlistdefault(key, []).extend(value_list)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
for key, value in other_dict.items():
|
|
|
|
self.setlistdefault(key, []).append(value)
|
|
|
|
except TypeError:
|
|
|
|
raise ValueError, "MultiValueDict.update() takes either a MultiValueDict or dictionary"
|
|
|
|
for key, value in kwargs.iteritems():
|
|
|
|
self.setlistdefault(key, []).append(value)
|
2005-11-30 12:08:46 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
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.
|
|
|
|
|
2007-09-15 16:29:56 +08:00
|
|
|
>>> d = DotExpandedDict({'person.1.firstname': ['Simon'], \
|
|
|
|
'person.1.lastname': ['Willison'], \
|
|
|
|
'person.2.firstname': ['Adrian'], \
|
2005-07-13 09:25:57 +08:00
|
|
|
'person.2.lastname': ['Holovaty']})
|
|
|
|
>>> d
|
2007-09-15 16:29:56 +08:00
|
|
|
{'person': {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}}
|
2005-07-13 09:25:57 +08:00
|
|
|
>>> d['person']
|
2007-09-15 16:29:56 +08:00
|
|
|
{'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}
|
2005-07-13 09:25:57 +08:00
|
|
|
>>> d['person']['1']
|
2007-09-15 16:29:56 +08:00
|
|
|
{'lastname': ['Willison'], 'firstname': ['Simon']}
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
# Gotcha: Results are unpredictable if the dots are "uneven":
|
|
|
|
>>> DotExpandedDict({'c.1': 2, 'c.2': 3, 'c': 1})
|
2007-09-15 16:29:56 +08:00
|
|
|
{'c': 1}
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
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.
|
2007-11-26 02:11:44 +08:00
|
|
|
current = {bits[-1]: v}
|
2007-08-12 20:02:08 +08:00
|
|
|
|
|
|
|
class FileDict(dict):
|
|
|
|
"""
|
|
|
|
A dictionary used to hold uploaded file contents. The only special feature
|
|
|
|
here is that repr() of this object won't dump the entire contents of the
|
|
|
|
file to the output. A handy safeguard for a large file upload.
|
|
|
|
"""
|
|
|
|
def __repr__(self):
|
|
|
|
if 'content' in self:
|
|
|
|
d = dict(self, content='<omitted>')
|
|
|
|
return dict.__repr__(d)
|
|
|
|
return dict.__repr__(self)
|