summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-12-18 01:18:17 -0700
committerJason Gerard DeRose <jderose@redhat.com>2008-12-18 01:18:17 -0700
commitac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9 (patch)
treebb9de1f2db81e90ae55d32db19d400c4ea3271a6
parentbf8154fa5017d12c7377175f85f60b670dc294f9 (diff)
downloadfreeipa-ac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9.tar.gz
freeipa-ac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9.tar.xz
freeipa-ac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9.zip
New Param: fixed small bug in Param.convert() and added detailed docstring
-rw-r--r--ipalib/parameter.py53
-rw-r--r--tests/test_ipalib/test_parameter.py2
2 files changed, 51 insertions, 4 deletions
diff --git a/ipalib/parameter.py b/ipalib/parameter.py
index aaca8d9d7..6a5695a77 100644
--- a/ipalib/parameter.py
+++ b/ipalib/parameter.py
@@ -328,14 +328,61 @@ class Param(ReadOnly):
return 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:
return
if self.multivalue:
if type(value) not in (tuple, list):
value = (value,)
- values = filter(
- lambda val: val not in NULLS,
- (self._convert_scalar(v, i) for (i, v) in enumerate(value))
+ values = tuple(
+ self._convert_scalar(v, i) for (i, v) in filter(
+ lambda tup: tup[1] not in NULLS, enumerate(value)
+ )
)
if len(values) == 0:
return
diff --git a/tests/test_ipalib/test_parameter.py b/tests/test_ipalib/test_parameter.py
index 7d9315831..5ee9e8caf 100644
--- a/tests/test_ipalib/test_parameter.py
+++ b/tests/test_ipalib/test_parameter.py
@@ -297,7 +297,7 @@ class test_Str(ClassChecker):
o = self.cls('my_str')
for value in (u'Hello', 42, 1.2, True):
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)
assert str(e) == \
'Can only implicitly convert int, float, or bool; got %r' % value