-2
我是python編程的新手,我需要一個能顯示當前CPU溫度的程序。我的CPU有4個內核,所以我做了以下程序。Python noob,需要幫助改進方案
#!/usr/bin/python
import subprocess
import re
sensor_data = subprocess.check_output('/usr/bin/sensors')
find_temp = re.search(r'Core 0:\s+\+([\d]+).*', sensor_data)
if find_temp:
core_0_temp = find_temp.groups()[0]
else:
core_0_temp = 0
find_temp = re.search(r'Core 1:\s+\+([\d]+).*', sensor_data)
if find_temp:
core_1_temp = find_temp.groups()[0]
else:
core_1_temp = 0
find_temp = re.search(r'Core 2:\s+\+([\d]+).*', sensor_data)
if find_temp:
core_2_temp = find_temp.groups()[0]
else:
core_2_temp = 0
find_temp = re.search(r'Core 3:\s+\+([\d]+).*', sensor_data)
if find_temp:
core_3_temp = find_temp.groups()[0]
else:
core_3_temp = 0
print core_0_temp, core_1_temp, core_2_temp, core_3_temp
任何人都可以提出一些建議,如何改善這個計劃?
目前它的工作原理,但我能做些什麼來改善這個代碼?傳感器命令的
[email protected]:~/utils/cpu$ ./cpu_temp
49 48 48 46
[email protected]:~/utils/cpu$
結果給出如下
[email protected]:~/utils/cpu$ /usr/bin/sensors
atk0110-acpi-0
Adapter: ACPI interface
Vcore Voltage: +0.93 V (min = +0.80 V, max = +1.60 V)
+3.3V Voltage: +3.28 V (min = +2.97 V, max = +3.63 V)
+5V Voltage: +5.14 V (min = +4.50 V, max = +5.50 V)
+12V Voltage: +12.03 V (min = +10.20 V, max = +13.80 V)
CPU Fan Speed: 2721 RPM (min = 600 RPM, max = 7200 RPM)
Chassis1 Fan Speed: 0 RPM (min = 600 RPM, max = 7200 RPM)
Chassis2 Fan Speed: 0 RPM (min = 600 RPM, max = 7200 RPM)
NB Fan Speed: 1318 RPM (min = 600 RPM, max = 7200 RPM)
Power Fan Speed: 0 RPM (min = 0 RPM, max = 7200 RPM)
CPU Temperature: +56.0°C (high = +60.0°C, crit = +95.0°C)
MB Temperature: +60.0°C (high = +45.0°C, crit = +75.0°C)
NB Temperature: +56.5°C (high = +60.0°C, crit = +95.0°C)
coretemp-isa-0000
Adapter: ISA adapter
Core 0: +48.0°C (high = +80.0°C, crit = +100.0°C)
Core 1: +47.0°C (high = +80.0°C, crit = +100.0°C)
Core 2: +47.0°C (high = +80.0°C, crit = +100.0°C)
Core 3: +47.0°C (high = +80.0°C, crit = +100.0°C)
radeon-pci-0400
Adapter: PCI adapter
temp1: +57.0°C (crit = +120.0°C, hyst = +90.0°C)
[email protected]:~/utils/cpu$
更好的發佈在[Codereview.SE](http://codereview.stackexchange.com)上。 – 2014-09-03 08:28:26
你想要什麼樣的改進? – gosom 2014-09-03 08:29:51
連續命名的變量('core_1','core_2')通常表示您確實需要列表。 – georg 2014-09-03 08:31:11