|
From: Michael F. <fuz...@vo...> - 2010-03-17 18:35:18
|
On 17/03/2010 15:42, Jason Baker wrote:
> I'm not really sure how best to phrase my example, so I'll illustrate
> it with code. :-)
>
> Suppose I have the following config file:
>
> [section]
> foo='bar'
> [[subsection]]
>
> Now suppose I do the following:
>
> configobj = <get configobj somehow>
> subsection = configobj['section']['subsection']
> subsection['foo']
>
> This throws a key error, which is most likely the expected behavior as
> foo isn't a key on subsection. However, I would like to have a way to
> get the value of foo as if subsection were like this:
>
> [[subsection]]
> foo = '$foo'
>
> That way I can fetch the value through the interpolation path rather
> than only checking the current section. I suppose I could do
> something like this:
>
> try:
> return subsection['foo']
> except KeyError, ke:
> subsection['foo'] = '$foo'
> return subsection['foo']
>
> But that feels a bit hacky. Are there any better ways to do this that
> I'm not thinking of, or am I just going to have to buck up and put the
> hack in?
How about setdefault?
>>> c = ConfigObj()
>>> c['bar'] = 'baz'
>>> c.setdefault('foo', '%(bar)s')
'baz'
Michael
>
> ------------------------------------------------------------------------------
> Download Intel® Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
>
>
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies ("BOGUS AGREEMENTS") that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.
|