|
From: Michael F. <fuz...@vo...> - 2010-07-28 14:48:32
|
On 26/07/2010 13:52, tin...@li... wrote:
> Hallo,
> I was suggest to use this module to handle configuration values that can be
> modify via GUI from the user.
> I find the program powerful and quite easy to understan.
> However I'm stuck trying to write the validation file programatically.
> Here is my code; apparently if I write a file with the fileobject function f.
> write, I have some error and all values are read as strings.I suspect a problem
> with the encoding, as writing the same file directly works all right (it is
> the example provided in the tutorial).
> if you could give me hints how to write the file prgorammatically it would be
> great.
>
> config = ConfigObj()
> config.filename = 'config.ini'
> config['name'] = 'Fred Smith'
> config['Favourites'] = {}
> config['Favourites']['color'] = 'Mostly Red'
> config['Favourites']['software'] = {'ide': 'Emacs'}
> config.write()
>
> valstr = '''name = string(min=1, max=30, default=Fred)
> age = float(min=0, max=200, default=29)
> attributes = string_list(min=5, max=5, default=list('arms', 'legs', 'head',
> 'body', 'others'))
> likes_cheese = boolean(default=True)
> favourite_color = option('red', 'green', 'blue', default="red")
> '''
>
> f = open ('configspec.ini','w')
> f.write(valstr)
> f.close
>
This should be f.close() - when I change this I get the following
results from your code (which I think is what you were expecting):
else
True
Fred Smith
<type 'float'>
['arms', 'legs', 'head', 'body', 'others']
All the best,
Michael
> config = ConfigObj('config.ini', configspec='configspec.ini')
>
> validator = Validator()
> result = config.validate(validator)
>
> if result != True:
> print 'Config file validation failed!'
> else:
> print 'else'
>
>
> #config = ConfigObj('config.ini', configspec='configspec.ini')
> validator = Validator()
> a=config.validate(validator)
> print a
> print config['name']
> print type(config['age'])
> print config['attributes']
>
> ------------------------------------------------------------------------------
> The Palm PDK Hot Apps Program offers developers who use the
> Plug-In Development Kit to bring their C/C++ apps to Palm for a share
> of $1 Million in cash or HP Products. Visit us here for more details:
> http://ad.doubleclick.net/clk;226879339;13503038;l?
> http://clk.atdmt.com/CRS/go/247765532/direct/01/
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
--
http://www.ironpythoninaction.com/
|