[tor-commits] [doctor/master] Taking advantage of lru_cache
atagar at torproject.org
atagar at torproject.org
Mon Oct 7 00:42:10 UTC 2013
commit 59339e8c5ad51209a8ef6317c581a103e1f32403
Author: Damian Johnson <atagar at torproject.org>
Date: Sun Oct 6 17:31:11 2013 -0700
Taking advantage of lru_cache
Making life a little nicer by using stem's new caching annotation rather than
doing it by hand every damn time.
---
consensus_health_checker.py | 43 ++++++++++++++++++-------------------------
1 file changed, 18 insertions(+), 25 deletions(-)
diff --git a/consensus_health_checker.py b/consensus_health_checker.py
index 7c48fb1..734c5ea 100755
--- a/consensus_health_checker.py
+++ b/consensus_health_checker.py
@@ -18,6 +18,7 @@ import stem.util.conf
import stem.util.enum
from stem import Flag
+from stem.util.lru_cache import lru_cache
Runlevel = stem.util.enum.UppercaseEnum("NOTICE", "WARNING", "ERROR")
@@ -51,9 +52,7 @@ class Issue(object):
self._template = template
self._attr = attr
- self._msg = None
- self._suppression_duration = None
-
+ @lru_cache()
def get_message(self):
"""
Provides the description of the problem.
@@ -61,18 +60,15 @@ class Issue(object):
:returns: **str** with a description of the issue
"""
- if self._msg is None:
- if self._template in CONFIG['msg']:
- try:
- self._msg = CONFIG['msg'][self._template].format(**self._attr)
- except:
- self._msg = ''
- log.error("Unable to apply formatted string attributes to msg.%s: %s" % (self._template, self._attr))
- else:
- self._msg = ''
- log.error("Missing configuration value: msg.%s" % self._template)
+ if self._template in CONFIG['msg']:
+ try:
+ return CONFIG['msg'][self._template].format(**self._attr)
+ except:
+ log.error("Unable to apply formatted string attributes to msg.%s: %s" % (self._template, self._attr))
+ else:
+ log.error("Missing configuration value: msg.%s" % self._template)
- return self._msg
+ return ''
def get_runlevel(self):
"""
@@ -83,6 +79,7 @@ class Issue(object):
return self._runlevel
+ @lru_cache()
def get_suppression_duration(self):
"""
Provides the number of hours we should suppress this message after it has
@@ -92,19 +89,15 @@ class Issue(object):
after its been shown
"""
- if self._suppression_duration is None:
- if self._template in CONFIG['suppression']:
- suppression_duration = CONFIG['suppression'][self._template]
+ if self._template in CONFIG['suppression']:
+ suppression_duration = CONFIG['suppression'][self._template]
- try:
- self._suppression_duration = int(suppression_duration)
- except ValueError:
- log.error("Non-numic suppression time (%s): %s" % (self._template, suppression_duration))
- self._suppression_duration = 0
- else:
- self._suppression_duration = 0
+ try:
+ return int(suppression_duration)
+ except ValueError:
+ log.error("Non-numic suppression time (%s): %s" % (self._template, suppression_duration))
- return self._suppression_duration
+ return 0
def __str__(self):
return "%s: %s" % (self.get_runlevel(), self.get_message())
More information about the tor-commits
mailing list