3 """Type conversion utilities.
6 CONVERT_FROM_STRING = {
8 'bool': lambda x: x == 'True',
9 'int': lambda x: int(x),
10 'float': lambda x: float(x),
12 """Functions converting strings to values, keyed by type.
20 """Types that may be treated as other types.
22 These types may have optional special handling on the UI end
23 (e.g. file picker dialogs), but it is not required.
34 """List of types that should not be converted.
37 def to_string(value, type, count=1):
38 """Convert `value` from `type` to a unicode string.
40 type = ANALOGS.get(type, type)
44 values = [to_string(v, type) for v in value]
45 return '[%s]' % ', '.join(values)
48 def from_string(value, type, count=1):
49 """Convert `value` from a string to `type`.
51 type = ANALOGS.get(type, type)
54 fn = globals()['_string_to_%s' % type]
56 assert value.startswith('[') and value.endswith(']'), value
57 value = value[1:-1] # strip off brackets
58 values = [from_string(v, type) for v in value.split(', ')]
59 assert count == -1 or len(values) == count, (
60 'array with %d != %d values: %s'
61 % (len(values), count, values))
65 def _string_to_string(value):
68 def _string_to_bool(value):
69 return value == 'True'
71 def _string_to_int(value):
74 def _string_to_float(value):