|
From: Jeffrey B. <jef...@ea...> - 2008-09-21 20:23:00
|
On Sunday 21 September 2008 14:01:48 Michael Foord wrote: > Jeffrey Barish wrote: > > On Sunday 21 September 2008 13:25:27 Michael Foord wrote: > >> Jeffrey Barish wrote: > >>> On Sunday 21 September 2008 11:30:36 Michael Foord wrote: > >>>> Jeffrey Barish wrote: > >>>>> On Sunday 21 September 2008 04:17:50 Michael Foord wrote: > >>>>>> Jeffrey Barish wrote: > >>>>>>> On Saturday 20 September 2008 18:55:17 Jeffrey Barish wrote: > >>>>>>>> Configobj was working fine until I switched to using unrepr mode. > >>>>>>>> Now it works fine on one platform, but on another the same code > >>>>>>>> produces the following error: > >>>>>>>> > >>>>>>>> ... > >>>>>>>> File "/usr/lib/python2.5/site-packages/configobj.py", line 1272, > >>>>>>>> in __init__ self._load(infile, configspec) > >>>>>>>> File "/usr/lib/python2.5/site-packages/configobj.py", line 1355, > >>>>>>>> in _load raise error > >>>>>>>> configobj.ConfigObjError: Parsing failed with several errors. > >>>>>>>> First error at line 3. > >>>>>>>> > >>>>>>>> Line 3 is: > >>>>>>>> > >>>>>>>> v = 0.77304964539007093 > >>>>>>>> > >>>>>>>> I am using version 4.5.3 on python 2.5.2. Considering that the > >>>>>>>> same rc file works fine on the other platform, I am at a loss as > >>>>>>>> to what to do. Any suggestions? > >>>>>>> > >>>>>>> The problem appears to arise because there is no compiler module on > >>>>>>> this platform (Nokia N800). > >>>>>>> > >>>>>>> Do I have any options other than returning to code like > >>>>>>> > >>>>>>> if x == 'True' > >>>>>> > >>>>>> unrepr mode relies on the compiler module - so it won't work on a > >>>>>> platform that doesn't provide it. > >>>>>> > >>>>>> You could use validate and a configspec to convert types instead. > >>>>>> > >>>>>> > >>>>>> Michael > >>>>> > >>>>> OK. I learned how to use validate. It works nicely for converting > >>>>> types and I am also doing range checking, so it's a better solution > >>>>> anyway. > >>>>> > >>>>> One problem, though. In my configuration file I have a section with > >>>>> colors, but I don't know a priori what colors it will contain. For > >>>>> example: > >>>>> > >>>>> [colors] > >>>>> color1 = [143, 188, 143] > >>>>> color2 = [70, 130, 180] > >>>>> > >>>>> and so on. Since I don't know what the keys are going to be, I > >>>>> presume that there is no way for me to validate the entries. Is that > >>>>> true? It would be nice to be able to test that values are between 0 > >>>>> and 255 and to convert the strings to integers and the brackets to a > >>>>> list. Currently I am doing > >>>>> > >>>>> color1 = 143, 188, 143 > >>>>> > >>>>> and not validating. > >>>> > >>>> You're correct that ConfigObj doesn't support validating arbitrary > >>>> entries in a section. It is a worthy feature request though (perhaps > >>>> "* = check()"). > >>>> > >>>> I'll add it to my list - but as I am *still* finishing a book it will > >>>> be quicker if someone else implements it. It will need to be in the > >>>> ConfigObj.validate method rather than in the validate module. > >>>> > >>>> Michael > >>> > >>> Here's a worse problem: after using config.reload(), all values revert > >>> to strings. I have to revalidate after every reload to get values back > >>> into their correct types. > >> > >> Hmmm... that sounds like correct behaviour to me. Validation is an > >> explicit step. > >> > >> Michael > > > > You make a good point. But consider: If I provided a configspec when I > > created the ConfigObj and I then validated the ConfigObj, would it ever > > happen that I would not want the ConfigObj validated again when I call > > reload? If someone really wanted the values to revert to strings, it > > seems to me that he would create a new ConfigObj (without a configspec) > > rather than call reload. I see reload as a shortcut for refreshing the > > values using the same procedure I used initially to create them. > > On the other hand, validation requires a validator - and having reload > do an implicit validate would require us to keep a reference to the > validator used the first time. This may or may not keep a lot of memory > in use unnecessarily or even potentially cause us to reuse an object not > in a valid state. > > I see reload as returning the ConfigObj instance to the state > immediately after loading - which would be before validation. Is calling > validate again really such a big burden? > > > Michael It's not a problem. Thanks for explaining the issues. -- Jeffrey Barish |