Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > (beginner question) ConfigParser nuances

Reply
Thread Tools

(beginner question) ConfigParser nuances

 
 
Unknown Moss
Guest
Posts: n/a
 
      05-02-2011
Hi - Beginner question here. I'm working with ConfigParser. I'd like
to take a multiline variable and convert it directly to an array.
Seems like a common problem, but I don't see how I can do it without
doing a little parsing in my own code. Here's what I'm doing ...

>>> import ConfigParser
>>> import io
>>> sample = """

.... [Example]
.... fruit = apple
.... orange
.... pear
.... """
>>> config = ConfigParser.RawConfigParser()
>>> config.readfp(io.BytesIO(sample))
>>> config.get("Example", "fruit")

'apple\norange\npear'
>>> temp = config.get("Example", "fruit")
>>> temp.split()

['apple', 'orange', 'pear']

I'm thinking there's a way to avoid this intermediate temp.split()
step. Is there not a way to move a multiline value straight into an
array using ConfigParser?

Thanks for the help.
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      05-02-2011
On Mon, May 2, 2011 at 3:04 PM, Unknown Moss <> wrote:
> Hi - Beginner question here. I'm working with ConfigParser. I'd like
> to take a multiline variable and convert it directly to an array.
> Seems like a common Â*problem, but I don't see how I can do it without
> doing a little parsing in my own code. Here's what I'm doing ...
>
>>>> import ConfigParser
>>>> import io
>>>> sample = """

> ... [Example]
> ... fruit = apple
> ... Â* Â* orange
> ... Â* Â* pear
> ... """
>>>> config = ConfigParser.RawConfigParser()
>>>> config.readfp(io.BytesIO(sample))
>>>> config.get("Example", "fruit")

> 'apple\norange\npear'
>>>> temp = config.get("Example", "fruit")
>>>> temp.split()

> ['apple', 'orange', 'pear']
>
> I'm thinking there's a way to avoid this intermediate temp.split()
> step. Is there not a way to move a multiline value straight into an
> array using ConfigParser?


Nope, there is not. I think some might instead use several numbered
options to similar effect:

# config file
[Example]
fruit1: apple
fruit2: orange
fruit3: pear

# Python
from itertools import count
fruits = []
names = ("fruit" + str(i) for i in count(1))
for name in names:
if not config.has_option("Example", name):
break
fruits.append(config.get("Example", name))


Cheers,
Chris
--
http://rebertia.com
 
Reply With Quote
 
 
 
 
Unknown Moss
Guest
Posts: n/a
 
      05-03-2011
On May 2, 3:25*pm, Chris Rebert <c...@rebertia.com> wrote:
> On Mon, May 2, 2011 at 3:04 PM, Unknown Moss <unknownm...@gmail.com> wrote:
> > Hi -Beginnerquestionhere. I'm working with ConfigParser. I'd like
> > to take a multiline variable and convert it directly to an array.
> > Seems like a common *problem, but I don't see how I can do it without
> > doing a little parsing in my own code. Here's what I'm doing ...

>
> >>>> import ConfigParser
> >>>> import io
> >>>> sample = """

> > ... [Example]
> > ... fruit = apple
> > ... * * orange
> > ... * * pear
> > ... """
> >>>> config = ConfigParser.RawConfigParser()
> >>>> config.readfp(io.BytesIO(sample))
> >>>> config.get("Example", "fruit")

> > 'apple\norange\npear'
> >>>> temp = config.get("Example", "fruit")
> >>>> temp.split()

> > ['apple', 'orange', 'pear']

>
> > I'm thinking there's a way to avoid this intermediate temp.split()
> > step. Is there not a way to move a multiline value straight into an
> > array using ConfigParser?

>
> Nope, there is not. I think some might instead use several numbered
> options to similar effect:
>
> # config file
> [Example]
> fruit1: apple
> fruit2: orange
> fruit3: pear
>
> # Python
> from itertools import count
> fruits = []
> names = ("fruit" + str(i) for i in count(1))
> for name in names:
> * * if not config.has_option("Example", name):
> * * * * break
> * * fruits.append(config.get("Example", name))
>
> Cheers,
> Chris
> --http://rebertia.com


Ok, thanks Chris. Maybe I'm getting too lazy in my old age.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nuances of alias_method Richard Leber Ruby 1 04-12-2010 05:41 PM
ConfigParser: Can I read(ConfigParser.get()) a configuration file anduse it to call a funciton? jamitwidme@gmail.com Python 3 06-26-2008 08:01 PM
Please explain nuances of ||= Ruby Freak Ruby 26 05-02-2008 07:44 PM
ConfigParser: buggy or just a mess? Martin Maney Python 1 10-24-2003 08:58 PM
Subclassing ConfigParser Greg Krohn Python 2 08-24-2003 01:58 AM



Advertisments