From 852549c44cde1006cc7cd27562b3fd7a9e4e24f7 Mon Sep 17 00:00:00 2001 From: Stefan Hacker Date: Fri, 31 Dec 2010 15:04:34 +0100 Subject: [PATCH] Update config module to provide dict like access --- config.py | 7 +++++++ config_test.py | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/config.py b/config.py index bbd6d30..7ec04b3 100644 --- a/config.py +++ b/config.py @@ -85,6 +85,13 @@ class Config(object): self.__dict__[section].__dict__[name] = conv(cfg.get(section, name)) except (ValueError, ConfigParser.NoSectionError, ConfigParser.NoOptionError): self.__dict__[section].__dict__[name] = vdefault + + def __getitem__(self, key): + try: + return getattr(self, key) + except AttributeError, e: + raise KeyError(e) + def x2bool(s): """ diff --git a/config_test.py b/config_test.py index 9f7eff3..9cb3976 100644 --- a/config_test.py +++ b/config_test.py @@ -130,6 +130,15 @@ value = True assert(cfg.world.domination == False) assert(cfg.somethingelse.bla == "test") assert(cfg.world.somenum == 0) + + def testGetItem(self): + cfg = Config(default=self.cfg_default) + assert(cfg["world"]["domination"] == False) + + def invalidaccess(c): + c["nointhisconfig"] + + self.assertRaises(KeyError, invalidaccess, cfg) if __name__ == "__main__":