From: Michael F. <mi...@pc...> - 2005-11-08 15:43:19
|
Hello Brendan, brendan simons wrote: > Hi all, > > I'm just sitting down to write a new project, and I > was considering using ConfigObj for my application. > However I'm not sure how one would express the > structure of my data in your file format. ConfigObj is designed for mapping names to values (like a config file - and also like a dictionary). We also store the order of keys - so effectively a section in a ConfigObj is a list of values. Each value *can* be a sub-section - so every section can be a list of sections. You can have individual values as lists - but not lists of dictionaries. Everywhere you need a dictionary you should make it a section. > To > illustrate, my data looks like something like this (in > python): > > #-----------------begin example--------------------- > param1 = 1 > param2 = 2 > #etc > > #the following is an arbitrarily long list > models = [ > Model( > mParam1 = 4, > mParam2 = 5, > #etc > > #the following is another arbitrarily long > list > mListParams = [ > mlParam1 = ModelInner( > miParam1 = 6, > miParam2 = 7 > ), > mlParam2 = ModelInner( > miParam1 = 7, > miParam2 = 8 > ) > ] > ) > > Model( > mParam1 = 4, > mParam2 = 5, > #etc > > #the following is another arbitrarily long > list > mListParams = [ > mlParam1 = ModelInner( > miParam1 = 6, > miParam2 = 7 > ), > mlParam2 = ModelInner( > miParam1 = 7, > miParam2 = 8 > ) > ] # end of mListParams > ) #end of model > ] #end of models list > > #----------------end example-------------------------- > > > Reading the ConfigObj docs, I can see how to make > nested structure. But the parser seems heavily geared > towards nested dictionaries. I'm not sure how to > describe the nested lists. I could the lists into > dicts before saving and after opening by giving them > keys like "value1", "value2" etc, but I'd rather use a > standard approach. Does ConfigObj have the facility > to do this automatically? > You could turn your lists into dictionaries before putting them into a ConfigObj section. You would lose order doing this. If you put them straight into the ConfigObj it will retain the order you do it in. Below is my approximation of your example data structure, as a ConfigObj file. #-----------------begin example--------------------- param1 = 1 param2 = 2 #etc # the following is an arbitrarily long list # it needs to be a section [models] # each model is itself a subsection [[Model 1]] mParam1 = 4 mParam2 = 5 #etc #the following is another arbitrarily long list # so it also needs to be a sub-section [[[mListParams]]] # if these keys need to be named # they also need to be a sub-section # they could just be a list "mlParam1 = 6, 7" [[[[mlParam1]]]] miParam1 = 6 miParam2 = 7 [[[[mlParam2]]]] miParam1 = 7 miParam2 = 8 [[Model2]] mParam1 = 4 mParam2 = 5 #etc #the following is another arbitrarily long list # so it also needs to be a sub-section [[[mListParams]]] # if these keys need to be named # they also need to be a sub-section # they could just be a list "mlParam1 = 6, 7" [[[[mlParam1]]]] miParam1 = 6 miParam2 = 7 [[[[mlParam2]]]] miParam1 = 7 miParam2 = 8 > #----------------end example-------------------------- You will need to write a simple function to convert to and from the ConfigObj instance to your data structure. You might find it easy to do that using validate. You can write a configspec which specifies the 'type' of each member - and it will do the conversion when you validate. (As per Nicola's email) Alternatively you could look at ConfigPersist.py - it contains examples of using ConfigObj for data persistence. http://www.voidspace.org.uk/python/articles/configobj_for_data_persistence.shtml All the best, Fuzzyman http://www.voidspace.org.uk/python > Thanks in advance, > Brendan > > > > > > > __________________________________________________________ > Find your next car at http://autos.yahoo.ca > > > ------------------------------------------------------- > SF.Net email is sponsored by: > Tame your development challenges with Apache's Geronimo App Server. Download > it for free - -and be entered to win a 42" plasma tv or your very own > Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php > _______________________________________________ > Configobj-develop mailing list > Con...@li... > https://lists.sourceforge.net/lists/listinfo/configobj-develop > > > |