From 47e81e7471d5f042f756cc3d616762248b5e4f5c Mon Sep 17 00:00:00 2001 From: desbma Date: Thu, 15 Jan 2015 21:20:03 +0100 Subject: [PATCH] Pre compile regexes --- hddfancontrol/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hddfancontrol/__init__.py b/hddfancontrol/__init__.py index e74fdf8..9b338b4 100755 --- a/hddfancontrol/__init__.py +++ b/hddfancontrol/__init__.py @@ -38,6 +38,8 @@ class Drive: DriveState = enum.Enum("DriveState", ("UNKNOWN", "ACTIVE_IDLE", "STANDBY", "SLEEPING")) + HDPARM_GET_TEMP_HITACHI_REGEX = re.compile("drive temperature \(celsius\) is:\s*([0-9]*)") + def __init__(self, device_filepath, stat_filepath): assert(stat.S_ISBLK(os.stat(device_filepath).st_mode)) self.device_filepath = device_filepath @@ -95,7 +97,7 @@ class Drive: stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, universal_newlines=True) - temp = int(re.search("drive temperature \(celsius\) is:\s*([0-9]*)", output).group(1)) + temp = int(__class__.HDPARM_GET_TEMP_HITACHI_REGEX.search(output).group(1)) self.logger.debug("Drive temperature: %u C" % (temp)) return temp @@ -173,6 +175,8 @@ class Fan: """ Represent a fan associated with a PWM file to control its speed. """ + LAST_DIGITS_REGEX = re.compile("[^0-9]*([0-9]*)$") + def __init__(self, id, pwm_filepath, start_value, stop_value): assert(0 <= start_value <= 255) assert(0 <= stop_value <= 255) @@ -185,7 +189,7 @@ class Fan: def getRpm(self): """ Read fan speed in revolutions per minute. """ - pwm_num = int(re.search("[^0-9]*([0-9]*)$", self.pwm_filepath).group(1)) + pwm_num = int(__class__.LAST_DIGITS_REGEX.search(self.pwm_filepath).group(1)) fan_input_filepath = os.path.join(os.path.dirname(self.pwm_filepath), "fan%u_input" % (pwm_num)) with open(fan_input_filepath, "rt") as fan_input_file: rpm = int(fan_input_file.read().strip())