[Patches] [ python-Patches-467580 ] ConfigParser.getboolean(): FALSE, TRUE.

noreply@sourceforge.net noreply@sourceforge.net
Fri, 05 Oct 2001 15:02:40 -0700


Patches item #467580, was opened at 2001-10-03 11:26
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=467580&group_id=5470

Category: Library (Lib)
Group: None
Status: Closed
Resolution: Accepted
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Guido van Rossum (gvanrossum)
Summary: ConfigParser.getboolean(): FALSE, TRUE.

Initial Comment:
This patch allows ConfigParser.getboolean() to interpret TRUE, FALSE, YES, NO, ON and OFF insteaf just '0' and '1'.

While just allowing '0' and '1' sounds more correct users ofetn demand to use more descriptive directives in configuration files. Instead of forcing every programmer do brew his own solution a system should include the batteries for this.

http://c0re.jp/c0de/misc/ConfigParser-bool.patch

This little patch allows Pythons ConfigParser.getboolean()
to interpret TRUE, FALSE, YES, NO, ON and OFF insteaf just
'0' and '1'.
  --drt@un.bewaff.net - http://c0re.jp/
--- ConfigParser.py     Wed Oct  3 16:31:43 2001
+++ ConfigParser.py     Wed Oct  3 16:33:18 2001
@@ -69,8 +69,9 @@
         like get(), but convert value to a float
 
     getboolean(section, options)
-        like get(), but convert value to a boolean (currently defined as 0 or
-        1, only)
+        like get(), but convert value to a boolean (currently case insensitive
+        defined as 0, FALSE, NO or OFF for 0 or 1, TRUE, YES, ON for 1).
+        Returns an int.
 
     remove_section(section)
         remove the given file section and all its options
@@ -314,11 +315,12 @@
         return self.__get(section, string.atof, option)
 
     def getboolean(self, section, option):
-        v = self.get(section, option)
-        val = int(v)
-        if val not in (0, 1):
+        states = {'1': 1, 'YES': 1, 'TRUE': 1, 'ON': 1,
+                  '0': 0, 'NO': 0, 'FALSE': 0, 'OFF': 0}
+        v = self.get(section, option)       
+        if not states.has_key(v.upper()):
             raise ValueError, 'Not a boolean: %s' % v
-        return val
+        return states[v.upper()]
 
     def optionxform(self, optionstr):
         return optionstr.lower()

----------------------------------------------------------------------

Comment By: Nobody/Anonymous (nobody)
Date: 2001-10-05 15:02

Message:
Logged In: NO 

The missing documentation - untested since i had no TeX at hand.
http://c0re.jp/c0de/misc/ConfigParser-bool-doc.patch

This patch adds Documentation to my ConfigParser-bool patch like 
GvR requested. See http://sourceforge.net/tracker/?func=detail&atid=305470&aid=467580&group_id=5470 
for further enlightenment.
  --drt@un.bewaff.net - http://c0re.jp/
? ConfigParser-bool-doc.patch
Index: Doc/lib/libcfgparser.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libcfgparser.tex,v
retrieving revision 1.18
diff -c -r1.18 libcfgparser.tex
*** Doc/lib/libcfgparser.tex	2001/10/01 17:04:10	1.18
--- Doc/lib/libcfgparser.tex	2001/10/05 21:46:51
***************
*** 163,171 ****
  
  \begin{methoddesc}{getboolean}{section, option}
  A convenience method which coerces the \var{option} in the specified
! \var{section} to a Boolean value.  Note that the only accepted values
! for the option are \samp{0} and \samp{1}, any others will raise
! \exception{ValueError}.
  \end{methoddesc}
  
  \begin{methoddesc}{set}{section, option, value}
--- 163,173 ----
  
  \begin{methoddesc}{getboolean}{section, option}
  A convenience method which coerces the \var{option} in the specified
! \var{section} to a Boolean value. \samp{0}, \samp{FALSE}, \samp{NO} 
! and \samp{OFF} will return \samp{0} while \samp{1}, \samp{TRUE},
! \samp{YES} and \samp{ON} will return \samp{1}, any others will raise
! \exception{ValueError}. The interpretation of the textual values is 
! case insensitive. 
  \end{methoddesc}
  
  \begin{methoddesc}{set}{section, option, value}


----------------------------------------------------------------------

Comment By: Nobody/Anonymous (nobody)
Date: 2001-10-05 06:40

Message:
Logged In: NO 

Name: Doobee R. Tzeck <drt@un.bewaff.net>
Docs/Tests: will follow shortly.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-04 12:59

Message:
Logged In: YES 
user_id=6380

Nice. Applied to CVS. Would you mind giving your name so we
can update the Misc/ACKS file? Also, would you mind
providing updates to the docs and to the test suite?

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=467580&group_id=5470