Mark Leonard wrote:
> According to the docs:
> http://www.voidspace.org.uk/python/configobj.html#default-values
> "You can set a default value in your check. If the value is missing
> from the config file then this value will be used instead."
>
> I think I'm making an incorrect assumption about retrieving default
> values from the dict, as illustrated here:
>
> File test.spec containing only:
> key1 = integer(0, 30, default=15)
> key2 = integer(default=15)
>
> File test.conf containing only:
> key1 = 5
>
> >>> c = ConfigObj( 'test.conf', configspec='test.spec' )
> >>> c
> {'key1': '5'}
> >>> c.configspec
> {'key2': 'integer(default=15)', 'key1': 'integer(0, 30, default=15)'}
> >>> c['key1']
> '5'
> >>> c['key2']
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "C:\Python24\Lib\site-packages\configobj.py", line 337, in
> __getitem__
> val = dict.__getitem__(self, key)
> KeyError: 'key2'
>
> Shouldn't this return the default value, in this case 15? Is there a
> different method I should be using for retrieving values? I can't find
> anything in the documentation or via introspection. I specifically
> want the defaults specified in an external file and not in the code
> using something like c.get('key2',15).
>
Your logic is correct - but missing one step.
The default values are filled in by the validation process (as is the
type conversion to integers).
>>> from configobj import ConfigObj
>>> from validate import Validator
>>> c = ConfigObj( 'test.conf', configspec='test.spec' )
>>> c
{'key1': '5'}
>>> c.configspec
{'key2': 'integer(default=15)', 'key1': 'integer(0, 30, default=15)'}
>>> v = Validator()
>>> test = c.validate(v)
>>> if test != True:
... print 'Something went wrong ;-)'
>>> c['key2']
15
Untested, but should work. ;-)
Let me know if it doesn't.
> I'm using configobj version 4.2.0beta2
>
I changed a lot in the implementation of unicode from 4.2.0 beta 2 to
the released version, as well as other tinkerings. (See the homepage
http://www.voidspace.org.uk/python/configobj.html for a download link).
I would highly recommend using the latest version.
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
> Cheers,
> -Mark
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> that extends applications into web and mobile media. Attend the live
> webcast
> and join the prime developer group breaking into this new coding
> territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
|