Skip to content

Instantly share code, notes, and snippets.

@grigory51
Last active April 10, 2018 00:33
Show Gist options
  • Save grigory51/3a91f7f1507cbd5f993a to your computer and use it in GitHub Desktop.
Save grigory51/3a91f7f1507cbd5f993a to your computer and use it in GitHub Desktop.
Safe RawConfigParser with default value
from ConfigParser import RawConfigParser
class SafeConfigParser(object, RawConfigParser):
def __init__(self, **kwargs):
RawConfigParser.__init__(self, **kwargs)
def get(self, section, option, default=None):
try:
return super(SafeConfigParser, self).get(section, option)
except Exception:
return default
def getint(self, section, option, default=None):
try:
return super(SafeConfigParser, self).getint(section, option)
except Exception:
return default
def getfloat(self, section, option, default=None):
try:
return super(SafeConfigParser, self).getfloat(section, option)
except Exception:
return default
def getboolean(self, section, option, default=None):
try:
return super(SafeConfigParser, self).getboolean(section, option)
except Exception as e:
return default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment