Skip to content

Commit 9720a73

Browse files
committed
Document new multidicts
1 parent b1d9829 commit 9720a73

File tree

1 file changed

+141
-48
lines changed

1 file changed

+141
-48
lines changed

docs/multidict.rst

Lines changed: 141 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ Multidicts
1313
several *values* for the same *key*.
1414

1515
:mod:`aiohttp.multidict` has four multidict classes:
16-
:class:`MultiDict`, :class:`MutableMultiDict`, :class:`CIMultiDict`
17-
and :class:`CIMutableMultiDict`.
16+
:class:`MultiDict`, :class:`MultiDictProxy`, :class:`CIMultiDict`
17+
and :class:`CIMultiDictProxy`.
1818

19+
Immutable proxies (:class:`MultiDictProxy` and
20+
:class:`CIMultiDictProxy`) provide a dynamic view on the
21+
proxied multidict, the view reflects the multidict changes. They are
22+
implement :class:`~collections.abc.Mapping` interface.
1923

20-
Immutable (:class:`MultiDict` and :class:`CIMultiDict`) classes
21-
doesn't allow to change *muldidict* content. They are implement
22-
:class:`~collections.abc.Mapping` interface.
23-
24-
Mutable (:class:`MutableMultiDict` and :class:`CIMutableMultiDict`)
25-
ones implement :class:`~collections.abc.MutableMapping`.
24+
Regular mutable (:class:`MultiDict` and :class:`CIMultiDict`) clases
25+
implement :class:`~collections.abc.MutableMapping` and allow to change
26+
own content.
2627

2728

2829
*Case insensitive* (:class:`CIMultiDict` and
29-
:class:`CIMutableMultiDict`) ones assumes the *keys* are case
30+
:class:`CIMultiDictProxy`) ones assumes the *keys* are case
3031
insensitive, e.g.::
3132

3233
>>> dct = CIMultiDict(a='val')
@@ -38,20 +39,20 @@ insensitive, e.g.::
3839
*Keys* should be a :class:`str`.
3940

4041

41-
MultiDict
42-
---------
42+
MutableMultiDict
43+
----------------
4344

44-
.. class:: MultiDict(**kwargs)
45-
MultiDict(mapping, **kwargs)
46-
MultiDict(iterable, **kwargs)
45+
.. class:: MutableMultiDict(**kwargs)
46+
MutableMultiDict(mapping, **kwargs)
47+
MutableMultiDict(iterable, **kwargs)
4748
48-
Create an immutable multidict instance.
49+
Create a mutable multidict instance.
4950

5051
Accepted parameters are the same as for :class:`dict`.
5152

5253
If the same key produced several times it will be added, e.g.::
5354

54-
>>> d = MultiDict([('a', 1), ('b', 2), ('a', 3)])
55+
>>> d = MultiDict[('a', 1), ('b', 2), ('a', 3)])
5556
>>> d
5657
<MultiDict {'a': 1, 'b': 2, 'a': 3}>
5758

@@ -65,6 +66,18 @@ MultiDict
6566

6667
Raises a :exc:`KeyError` if key is not in the multidict.
6768

69+
.. method:: d[key] = value
70+
71+
Set ``d[key]`` to *value*.
72+
73+
Replace all items where key is equal to *key* with single item
74+
``(key, value)``.
75+
76+
.. method:: del d[key]
77+
78+
Remove all items where key is equal to *key* from *d*.
79+
Raises a :exc:`KeyError` if *key* is not in the map.
80+
6881
.. method:: key in d
6982

7083
Return ``True`` if d has a key *key*, else ``False``.
@@ -78,10 +91,30 @@ MultiDict
7891
Return an iterator over the keys of the dictionary.
7992
This is a shortcut for ``iter(d.keys())``.
8093

94+
.. method:: add(key, value)
95+
96+
Append ``(key, value)`` pair to the dictiaonary.
97+
98+
.. method:: clear()
99+
100+
Remove all items from the dictionary.
101+
81102
.. method:: copy()
82103

83104
Return a shallow copy of the dictionary.
84105

106+
.. method:: extend([other])
107+
108+
Extend the dictionary with the key/value pairs from *other*,
109+
overwriting existing keys.
110+
Return ``None``.
111+
112+
:meth:`extend` accepts either another dictionary object or an
113+
iterable of key/value pairs (as tuples or other iterables of
114+
length two). If keyword arguments are specified, the dictionary
115+
is then extended with those key/value pairs:
116+
``d.extend(red=1, blue=2)``.
117+
85118
.. method:: getone(key[, default])
86119

87120
Return the **first** value for *key* if *key* is in the
@@ -129,6 +162,12 @@ MultiDict
129162
View contains all values if *getall* is ``True`` (default) or
130163
only first key occurrences otherwise.
131164

165+
.. method:: setdefault(key[, default])
166+
167+
If *key* is in the dictionary, return its the **first** value.
168+
If not, insert *key* with a value of *default* and return *default*.
169+
*default* defaults to ``None``.
170+
132171

133172
CIMultiDict
134173
-----------
@@ -138,7 +177,7 @@ CIMultiDict
138177
CIMultiDict(mapping, **kwargs)
139178
CIMultiDict(iterable, **kwargs)
140179
141-
Create an immutable case insensitive multidict instance.
180+
Create a case insensitive multidict instance.
142181

143182
The behavior is the same as of :class:`MultiDict` but key
144183
comparsions are case insensitive, e.g.::
@@ -150,53 +189,107 @@ CIMultiDict
150189
'val'
151190
>>> dct['a']
152191
'val'
192+
>>> dct['b'] = 'new val'
193+
>>> dct['B']
194+
'new val'
153195

196+
The class is inherited from :class:`MultiDict`.
154197

155-
MutableMultiDict
156-
----------------
157198

158-
.. class:: MutableMultiDict(**kwargs)
159-
MutableMultiDict(mapping, **kwargs)
160-
MutableMultiDict(iterable, **kwargs)
199+
MultiDictProxy
200+
---------------
161201

162-
Create a mutable multidict instance.
202+
.. class:: MultiDictProxy(multidict)
163203

164-
The class inherited from :class:`MultiDict`.
204+
Create an immutable multidict proxy.
165205

166-
.. method:: d[key] = value
206+
It provides a dynamic view on
207+
the multidict’s entries, which means that when the multidict changes,
208+
the view reflects these changes.
167209

168-
Set ``d[key]`` to *value*.
210+
Raises :exc:`TypeError` is *multidict* is not :class:`MultiDict` instance.
169211

170-
Replace all items where key is equal to *key* with single item
171-
``(key, value)``.
212+
.. method:: len(d)
172213

173-
.. method:: del d[key]
214+
Return number of items in multidict *d*.
174215

175-
Remove all items where key is equal to *key* from *d*.
176-
Raises a :exc:`KeyError` if *key* is not in the map.
216+
.. method:: d[key]
177217

178-
.. method:: add(key, value)
218+
Return the **first** item of *d* with key *key*.
179219

180-
Append ``(key, value)`` pair to the dictiaonary.
220+
Raises a :exc:`KeyError` if key is not in the multidict.
181221

182-
.. method:: clear()
222+
.. method:: key in d
183223

184-
Remove all items from the dictionary.
224+
Return ``True`` if d has a key *key*, else ``False``.
185225

186-
.. method:: extend([other])
226+
.. method:: key not in d
187227

188-
Extend the dictionary with the key/value pairs from *other*,
189-
overwriting existing keys.
190-
Return ``None``.
228+
Equivalent to ``not (key in d)``
191229

192-
:meth:`extend` accepts either another dictionary object or an
193-
iterable of key/value pairs (as tuples or other iterables of
194-
length two). If keyword arguments are specified, the dictionary
195-
is then extended with those key/value pairs:
196-
``d.extend(red=1, blue=2)``.
230+
.. method:: iter(d)
197231

198-
.. method:: setdefault(key[, default])
232+
Return an iterator over the keys of the dictionary.
233+
This is a shortcut for ``iter(d.keys())``.
199234

200-
If *key* is in the dictionary, return its the **first** value.
201-
If not, insert *key* with a value of *default* and return *default*.
202-
*default* defaults to ``None``.
235+
.. method:: copy()
236+
237+
Return a shallow copy of the dictionary.
238+
239+
.. method:: getone(key[, default])
240+
241+
Return the **first** value for *key* if *key* is in the
242+
dictionary, else *default*.
243+
244+
Raises :exc:`KeyError` if *default* is not given and *key* is not found.
245+
246+
``d[key]`` is equivalent to ``d.getone(key)``.
247+
248+
.. method:: getall(key[, default])
249+
250+
Return a list of all values for *key* if *key* is in the
251+
dictionary, else *default*.
252+
253+
Raises :exc:`KeyError` if *default* is not given and *key* is not found.
254+
255+
.. method:: get(key[, default])
256+
257+
Return the **first** value for *key* if *key* is in the
258+
dictionary, else *default*.
259+
260+
If *default* is not given, it defaults to ``None``, so that this
261+
method never raises a :exc:`KeyError`.
262+
263+
``d.get(key)`` is equivalent to ``d.getone(key, None)``.
264+
265+
.. method:: keys(getall=True)
266+
267+
Return a new view of the dictionary's keys.
268+
269+
View contains all keys if *getall* is ``True`` (default) or
270+
distinct set of ones otherwise.
271+
272+
.. method:: keys(getall=True)
273+
274+
Return a new view of the dictionary's items (``(key, value)`` pairs).
275+
276+
View contains all items if *getall* is ``True`` (default) or
277+
only first key occurrences otherwise.
278+
279+
.. method:: values(getall=True)
280+
281+
Return a new view of the dictionary's values.
282+
283+
View contains all values if *getall* is ``True`` (default) or
284+
only first key occurrences otherwise.
285+
286+
CIMultiDictProxy
287+
----------------
288+
289+
.. class:: CIMultiDictProxy(multidict)
290+
291+
Case insensitive version of :class:`MultiDictProxy`.
292+
293+
Raises :exc:`TypeError` is *multidict* is not :class:`CIMultiDict` instance.
294+
295+
The class is inherited from :class:`MultiDict`.

0 commit comments

Comments
 (0)