From 6762e5c7b557c6c39eb8ff8ff436115400594fb2 Mon Sep 17 00:00:00 2001 From: Christopher Amin Date: Fri, 12 Feb 2016 12:09:07 +0100 Subject: [PATCH] Add __nonzero__ method to ParsingDict so that keys() isn't called if you evaluate its truthiness --- ripe/atlas/sagan/base.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ripe/atlas/sagan/base.py b/ripe/atlas/sagan/base.py index 604cd83..cb141ef 100644 --- a/ripe/atlas/sagan/base.py +++ b/ripe/atlas/sagan/base.py @@ -89,6 +89,11 @@ def __init__(self, **kwargs): self._on_malformation = kwargs.pop("on_malformation", self.ACTION_WARN) self.is_malformed = False + def __nonzero__(self): + # If we don't define this, Python ends up calling keys() + # via __len__() whenever we evaluate the object as a bool. + return True + def __len__(self): return len(self.keys())