From 2f778885e6fd10eca3bd86f45a29e6dbd54a6375 Mon Sep 17 00:00:00 2001 From: gacekjk Date: Fri, 5 Jun 2015 17:23:21 +0200 Subject: [PATCH] code refactor, reformat and add docstrings --- i3pystatus/cpu_freq.py | 30 +++++++++++++++++++++++++++--- tests/test_cpu_freq.py | 11 ++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/i3pystatus/cpu_freq.py b/i3pystatus/cpu_freq.py index a4f5513..22df912 100644 --- a/i3pystatus/cpu_freq.py +++ b/i3pystatus/cpu_freq.py @@ -1,19 +1,37 @@ +""" +The module gathers information by default from `/proc/cpuinfo` about the current cpu frequency +""" +# coding=utf-8 from i3pystatus import IntervalModule class CpuFreq(IntervalModule): - format = "{avg}" + """ + class uses by default `/proc/cpuinfo` to determine the current cpu frequency + + .. rubric:: Available formatters + + * `{avg}` - mean from all cores in MHz `4.3f` + * `{avgg}` - mean from all cores in GHz `1.2f` + * `{corex}` - frequency of a selected core in MHz `4.3f` + * `{corexg}` - frequesncy of a selscted core in GHz `1.2f` + + """ + format = "{avgg}" settings = ( "format", ("color", "The text color"), - ("average", "Show average for every core"), ("file", "override default path"), ) file = '/proc/cpuinfo' color = '#FFFFFF' - def run(self): + def createvaluesdict(self): + """ + function processes the /proc/cpuinfo file + :return: dictionary used as the full-text output for the module + """ with open(self.file) as f: mhz_values = [float(line.split(':')[1]) for line in f if line.startswith('cpu MHz')] ghz_values = [value / 1000.0 for value in mhz_values] @@ -24,7 +42,13 @@ class CpuFreq(IntervalModule): cdict.update(ghz) cdict['avg'] = "{0:4.3f}".format(sum(mhz_values) / len(mhz_values)) cdict['avgg'] = "{0:1.2f}".format(sum(ghz_values) / len(ghz_values), 2) + return cdict + + def run(self): + cdict = self.createvaluesdict() self.output = { "full_text": self.format.format(**cdict), + "color": self.color, + "format": self.format, } diff --git a/tests/test_cpu_freq.py b/tests/test_cpu_freq.py index 4c857b4..43fd64a 100644 --- a/tests/test_cpu_freq.py +++ b/tests/test_cpu_freq.py @@ -1,6 +1,11 @@ +# coding=utf-8 +""" +Basic tests for the cpu_freq module +""" #!/usr/bin/env python3 import os + from i3pystatus import cpu_freq @@ -11,8 +16,12 @@ def cpu_freq_test(tfpath, tformat, expected): def test_basic(): + """ + Tests against the pre-prepared file + """ cases = [ - ('cpufreq01', '1240.382', '1236.828', '1203.007', '1264.859', '1236.269', '1.24', '1.24', '1.20', '1.26', '1.24'), + ('cpufreq01', '1240.382', '1236.828', '1203.007', '1264.859', '1236.269', '1.24', '1.24', '1.20', '1.26', + '1.24'), ] for path, core0, core1, core2, core3, avg, core0g, core1g, core2g, core3g, avgg in cases: cpu_freq_test(path, "{avg}", avg)