diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-12-18 01:18:17 -0700 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-12-18 01:18:17 -0700 |
commit | ac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9 (patch) | |
tree | bb9de1f2db81e90ae55d32db19d400c4ea3271a6 /ipalib/parameter.py | |
parent | bf8154fa5017d12c7377175f85f60b670dc294f9 (diff) | |
download | freeipa.git-ac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9.tar.gz freeipa.git-ac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9.tar.xz freeipa.git-ac335bc7ea5ca290a25b9ac27e17f1b68bd8b4a9.zip |
New Param: fixed small bug in Param.convert() and added detailed docstring
Diffstat (limited to 'ipalib/parameter.py')
-rw-r--r-- | ipalib/parameter.py | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/ipalib/parameter.py b/ipalib/parameter.py index aaca8d9d..6a5695a7 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 |