ANALOGS = {
'file': 'string',
+ 'path': 'string',
'point': 'int',
}
"""Types that may be treated as other types.
"""List of types that should not be converted.
"""
-def to_string(value, type):
+def to_string(value, type, count=1):
"""Convert `value` from `type` to a unicode string.
"""
type = ANALOGS.get(type, type)
if type in RAW_TYPES:
return value
+ if count != 1:
+ values = [to_string(v, type) for v in value]
+ return '[%s]' % ', '.join(values)
return unicode(value)
-def from_string(value, type):
+def from_string(value, type, count=1):
"""Convert `value` from a string to `type`.
"""
type = ANALOGS.get(type, type)
if type in RAW_TYPES:
return value
fn = globals()['_string_to_%s' % type]
+ if count != 1:
+ assert value.startswith('[') and value.endswith(']'), value
+ value = value[1:-1] # strip off brackets
+ values = [from_string(v, type) for v in value.split(', ')]
+ assert count == -1 or len(values) == count, (
+ 'array with %d != %d values: %s'
+ % (len(values), count, values))
+ return values
return fn(value)
def _string_to_string(value):