From c61743a4b9dbc766ef3befdd31d27d6b17338edc Mon Sep 17 00:00:00 2001 From: Apalala Date: Fri, 8 May 2015 18:51:17 -0430 Subject: [PATCH] Implemented the Python attribute protocol. Now these work: hasattribute(element, 'aname') getattribute(element, 'aname', []) --- untangle.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/untangle.py b/untangle.py index 54dee14..64269f0 100755 --- a/untangle.py +++ b/untangle.py @@ -80,7 +80,14 @@ def __getattr__(self, key): self.__dict__[key] = matching_children return matching_children else: - raise IndexError('Unknown key <%s>' % key) + raise AttributeError( + "'%s' has no attribute '%s'" % (self._name, key) + ) + + def __hasattribute__(self, name): + if name in self.__dict__: + return True + return any(self.children, lambda x: x._name == name) def __iter__(self): yield self