Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ca041f

Browse files
committed
items(): New method, provided by Gustavo Niemeyer in SF bug #545096.
1 parent 309db06 commit 2ca041f

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Doc/lib/libcfgparser.tex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ \subsection{ConfigParser Objects \label{ConfigParser-objects}}
189189
cause it to raise \exception{ValueError}.
190190
\end{methoddesc}
191191

192+
\begin{methoddesc}{items}{section\optional{, raw\optional{, vars}}}
193+
Create a generator which will return a tuple \code{(name, value)} for
194+
each option in the given \var{section}. Optional arguments have the
195+
same meaning as for the \code{get()} method.
196+
\versionadded{2.3}
197+
\end{methoddesc}
198+
192199
\begin{methoddesc}{set}{section, option, value}
193200
If the given section exists, set the given option to the specified value;
194201
otherwise raise \exception{NoSectionError}.

Lib/ConfigParser.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
insensitively defined as 0, false, no, off for 0, and 1, true,
7171
yes, on for 1). Returns 0 or 1.
7272
73+
items(section, raw=0, vars=None)
74+
return a list of tuples with (name, value) for each option
75+
in the section.
76+
7377
remove_section(section)
7478
remove the given file section and all its options
7579
@@ -278,6 +282,35 @@ def get(self, section, option, raw=0, vars=None):
278282
return value
279283
return self._interpolate(section, option, value, d)
280284

285+
def items(self, section, raw=0, vars=None):
286+
"""Return a list of tuples with (name, value) for each option
287+
in the section.
288+
289+
All % interpolations are expanded in the return values, based on the
290+
defaults passed into the constructor, unless the optional argument
291+
`raw' is true. Additional substitutions may be provided using the
292+
`vars' argument, which must be a dictionary whose contents overrides
293+
any pre-existing defaults.
294+
295+
The section DEFAULT is special.
296+
"""
297+
d = self.__defaults.copy()
298+
try:
299+
d.update(self.__sections[section])
300+
except KeyError:
301+
if section != DEFAULTSECT:
302+
raise NoSectionError(section)
303+
# Update with the entry specific variables
304+
if vars:
305+
d.update(vars)
306+
if raw:
307+
for option in self.options(section):
308+
yield (option, d[option])
309+
else:
310+
for option in self.options(section):
311+
yield (option,
312+
self._interpolate(section, option, d[option], d))
313+
281314
def _interpolate(self, section, option, rawval, vars):
282315
# do the string interpolation
283316
value = rawval

0 commit comments

Comments
 (0)