333: Param.convert() now uses name Param.__multivalue() helper method as Param.normalize()

This commit is contained in:
Jason Gerard DeRose 2008-09-24 06:35:19 +00:00
parent 6bedb15674
commit 1125d420bd
2 changed files with 9 additions and 12 deletions

View File

@ -105,16 +105,18 @@ class Param(plugable.ReadOnly):
self.rules = (type_.validate,) + rules
lock(self)
def __if_multivalue(self, value, scalar):
def __multivalue(self, value, scalar):
if self.multivalue:
if type(value) in (tuple, list):
if len(value) == 0:
return None
return tuple(scalar(v) for v in value)
return (scalar(value),) # tuple
return tuple(
scalar(v, i) for (i, v) in enumerate(value)
)
return (scalar(value, 0),) # tuple
return scalar(value)
def __normalize_scalar(self, value):
def __normalize_scalar(self, value, index=None):
if not isinstance(value, basestring):
return value
try:
@ -140,7 +142,7 @@ class Param(plugable.ReadOnly):
"""
if self.__normalize is None:
return value
return self.__if_multivalue(value, self.__normalize_scalar)
return self.__multivalue(value, self.__normalize_scalar)
def __convert_scalar(self, value, index=None):
if value is None:
@ -153,13 +155,7 @@ class Param(plugable.ReadOnly):
return converted
def convert(self, value):
if self.multivalue:
if type(value) in (tuple, list):
return tuple(
self.__convert_scalar(v, i) for (i, v) in enumerate(value)
)
return (self.__convert_scalar(value, 0),) # tuple
return self.__convert_scalar(value)
return self.__multivalue(value, self.__convert_scalar)

View File

@ -163,6 +163,7 @@ class test_Param(ClassChecker):
# Scenario 2: multivalue=True
o = self.cls(name, type_, multivalue=True)
assert o.convert([]) is None
for none in [None, (7, None)]:
e = raises(TypeError, o.convert, none)
assert str(e) == 'value cannot be None'