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

noreply@sourceforge.net noreply@sourceforge.net
Thu, 04 Oct 2001 12:59:35 -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: 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