Yea, the config parser probably isn't the best choice...but if you really want to, try this:
import unittest
from ConfigParser import SafeConfigParser
from cStringIO import StringIO
def _parse_float_list(string_value):
return [float(v.strip()) for v in string_value.split(',')]
def _generate_float_list(float_values):
return ','.join(str(value) for value in float_values)
def get_float_list(parser, section, option):
string_value = parser.get(section, option)
return _parse_float_list(string_value)
def set_float_list(parser, section, option, float_values):
string_value = _generate_float_list(float_values)
parser.set(section, option, string_value)
class TestConfigParser(unittest.TestCase):
def setUp(self):
self.a = [5e6,6e6,7e6,8e6,8.5e6,9e6,9.5e6,10e6,11e6,12e6]
self.p = [0.0,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.01,0.015,0.05,0.1,0.15,0.2]
def testRead(self):
parser = SafeConfigParser()
f = StringIO('''[values]
a: 5e6, 6e6, 7e6, 8e6,
8.5e6, 9e6, 9.5e6, 10e6,
11e6, 12e6
p: 0.0 , 0.001, 0.002,
0.003, 0.004, 0.005,
0.006, 0.007, 0.008,
0.009, 0.01 , 0.015,
0.05 , 0.1 , 0.15 ,
0.2
''')
parser.readfp(f)
self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))
def testRoundTrip(self):
parser = SafeConfigParser()
parser.add_section('values')
set_float_list(parser, 'values', 'a', self.a)
set_float_list(parser, 'values', 'p', self.p)
self.assertEquals(self.a, get_float_list(parser, 'values', 'a'))
self.assertEquals(self.p, get_float_list(parser, 'values', 'p'))
if __name__ == '__main__':
unittest.main()