|
From: Stefan P. <pa...@ik...> - 2011-01-03 14:41:32
|
On Mon, Jan 3, 2011 at 3:37 PM, Michael Foord <fuz...@vo...> wrote:
>> This would also solve problems with inconsistent naming (integer vs.
>> int_list types). The old types could be kept for backwards compatibility.
> This sounds like a good change.
I can take a stab at it myself if you are busy/not interested in doing
it yourself.
> Note that in issue 26 values will *already* have been converted to lists if
> they are in list format - so doing an *additional* split on ',' is
> incorrect. For example this single string does *not* represent a list:
>
> value = "something, with, commas"
Well, I think that string does represent a list :-) And so does
ConfigObj in some cases. Consider the following:
import configobj, validate
spec_str = """
a = list
b = integer
"""
conf_str = """
a = 1, 2
b = 0
"""
spec = configobj.ConfigObj(spec_str.split('\n'))
conf = configobj.ConfigObj(conf_str.split('\n'), configspec=spec)
conf.validate(validate.Validator())
print type(conf['a']), conf['a']
print type(conf['b']), conf['b']
conf['a'] = '1, 2'
conf['b'] = '0'
res = conf.validate(validate.Validator())
print type(conf['a']), conf['a']
print type(conf['b']), conf['b']
---
The above outputs:
<type 'list'> ['1', '2']
<type 'int'> 0
<type 'str'> 1, 2
<type 'int'> 0
So when parsing the config file "1,2" is indeed interpreted as a list,
but when "1,2" is assigned as a string it fails. Note that with the
integer type, it succeeds in both cases. I expect that assigning a
string with the same content as I would write in the config file would
result in the same values after validation. It does not, which is
confusing at least to me. I don't see a problem with splitting a
string when it is assigned to something expecting a list. This is
probably what the user wants anyway. Currently nothing sensible at all
happens (you end up with a string where you are expecting a list!).
There is the force_list type, which kind of solves the problem (in a
bad way IMHO), by converting the string to a list with a single item.
But why is a string properly converted to an int, while a string is
only converted to a list if force_list is used? Why is there no
force_int?
In summary: It is terribly confusing when I can't use the same syntax
when assigning as I can when writing config files.
--
Stefan Parviainen
|