From 4a42e01f368a1939c8d7e9120806427cfd416152 Mon Sep 17 00:00:00 2001 From: Stefan Hacker Date: Mon, 25 Feb 2013 10:08:19 +0100 Subject: [PATCH] Add ability to check for config keys using 'in' --- config.py | 9 ++++----- config_test.py | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index 7ec04b3..126316d 100644 --- a/config.py +++ b/config.py @@ -87,11 +87,10 @@ class Config(object): self.__dict__[section].__dict__[name] = vdefault def __getitem__(self, key): - try: - return getattr(self, key) - except AttributeError, e: - raise KeyError(e) - + return self.__dict__.__getitem__(key) + + def __contains__(self, key): + return self.__dict__.__contains__(key) def x2bool(s): """ diff --git a/config_test.py b/config_test.py index 9cb3976..1a3d7b3 100644 --- a/config_test.py +++ b/config_test.py @@ -134,6 +134,7 @@ value = True def testGetItem(self): cfg = Config(default=self.cfg_default) assert(cfg["world"]["domination"] == False) + assert("world" in cfg) def invalidaccess(c): c["nointhisconfig"]