|
From: Nicolas M. <nic...@le...> - 2011-08-30 06:21:41
|
So in my example, "config" is an instance of the ConfigObj object:
config = ConfigObj('PATH_TO_CONFIG_FILE')
# Then you can start a loop :
for key in config[main_key]:
is_a_subsection = isinstance(config[main_key][key],dict)
if is_a_subsection:
do something
So with your exemple, my "main_key" is the [header] and the key is your
[[loop]]
You can understand it like this :
- open python in interractive mode :
python
- import configobj :
from configobj import ConfigObj
- create your configobj instance :
config = ConfigObj('PATH_TO_CONFIG_FILE')
- then display the content of config :
config
Here is an exerpt of my config file :
ConfigObj({'opghl': {'enable': 'yes', 'acc_host': 'ghl-acc',
'acc_storage': '/home/fabric/postgres_repprod/', 'ghl': {'acc_db':
'ghl-sync'}}, ...
So if you reconstruct the tree with that list :
[opghl]
enable = yes
acc_host = ghl-acc
acc_storage = /home/fabric/postgres_repprod/
[[ghl]]
acc_db = ghl-sync
that's because opghl is a dictionnary with some keys/values. And one of
these keys ("ghl") is also a dictionnary.
So in my code isinstance test each keys into opghl (this is the
"main_key"). So it test :
- enable
- acc_host
- acc_storage
- ghl
If one is a dictionnary then it goes through my condition (isinstance
returns true).
On 08/29/2011 10:37 PM, Arash Azarmi wrote:
> Would you elaborate ? I dont get it.
>
> On Mon, Aug 29, 2011 at 1:23 PM, Nicolas Michel
> <nic...@le... <mailto:nic...@le...>> wrote:
>
> Hehe, not so easy! ;)
> This is a subsection only if the key is not only a value but a
> dictionary :
>
> is_a_subsection = isinstance(config[main_key][key],dict)
> if is_a_subsection:
> do ...
>
> On 08/29/2011 07:41 PM, Arash Azarmi wrote:
> > Hi all ,
> > I have 2 questions.
> >
> >
> > 1- I was wondering whats the procedure for reading the value for
> > subsection, sub-sub section and so on :
> >
> > config.cfg file :
> >
> > [Header]
> > User=value
> >
> > [[ loop]]
> > start= startPoint
> > step= stepPoint
> > stop= stopPoint
> >
> >
> >
> > Python Code for reading config file :
> >
> > config=configObj(path)
> > Header = config['Header']
> > User = Header['User']
> > loop = ??
> > step= ???
> >
> > 2 - how can I have multiple subsections with similar name and
> keywords
> > within one section ?
> > For example having multiple [[loop]] subsections like one is
> defined above .
> >
> > Thank you ,
> > Arash.
> >
> >
> >
> ------------------------------------------------------------------------------
> > EMC VNX: the world's simplest storage, starting under $10K
> > The only unified storage solution that offers unified management
> > Up to 160% more powerful than alternatives and 25% more efficient.
> > Guaranteed. http://p.sf.net/sfu/emc-vnx-dev2dev
> >
> >
> >
> > _______________________________________________
> > Configobj-develop mailing list
> > Con...@li...
> <mailto:Con...@li...>
> > https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
> ------------------------------------------------------------------------------
> Special Offer -- Download ArcSight Logger for FREE!
> Finally, a world-class log management solution at an even better
> price-free! And you'll get a free "Love Thy Logs" t-shirt when you
> download Logger. Secure your free ArcSight Logger TODAY!
> http://p.sf.net/sfu/arcsisghtdev2dev
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> <mailto:Con...@li...>
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
>
>
> ------------------------------------------------------------------------------
> Special Offer -- Download ArcSight Logger for FREE!
> Finally, a world-class log management solution at an even better
> price-free! And you'll get a free "Love Thy Logs" t-shirt when you
> download Logger. Secure your free ArcSight Logger TODAY!
> http://p.sf.net/sfu/arcsisghtdev2dev
>
>
>
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
|