return float(value)
-class FloatListSetting (Setting):
- """A named setting with a list of floating point values.
+class ListSetting(Setting):
+ """A named setting with a list of string values.
- >>> s = FloatListSetting(name='floatlist')
+ >>> s = ListSetting(name='list')
>>> s.default
[]
- >>> s.convert_to_text([1, 2.3])
- '1, 2.3'
- >>> s.convert_from_text('4.5, -6.7') # doctest: +ELLIPSIS
- [4.5, -6.7...]
+ >>> s.convert_to_text(['abc', 'def'])
+ 'abc,def'
+ >>> s.convert_from_text('uvw, xyz')
+ ['uvw', ' xyz']
>>> s.convert_to_text([])
''
>>> s.convert_from_text('')
[]
"""
- def __init__(self, default=[], **kwargs):
- super(FloatListSetting, self).__init__(default=default, **kwargs)
+ def __init__(self, default=None, **kwargs):
+ if default is None:
+ default = []
+ super(ListSetting, self).__init__(default=default, **kwargs)
def _convert_from_text(self, value):
- if value is None:
- return value
- return float(value)
+ return value
def convert_from_text(self, value):
if value is None:
def convert_to_text(self, value):
if value is None:
return None
- return ', '.join([str(x) for x in value])
+ return ','.join([str(x) for x in value])
+
+class IntegerListSetting (ListSetting):
+ """A named setting with a list of integer point values.
+
+ >>> s = IntegerListSetting(name='integerlist')
+ >>> s.default
+ []
+ >>> s.convert_to_text([1, 3])
+ '1,3'
+ >>> s.convert_from_text('4, -6')
+ [4, -6]
+ >>> s.convert_to_text([])
+ ''
+ >>> s.convert_from_text('')
+ []
+ """
+ def _convert_from_text(self, value):
+ if value is None:
+ return value
+ return int(value)
+
+
+class FloatListSetting (ListSetting):
+ """A named setting with a list of floating point values.
+
+ >>> s = FloatListSetting(name='floatlist')
+ >>> s.default
+ []
+ >>> s.convert_to_text([1, 2.3])
+ '1,2.3'
+ >>> s.convert_from_text('4.5, -6.7') # doctest: +ELLIPSIS
+ [4.5, -6.7...]
+ >>> s.convert_to_text([])
+ ''
+ >>> s.convert_from_text('')
+ []
+ """
+ def _convert_from_text(self, value):
+ if value is None:
+ return value
+ return float(value)
class ConfigSetting (Setting):
>>> c['alive'] = True
>>> group = f.create_group('base')
>>> c.save(group=group)
- >>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF
+ >>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF, +ELLIPSIS
/
/base
<HDF5 dataset "age": shape (), type "<f8">
[ 5.4 3.2 1. ]
<HDF5 dataset "children": shape (), type "|S1">
<BLANKLINE>
- <HDF5 dataset "daisies": shape (), type "<i4">
+ <HDF5 dataset "claws": shape (2,), type "<i8">
+ [1 2]
+ <HDF5 dataset "daisies": shape (), type "<i...">
13
<HDF5 dataset "name": shape (), type "|S1">
<BLANKLINE>
Norwegian Blue
<HDF5 dataset "spouse": shape (), type "|S1">
<BLANKLINE>
+ <HDF5 dataset "words": shape (2,), type "|S7">
+ ['cracker' 'wants']
>>> c.clear()
>>> c['alive']
False
except Exception, e:
_LOG.error('Could not access {}/{}: {}'.format(
group.name, s.name, e))
- raise
+ raise
if isinstance(v, _numpy.ndarray):
if isinstance(s, _config.BooleanSetting):
v = bool(v) # array(True, dtype=bool) -> True
elif v.dtype.type == _numpy.string_:
- v = str(v) # array('abc', dtype='|S3') -> 'abc'
+ if isinstance(s, _config.ListSetting):
+ try:
+ v = list(v)
+ except TypeError:
+ v = []
+ else:
+ v = str(v) # array('abc', dtype='|S3') -> 'abc'
elif isinstance(s, _config.IntegerSetting):
v = int(v) # array(3, dtpe='int32') -> 3
elif isinstance(s, _config.FloatSetting):
v = float(v) # array(1.2, dtype='float64') -> 1.2
elif isinstance(s, _config.NumericSetting):
raise NotImplementedError(type(s))
- elif isinstance(s, _config.FloatListSetting):
+ elif isinstance(s, _config.ListSetting):
v = list(v) # convert from numpy array
if isinstance(v, _types.StringTypes):
# convert back from None, etc.
value = None
if isinstance(s, (_config.BooleanSetting,
_config.NumericSetting,
- _config.FloatListSetting)):
+ _config.ListSetting)):
value = config[s.name]
if value in [None, []]:
value = s.convert_to_text(value)
- 3.2
- 1
children: ''
+ claws:
+ - 1
+ - 2
daisies: 13
name: ''
species: Norwegian Blue
spouse: ''
+ words:
+ - cracker
+ - wants
<BLANKLINE>
Loading reads the config files from disk.
setting = settings[key]
if isinstance(setting, (_config.BooleanSetting,
_config.NumericSetting,
+ _config.ListSetting,
+ _config.IntegerListSetting,
_config.FloatListSetting)):
if isinstance(value, _types.StringTypes):
# older versions of h5config
setting = settings[key]
if isinstance(setting, (_config.BooleanSetting,
_config.NumericSetting,
+ _config.ListSetting,
+ _config.IntegerListSetting,
_config.FloatListSetting)):
v = value
elif isinstance(setting, _config.ConfigListSetting) and value:
>>> c['syslog'] = True
>>> c.save()
->>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF
+>>> pprint_HDF5(filename) # doctest: +REPORT_UDIFF, +ELLIPSIS
/
/base
<HDF5 dataset "age": shape (), type "<f8">
[ 5.4 3.2 1. ]
<HDF5 dataset "children": shape (), type "|S1">
<BLANKLINE>
- <HDF5 dataset "daisies": shape (), type "<i4">
+ <HDF5 dataset "claws": shape (2,), type "<i8">
+ [1 2]
+ <HDF5 dataset "daisies": shape (), type "<i...">
13
<HDF5 dataset "name": shape (), type "|S1">
<BLANKLINE>
Norwegian Blue
<HDF5 dataset "spouse": shape (), type "|S1">
<BLANKLINE>
+ <HDF5 dataset "words": shape (2,), type "|S7">
+ ['cracker' 'wants']
If you want more details, you can dump with help strings.
daisies: 13 (Number of daisies pushed up by the parrot.
Default: 13.)
age: 1.3 (Parrot age in years Default: 1.3.)
-bids: 5.4, 3.2, 1 (Prices offered for parrot. Default: 5.4, 3.2, 1.)
+words: cracker,wants (Words known by the parrot. Default: cracker,wants.)
+claws: 1,2 (Claws on each foot. Default: 1,2.)
+bids: 5.4,3.2,1 (Prices offered for parrot. Default: 5.4,3.2,1.)
spouse: (This parrot's significant other. Default: .)
children: (This parrot's children. Default: .)
name='age',
help='Parrot age in years',
default=1.3),
+ _config.ListSetting(
+ name='words',
+ help='Words known by the parrot.',
+ default=['cracker', 'wants']),
+ _config.IntegerListSetting(
+ name='claws',
+ help='Claws on each foot.',
+ default=[1, 2]),
_config.FloatListSetting(
name='bids',
help='Prices offered for parrot.',
'alive': True,
'daisies': None,
'age': None,
+ 'words': ['arrrr', 'matey'],
+ 'claws': [3, 0],
'bids': [],
'spouse': _alternative_test_config(name='Lory'),
'children': [_alternative_test_config(name=n)
c.save()
c.load()
nd = list(_non_defaults(c))
- assert not nd, nd
+ assert not nd, (storage, nd)
for key,value in _ALTERNATIVES.items():
c[key] = value
c.dump()
c.save()
na = dict(_non_alternatives(c))
- assert not na, na
+ assert not na, (storage, na)
c.clear()
nd = list(_non_defaults(c))
- assert not nd, nd
+ assert not nd, (storage, nd)
c.load()
na = dict(_non_alternatives(c))
- assert not na, na
+ assert not na, (storage, na)
finally:
_os.remove(filename)