[Python-bugs-list] [Bug #122977] ConfigParser.get needs a default arg

noreply@sourceforge.net noreply@sourceforge.net
Mon, 20 Nov 2000 07:54:21 -0800


Bug #122977, was updated on 2000-Nov-20 07:22
Here is a current snapshot of the bug.

Project: Python
Category: Library
Status: Closed
Resolution: None
Bug Group: Trash
Priority: 5
Summary: ConfigParser.get needs a default arg

Details: Seems to me that ConfigParser instances sort of act like dictionaries.  It's just that their keys are effectively (section, name) tuples.  Shouldn't their get methods have a default value?  That would make setting a series of options much simpler.  Instead of having to trap for NoSectionError and NoOptionError, the programmer could just indicate a default:

    cp = ConfigParser.ConfigParser()
    cp.read(somefile)
    self.spam = cp.get("food","spam",0,None,"fried")
    self.eggs = cp.get("food","eggs",0,None,"scrambled")

instead of 

    cp = ConfigParser.ConfigParser()
    cp.read(somefile)
    try:
        self.spam = cp.get("food","spam")
    except (NoSectionError,NoOptionError):
        self.spam = "fried"
    try:
        self.eggs = cp.get("food","eggs")
    except (NoSectionError,NoOptionError):
        self.eggs = "scrambled"

Here's a possible (but untested) patch.  Whether default values should be subject to interpolation is not obvious.

*** /tmp/ConfigParser.py.~1.23~	Mon Nov 20 09:18:14 2000
--- /tmp/ConfigParser.py	Mon Nov 20 09:18:14 2000
***************
*** 249,255 ****
                  filename = '<???>'
          self.__read(fp, filename)
  
!     def get(self, section, option, raw=0, vars=None):
          """Get an option value for a given section.
  
          All % interpolations are expanded in the return values, based on the
--- 249,255 ----
                  filename = '<???>'
          self.__read(fp, filename)
  
!     def get(self, section, option, raw=0, vars=None, default=None):
          """Get an option value for a given section.
  
          All % interpolations are expanded in the return values, based on the
***************
*** 266,272 ****
              if section == DEFAULTSECT:
                  sectdict = {}
              else:
!                 raise NoSectionError(section)
          d = self.__defaults.copy()
          d.update(sectdict)
          # Update with the entry specific variables
--- 266,272 ----
              if section == DEFAULTSECT:
                  sectdict = {}
              else:
!                 return default
          d = self.__defaults.copy()
          d.update(sectdict)
          # Update with the entry specific variables
***************
*** 276,282 ****
          try:
              rawval = d[option]
          except KeyError:
!             raise NoOptionError(option, section)
  
          if raw:
              return rawval
--- 276,282 ----
          try:
              rawval = d[option]
          except KeyError:
!             return default
  
          if raw:
              return rawval


Follow-Ups:

Date: 2000-Nov-20 07:54
By: montanaro

Comment:
I withdraw this feature request.  I realized that the proper way to set defaults is to perform a series of set() calls followed by a read() call.  The get() method then doesn't require any default arguments.

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

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=122977&group_id=5470