From bf3b77ddbd76785e1ae2e60916b9b99ef63b217c Mon Sep 17 00:00:00 2001 From: enkore Date: Fri, 22 Feb 2013 23:21:53 +0100 Subject: [PATCH] Code cleanup --- i3pystatus/batterychecker.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/i3pystatus/batterychecker.py b/i3pystatus/batterychecker.py index b95da60..38ae486 100644 --- a/i3pystatus/batterychecker.py +++ b/i3pystatus/batterychecker.py @@ -10,21 +10,18 @@ class Battery: The data from uevent is transformed into attributes of this class, stripping their prefix. (i.e. POWER_SUPPLY_NAME becomes the NAME attribute). - Numbers are automatically converted to floats. - - Strings are stripped. - - (configparser would be a bit overkill for this) + Numbers are automatically converted to floats. Strings are stripped. """ - prefix = "POWER_SUPPLY_" + @staticmethod + def lchop(string, prefix="POWER_SUPPLY_"): + if string.startswith(prefix): + return string[len(prefix):] + return string @staticmethod - def lchop(string, substring): - if string.startswith(substring): - return string[len(substring):] - - return string + def convert(value): + return float(value) if value.isdecimal() else value.strip() def __init__(self, file): self.parse(file) @@ -36,13 +33,8 @@ class Battery: def parse_line(self, line): key, value = line.split("=", 2) - key = self.lchop(key, self.prefix) - value = value.strip() - if value.isdecimal(): - value = float(value) - - setattr(self, key, value) + setattr(self, self.lchop(key), self.convert(value)) class BatteryChecker(IntervalModule): """