Werkzeug provides some subclasses of common Python objects to extend them with additional features. Some of them are used to make them immutable, others are used to change some semantics to better work with HTTP.
在 0.6 版更改: The general purpose classes are now pickleable in each protocol as long as the contained objects are pickleable. This means that the FileMultiDict won’t be pickleable as soon as it contains a file.
Works like a regular dict but the get() method can perform type conversions. MultiDict and CombinedMultiDict are subclasses of this class and provide the same feature.
0.5 新版功能.
Return the default value if the requested data doesn’t exist. If type is provided and is a callable it should convert the value, return it or raise a ValueError if that is not possible. In this case the function will return the default as if the value was not found:
>>> d = TypeConversionDict(foo='42', bar='blub')
>>> d.get('foo', type=int)
42
>>> d.get('bar', -1, type=int)
-1
参数: |
|
---|
Works like a TypeConversionDict but does not support modifications.
0.5 新版功能.
A MultiDict is a dictionary subclass customized to deal with multiple values for the same key which is for example used by the parsing functions in the wrappers. This is necessary because some HTML form elements pass multiple values for the same key.
MultiDict implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key. If you want to gain access to the other values, too, you have to use the list methods as explained below.
Basic Usage:
>>> d = MultiDict([('a', 'b'), ('a', 'c')])
>>> d
MultiDict([('a', 'b'), ('a', 'c')])
>>> d['a']
'b'
>>> d.getlist('a')
['b', 'c']
>>> 'a' in d
True
It behaves like a normal dict thus all dict functions will only return the first value when multiple values for one key are found.
From Werkzeug 0.3 onwards, the KeyError raised by this class is also a subclass of the BadRequest HTTP exception and will render a page for a 400 BAD REQUEST if caught in a catch-all for HTTP exceptions.
A MultiDict can be constructed from an iterable of (key, value) tuples, a dict, a MultiDict or from Werkzeug 0.2 onwards some keyword parameters.
参数: | mapping – the initial value for the MultiDict. Either a regular dict, an iterable of (key, value) tuples or None. |
---|
Adds a new value for the key.
0.6 新版功能.
参数: |
|
---|
Return a shallow copy of this object.
v defaults to None.
Return the default value if the requested data doesn’t exist. If type is provided and is a callable it should convert the value, return it or raise a ValueError if that is not possible. In this case the function will return the default as if the value was not found:
>>> d = TypeConversionDict(foo='42', bar='blub')
>>> d.get('foo', type=int)
42
>>> d.get('bar', -1, type=int)
-1
参数: |
|
---|
Return the list of items for a given key. If that key is not in the MultiDict, the return value will be an empty list. Just as get getlist accepts a type parameter. All items will be converted with the callable defined there.
参数: |
|
---|---|
返回: | a list of all the values for the key. |
Like iteritems(), but returns a list.
Return an iterator of (key, value) pairs.
参数: | multi – If set to True the iterator returned will have a pair for each value of each key. Otherwise it will only contain pairs for the first value of each key. |
---|
Return a list of (key, values) pairs, where values is the list of all values associated with the key.
Return an iterator of all values associated with a key. Zipping keys() and this is the same as calling lists():
>>> d = MultiDict({"foo": [1, 2, 3]})
>>> zip(d.keys(), d.listvalues()) == d.lists()
True
Returns an iterator of the first value on every key’s value list.
Like iterkeys(), but returns a list.
Like iterlists(), but returns a list.
Like iterlistvalues(), but returns a list.
Pop the first item for a list on the dict. Afterwards the key is removed from the dict, so additional values are discarded:
>>> d = MultiDict({"foo": [1, 2, 3]})
>>> d.pop("foo")
1
>>> "foo" in d
False
参数: |
|
---|
Pop an item from the dict.
Pop a (key, list) tuple from the dict.
Pop the list for a key from the dict. If the key is not in the dict an empty list is returned.
在 0.5 版更改: If the key does no longer exist a list is returned instead of raising an error.
Returns the value for the key if it is in the dict, otherwise it returns default and sets that value for key.
参数: |
|
---|
Remove the old values for a key and add new ones. Note that the list you pass the values in will be shallow-copied before it is inserted in the dictionary.
>>> d = MultiDict()
>>> d.setlist('foo', ['1', '2'])
>>> d['foo']
'1'
>>> d.getlist('foo')
['1', '2']
参数: |
|
---|
Like setdefault but sets multiple values. The list returned is not a copy, but the list that is actually used internally. This means that you can put new values into the dict by appending items to the list:
>>> d = MultiDict({"foo": 1})
>>> d.setlistdefault("foo").extend([2, 3])
>>> d.getlist("foo")
[1, 2, 3]
参数: |
|
---|---|
返回: | a list |
Return the contents as regular dict. If flat is True the returned dict will only have the first item present, if flat is False all values will be returned as lists.
参数: | flat – If set to False the dict returned will have lists with all the values in it. Otherwise it will only contain the first value for each key. |
---|---|
返回: | a dict |
update() extends rather than replaces existing key lists.
Like itervalues(), but returns a list.
Works like a regular MultiDict but preserves the order of the fields. To convert the ordered multi dict into a list you can use the items() method and pass it multi=True.
In general an OrderedMultiDict is an order of magnitude slower than a MultiDict.
note
Due to a limitation in Python you cannot convert an ordered multi dict into a regular dict by using dict(multidict). Instead you have to use the to_dict() method, otherwise the internal bucket objects are exposed.
An immutable OrderedMultiDict.
0.6 新版功能.
A read only MultiDict that you can pass multiple MultiDict instances as sequence and it will combine the return values of all wrapped dicts:
>>> from werkzeug.datastructures import CombinedMultiDict, MultiDict
>>> post = MultiDict([('foo', 'bar')])
>>> get = MultiDict([('blub', 'blah')])
>>> combined = CombinedMultiDict([get, post])
>>> combined['foo']
'bar'
>>> combined['blub']
'blah'
This works for all read operations and will raise a TypeError for methods that usually change data which isn’t possible.
From Werkzeug 0.3 onwards, the KeyError raised by this class is also a subclass of the BadRequest HTTP exception and will render a page for a 400 BAD REQUEST if caught in a catch-all for HTTP exceptions.
An immutable dict.
0.5 新版功能.
An immutable list.
0.5 新版功能.
Private: |
---|
A special MultiDict that has convenience methods to add files to it. This is used for EnvironBuilder and generally useful for unittesting.
0.5 新版功能.
Adds a new file to the dict. file can be a file name or a file-like or a FileStorage object.
参数: |
|
---|
The FileStorage class is a thin wrapper over incoming files. It is used by the request object to represent uploaded files. All the attributes of the wrapper stream are proxied by the file storage so it’s possible to do storage.read() instead of the long form storage.stream.read().
The input stream for the uploaded file. This usually points to an open temporary file.
The filename of the file on the client.
The name of the form field.
The multipart headers as Headers object. This usually contains irrelevant information but in combination with custom multipart requests the raw headers might be interesting.
0.6 新版功能.
Close the underlying file if possible.
The content-length sent in the header. Usually not available
The content-type sent in the header. Usually not available
Like content_type but without parameters (eg, without charset, type etc.). For example if the content type is text/html; charset=utf-8 the mimetype would be 'text/html'.
0.7 新版功能.
The mimetype parameters as dict. For example if the content type is text/html; charset=utf-8 the params would be {'charset': 'utf-8'}.
0.7 新版功能.
Save the file to a destination path or file object. If the destination is a file object you have to close it yourself after the call. The buffer size is the number of bytes held in memory during the copy process. It defaults to 16KB.
For secure file saving also have a look at secure_filename().
参数: |
|
---|