[issue20943] configparser - add 'getDict' function

2014-03-16 Thread Łukasz Langa

Łukasz Langa added the comment:

Michael, what you wish is already a part of configparser. Let's use this as an 
example:

>>> p = configparser.ConfigParser()
>>> p.read_string("""
... [one]
... opt=val
... [two]
... num=1
... str=bzz
... bool=true
... """)

When asking for a specific section, you get a SectionProxy object. It has a 
dict-like API but is not a dict:

>>> p['two']

>>> isinstance(p['two'], dict)
False

The reason for that is two-fold: firstly this enables us to make changes made 
on a section object persistent within the main parser. Secondly, this enables 
us to add configparsser-specific functionality (like dynamic interpolation, 
type conversions, etc.):

>>> p['two']['num']
'1'
>>> p['two'].getint('num')
1
>>> p['two']['str'] = 'bool is %(bool)s'
>>> p['two']['str']
'bool is true'
>>> p['two']['bool'] = 'false'
>>> p['two']['str']
'bool is false'

Because the section proxy object follows the dict API very closely, it's 
trivial to convert a section to a dict:

>>> dict(p['two'])
{'str': 'bool is false', 'bool': 'false', 'num': '1'}

Note, however, that this sets in stone interpolations and doesn't provide 
built-in type conversions.

--
assignee:  -> lukasz.langa
resolution:  -> works for me
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20943] configparser - add 'getDict' function

2014-03-16 Thread R. David Murray

R. David Murray added the comment:

How is this different from myconfig['section']?

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20943] configparser - add 'getDict' function

2014-03-16 Thread Michael Müller

New submission from Michael Müller:

It would be nice to have a 'getDict' function in the configparser lib.

Adding that function would be simply

def getDict(self, *args):
return dict([entry for entry in self.items(*args)])

inside the RawConfigParser class next to the other get* functions.

--
components: Library (Lib)
messages: 213721
nosy: Michael.Müller, lukasz.langa
priority: normal
severity: normal
status: open
title: configparser - add 'getDict' function
type: enhancement
versions: Python 3.3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com