webob.multidict – multi-value dictionary object

multidict

Several parts of WebOb use a “multidict”, which is a dictionary where a key can have multiple values. The quintessential example is a query string like ?pref=red&pref=blue. The pref variable has two values, red and blue.

In a multidict, when you do request.GET['pref'], you’ll get back only 'blue' (the last value of pref). Sometimes returning a string and other times returning a list is a cause of frequent exceptions. If you want all the values back, use request.GET.getall('pref'). If you want to be sure there is one and only one value, use request.GET.getone('pref'), which will raise an exception if there is zero or more than one value for pref.

When you use operations like request.GET.items(), you’ll get back something like [('pref', 'red'), ('pref', 'blue')]. All the key/value pairs will show up. Similarly request.GET.keys() returns ['pref', 'pref']. Multidict is a view on a list of tuples; all the keys are ordered, and all the values are ordered.

Gives a multi-value dictionary object (MultiDict) plus several wrappers

class webob.multidict.MultiDict(*args, **kw)

An ordered dictionary that can have multiple values for each key. Adds the methods getall, getone, mixed and extend and add to the normal dictionary interface.

add(key, value)

Add the key and value, not overwriting any previous value.

dict_of_lists()

Returns a dictionary where each key is associated with a list of values.

classmethod from_fieldstorage(fs)

Create a dict from a cgi.FieldStorage instance

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
getall(key)

Return a list of all values matching the key (may be an empty list)

getone(key)

Get one value matching the key, raising a KeyError if multiple values were found.

mixed()

Returns a dictionary where the values are either single values, or a list of values when a key/value appears more than once in this dictionary. This is similar to the kind of dictionary often used to represent the variables in a web request.

classmethod view_list(lst)

Create a dict that is a view on the given list

class webob.multidict.NestedMultiDict(*dicts)

Wraps several MultiDict objects, treating it as one large MultiDict

class webob.multidict.NoVars(reason=None)

Represents no variables; used when no variables are applicable.

This is read-only