Created
May 13, 2014 22:42
-
-
Save thepaul/3abfee2167b17df7b8dc to your computer and use it in GitHub Desktop.
# the ConfigParser.*ConfigParser family is just comically terrible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ConfigParser | |
| class SanerConfigParser(ConfigParser.SafeConfigParser): | |
| def __init__(self, defaults=None, dict_type=dict, allow_no_value=False): | |
| ConfigParser.SafeConfigParser.__init__(self, dict_type=dict_type, | |
| allow_no_value=allow_no_value) | |
| self.mydefaults = defaults or {} | |
| no_default = object() | |
| def get(self, section, item, default=no_default): # pylint: disable=W0221 | |
| try: | |
| return ConfigParser.SafeConfigParser.get(self, section, item) | |
| except (ConfigParser.NoSectionError, ConfigParser.NoOptionError), e: | |
| if default is self.no_default: | |
| try: | |
| default = self.mydefaults[section][item] | |
| except KeyError: | |
| raise e | |
| return default | |
| def getboolean(self, *a, **kw): | |
| return bool(self.get(*a, **kw)) | |
| def getint(self, *a, **kw): | |
| return int(self.get(*a, **kw)) | |
| def getfloat(self, *a, **kw): | |
| return float(self.get(*a, **kw)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment