From d723ae47e3dcba7b251d71deb5e7b50e0d176025 Mon Sep 17 00:00:00 2001 From: gacekjk Date: Fri, 5 Jun 2015 04:13:37 +0200 Subject: [PATCH 1/5] added cpu frequency module --- i3pystatus/cpu_freq.py | 25 +++++++++++++++++++++++++ tests/cpufreq01 | 4 ++++ tests/test_cpu_freq.py | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 i3pystatus/cpu_freq.py create mode 100644 tests/cpufreq01 create mode 100644 tests/test_cpu_freq.py diff --git a/i3pystatus/cpu_freq.py b/i3pystatus/cpu_freq.py new file mode 100644 index 0000000..cc3ffb2 --- /dev/null +++ b/i3pystatus/cpu_freq.py @@ -0,0 +1,25 @@ +from i3pystatus import IntervalModule + + +class CpuFreq(IntervalModule): + format = "{avg}" + settings = ( + "format", + ("color", "The text color"), + ("average", "Show average for every core"), + ("file", "override default path"), + ) + + file = '/proc/cpuinfo' + color = '#FFFFFF' + + def run(self): + with open(self.file) as f: + mhz_values = [float(line.split(':')[1]) for line in f if line.startswith('cpu MHz')] + + cdict = {"core{}".format(key): str(value) for key, value in enumerate(mhz_values)} + cdict['avg'] = str(sum(mhz_values) / len(mhz_values)) + + self.output = { + "full_text": self.format.format(**cdict), + } diff --git a/tests/cpufreq01 b/tests/cpufreq01 new file mode 100644 index 0000000..5b79afb --- /dev/null +++ b/tests/cpufreq01 @@ -0,0 +1,4 @@ +cpu MHz : 1240.382 +cpu MHz : 1236.828 +cpu MHz : 1203.007 +cpu MHz : 1264.859 \ No newline at end of file diff --git a/tests/test_cpu_freq.py b/tests/test_cpu_freq.py new file mode 100644 index 0000000..ed678f3 --- /dev/null +++ b/tests/test_cpu_freq.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import os +from i3pystatus import cpu_freq + + +def cpu_freq_test(tfpath, tformat, expected): + cf = cpu_freq.CpuFreq(file=os.path.join(os.path.dirname(__file__), tfpath), format=tformat) + cf.run() + assert cf.output["full_text"] == expected + + +def test_basic(): + cases = [ + ('cpufreq01', '1240.382', '1236.828', '1203.007', '1264.859', '1236.269'), + ] + for path, core0, core1, core2, core3, avg in cases: + cpu_freq_test(path, "{avg}", avg) + cpu_freq_test(path, "{core0}", core0) + cpu_freq_test(path, "{core1}", core1) + cpu_freq_test(path, "{core2}", core2) + cpu_freq_test(path, "{core3}", core3) From f8c803e1cb0e01616cd45774661be0a5c9213d37 Mon Sep 17 00:00:00 2001 From: gacekjk Date: Fri, 5 Jun 2015 15:04:03 +0200 Subject: [PATCH 2/5] GHz values added --- i3pystatus/cpu_freq.py | 9 +++++++-- tests/test_cpu_freq.py | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/i3pystatus/cpu_freq.py b/i3pystatus/cpu_freq.py index cc3ffb2..a4f5513 100644 --- a/i3pystatus/cpu_freq.py +++ b/i3pystatus/cpu_freq.py @@ -16,9 +16,14 @@ class CpuFreq(IntervalModule): def run(self): 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] - cdict = {"core{}".format(key): str(value) for key, value in enumerate(mhz_values)} - cdict['avg'] = str(sum(mhz_values) / len(mhz_values)) + mhz = {"core{}".format(key): "{0:4.3f}".format(value) for key, value in enumerate(mhz_values)} + ghz = {"core{}g".format(key): "{0:1.2f}".format(value) for key, value in enumerate(ghz_values)} + cdict = mhz.copy() + 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) self.output = { "full_text": self.format.format(**cdict), diff --git a/tests/test_cpu_freq.py b/tests/test_cpu_freq.py index ed678f3..4c857b4 100644 --- a/tests/test_cpu_freq.py +++ b/tests/test_cpu_freq.py @@ -12,11 +12,16 @@ def cpu_freq_test(tfpath, tformat, expected): def test_basic(): cases = [ - ('cpufreq01', '1240.382', '1236.828', '1203.007', '1264.859', '1236.269'), + ('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 in cases: + for path, core0, core1, core2, core3, avg, core0g, core1g, core2g, core3g, avgg in cases: cpu_freq_test(path, "{avg}", avg) cpu_freq_test(path, "{core0}", core0) cpu_freq_test(path, "{core1}", core1) cpu_freq_test(path, "{core2}", core2) cpu_freq_test(path, "{core3}", core3) + cpu_freq_test(path, "{core0g}", core0g) + cpu_freq_test(path, "{core1g}", core1g) + cpu_freq_test(path, "{core2g}", core2g) + cpu_freq_test(path, "{core3g}", core3g) + cpu_freq_test(path, "{avgg}", avgg) From 2f778885e6fd10eca3bd86f45a29e6dbd54a6375 Mon Sep 17 00:00:00 2001 From: gacekjk Date: Fri, 5 Jun 2015 17:23:21 +0200 Subject: [PATCH 3/5] 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) From be8701625936ec39fdb6fe09a9658b76643a5049 Mon Sep 17 00:00:00 2001 From: gacekjk Date: Fri, 5 Jun 2015 17:40:45 +0200 Subject: [PATCH 4/5] fix pep8 --- tests/test_cpu_freq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cpu_freq.py b/tests/test_cpu_freq.py index 43fd64a..a3a2e03 100644 --- a/tests/test_cpu_freq.py +++ b/tests/test_cpu_freq.py @@ -1,8 +1,8 @@ +#!/usr/bin/env python3 # coding=utf-8 """ Basic tests for the cpu_freq module """ -#!/usr/bin/env python3 import os From adbb9c0165d92ca5e506477c68f22621c5998c18 Mon Sep 17 00:00:00 2001 From: gacekjk Date: Fri, 5 Jun 2015 22:23:33 +0200 Subject: [PATCH 5/5] changed docstrings to be more informative, removed unnecessary docstring --- i3pystatus/cpu_freq.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/i3pystatus/cpu_freq.py b/i3pystatus/cpu_freq.py index 22df912..e78d9d5 100644 --- a/i3pystatus/cpu_freq.py +++ b/i3pystatus/cpu_freq.py @@ -1,6 +1,3 @@ -""" -The module gathers information by default from `/proc/cpuinfo` about the current cpu frequency -""" # coding=utf-8 from i3pystatus import IntervalModule @@ -13,8 +10,8 @@ class CpuFreq(IntervalModule): * `{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` + * `{coreX}` - frequency of core number `X` in MHz (format `4.3f`), where 0 <= `X` <= number of cores - 1 + * `{coreXg}` - frequency of core number `X` in GHz (fromat `1.2f`), where 0 <= `X` <= number of cores - 1 """ format = "{avgg}"