2005-07-13 09:25:57 +08:00
|
|
|
class MergeDict:
|
|
|
|
"""
|
|
|
|
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):
|
|
|
|
for dict in self.dicts:
|
|
|
|
try:
|
|
|
|
return dict[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
def get(self, key, default):
|
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
|
|
|
def getlist(self, key):
|
|
|
|
for dict in self.dicts:
|
|
|
|
try:
|
|
|
|
return dict.getlist(key)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
item_list = []
|
|
|
|
for dict in self.dicts:
|
|
|
|
item_list.extend(dict.items())
|
|
|
|
return item_list
|
|
|
|
|
|
|
|
def has_key(self, key):
|
|
|
|
for dict in self.dicts:
|
|
|
|
if dict.has_key(key):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class SortedDict(dict):
|
|
|
|
"A dictionary that keeps its keys in the order in which they're inserted."
|
|
|
|
def __init__(self, data={}):
|
|
|
|
dict.__init__(self, data)
|
|
|
|
self.keyOrder = data.keys()
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
dict.__setitem__(self, key, value)
|
|
|
|
if key not in self.keyOrder:
|
|
|
|
self.keyOrder.append(key)
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
dict.__delitem__(self, key)
|
|
|
|
self.keyOrder.remove(key)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for k in self.keyOrder:
|
|
|
|
yield k
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return zip(self.keyOrder, self.values())
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self.keyOrder[:]
|
|
|
|
|
|
|
|
def values(self):
|
|
|
|
return [dict.__getitem__(self,k) for k in self.keyOrder]
|
|
|
|
|
|
|
|
def update(self, dict):
|
|
|
|
for k, v in dict.items():
|
|
|
|
self.__setitem__(k, v)
|
|
|
|
|
|
|
|
def setdefault(self, key, default):
|
|
|
|
if key not in self.keyOrder:
|
|
|
|
self.keyOrder.append(key)
|
|
|
|
return dict.setdefault(self, key, default)
|
|
|
|
|
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
|
|
|
"""
|
2005-11-30 12:08:46 +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=()):
|
|
|
|
dict.__init__(self, key_to_list_mapping)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-12-29 07:11:07 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "<MultiValueDict: %s>" % dict.__repr__(self)
|
|
|
|
|
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:
|
|
|
|
list_ = dict.__getitem__(self, key)
|
|
|
|
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):
|
2005-11-30 12:08:46 +08:00
|
|
|
dict.__setitem__(self, key, [value])
|
2006-03-29 01:31:04 +08:00
|
|
|
|
|
|
|
def __copy__(self):
|
|
|
|
return self.__class__(dict.items(self))
|
|
|
|
|
|
|
|
def __deepcopy__(self, memo={}):
|
|
|
|
import copy
|
|
|
|
result = self.__class__()
|
|
|
|
memo[id(self)] = result
|
|
|
|
for key, value in dict.items(self):
|
|
|
|
dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo))
|
|
|
|
return result
|
2005-11-30 08:19:15 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def get(self, key, default=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Returns the default value if the requested data doesn't exist"
|
|
|
|
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):
|
|
|
|
"Returns an empty list if the requested data doesn't exist"
|
|
|
|
try:
|
2005-11-30 12:08:46 +08:00
|
|
|
return dict.__getitem__(self, key)
|
2005-07-13 09:25:57 +08:00
|
|
|
except KeyError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def setlist(self, key, list_):
|
2005-11-30 12:08:46 +08:00
|
|
|
dict.__setitem__(self, 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):
|
|
|
|
"Appends an item to the internal list associated with key"
|
|
|
|
self.setlistdefault(key, [])
|
|
|
|
dict.__setitem__(self, 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):
|
|
|
|
"Returns a list of (key, list) pairs."
|
|
|
|
return dict.items(self)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def values(self):
|
|
|
|
"Returns a list of the last value on every key list."
|
|
|
|
return [self[key] for key in self.keys()]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def copy(self):
|
2005-11-30 12:08:46 +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
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def update(self, other_dict):
|
|
|
|
"update() extends rather than replaces existing key lists."
|
|
|
|
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"
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
>>> 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': {'firstname': ['Simon'], 'lastname': ['Willison'],
|
|
|
|
'2': {'firstname': ['Adrian'], 'lastname': ['Holovaty']}
|
|
|
|
>>> d['person']['1']
|
|
|
|
{'firstname': ['Simon'], 'lastname': ['Willison']}
|
|
|
|
|
|
|
|
# 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.
|
2006-05-02 09:31:56 +08:00
|
|
|
current = {bits[-1] : v}
|