34397
|
1 |
"""
|
|
2 |
Commonly useful converters.
|
|
3 |
"""
|
|
4 |
|
|
5 |
from __future__ import absolute_import, division, print_function
|
|
6 |
|
|
7 |
|
|
8 |
def optional(converter):
|
|
9 |
"""
|
|
10 |
A converter that allows an attribute to be optional. An optional attribute
|
|
11 |
is one which can be set to ``None``.
|
|
12 |
|
|
13 |
:param callable converter: the converter that is used for non-``None``
|
|
14 |
values.
|
|
15 |
|
|
16 |
.. versionadded:: 17.1.0
|
|
17 |
"""
|
|
18 |
|
|
19 |
def optional_converter(val):
|
|
20 |
if val is None:
|
|
21 |
return None
|
|
22 |
return converter(val)
|
|
23 |
|
|
24 |
return optional_converter
|