mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
New Param: fixed small bug in Param.convert() and added detailed docstring
This commit is contained in:
parent
bf8154fa50
commit
ac335bc7ea
@ -328,14 +328,61 @@ class Param(ReadOnly):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def convert(self, value):
|
def convert(self, value):
|
||||||
|
"""
|
||||||
|
Convert ``value`` to the Python type required by this parameter.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
>>> scalar = Str('my_scalar')
|
||||||
|
>>> scalar.type
|
||||||
|
<type 'unicode'>
|
||||||
|
>>> scalar.convert(43.2)
|
||||||
|
u'43.2'
|
||||||
|
|
||||||
|
(Note that `Str` is a subclass of `Param`.)
|
||||||
|
|
||||||
|
All values in `constants.NULLS` will be converted to None. For
|
||||||
|
example:
|
||||||
|
|
||||||
|
>>> scalar.convert(u'') is None # An empty string
|
||||||
|
True
|
||||||
|
>>> scalar.convert([]) is None # An empty list
|
||||||
|
True
|
||||||
|
|
||||||
|
Likewise, values in `constants.NULLS` will be filtered out of a
|
||||||
|
multivalue parameter. For example:
|
||||||
|
|
||||||
|
>>> multi = Str('my_multi', multivalue=True)
|
||||||
|
>>> multi.convert([True, '', 17, None, False])
|
||||||
|
(u'True', u'17', u'False')
|
||||||
|
>>> multi.convert([None, u'']) is None # Filters to an empty list
|
||||||
|
True
|
||||||
|
|
||||||
|
Lastly, multivalue parameters will always return a tuple (well,
|
||||||
|
assuming they don't return None as in the last example above).
|
||||||
|
For example:
|
||||||
|
|
||||||
|
>>> multi.convert(42) # Called with a scalar value
|
||||||
|
(u'42',)
|
||||||
|
>>> multi.convert([True, False]) # Called with a list value
|
||||||
|
(u'True', u'False')
|
||||||
|
|
||||||
|
Note that how values are converted (and from what types they will be
|
||||||
|
converted) completely depends upon how a subclass implements its
|
||||||
|
`Param._convert_scalar()` method. For example, see
|
||||||
|
`Str._convert_scalar()`.
|
||||||
|
|
||||||
|
:param value: A proposed value for this parameter.
|
||||||
|
"""
|
||||||
if value in NULLS:
|
if value in NULLS:
|
||||||
return
|
return
|
||||||
if self.multivalue:
|
if self.multivalue:
|
||||||
if type(value) not in (tuple, list):
|
if type(value) not in (tuple, list):
|
||||||
value = (value,)
|
value = (value,)
|
||||||
values = filter(
|
values = tuple(
|
||||||
lambda val: val not in NULLS,
|
self._convert_scalar(v, i) for (i, v) in filter(
|
||||||
(self._convert_scalar(v, i) for (i, v) in enumerate(value))
|
lambda tup: tup[1] not in NULLS, enumerate(value)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
if len(values) == 0:
|
if len(values) == 0:
|
||||||
return
|
return
|
||||||
|
@ -297,7 +297,7 @@ class test_Str(ClassChecker):
|
|||||||
o = self.cls('my_str')
|
o = self.cls('my_str')
|
||||||
for value in (u'Hello', 42, 1.2, True):
|
for value in (u'Hello', 42, 1.2, True):
|
||||||
assert o._convert_scalar(value) == unicode(value)
|
assert o._convert_scalar(value) == unicode(value)
|
||||||
for value in ('Hello', None, [u'42', '42'], dict(hello=u'world')):
|
for value in ('Hello', (None,), [u'42', '42'], dict(hello=u'world')):
|
||||||
e = raises(TypeError, o._convert_scalar, value)
|
e = raises(TypeError, o._convert_scalar, value)
|
||||||
assert str(e) == \
|
assert str(e) == \
|
||||||
'Can only implicitly convert int, float, or bool; got %r' % value
|
'Can only implicitly convert int, float, or bool; got %r' % value
|
||||||
|
Loading…
Reference in New Issue
Block a user