Python:对第一个OOP样式脚本的反馈/更正 [英] Python: feedback / corrections on first OOP style script
问题描述
我想对我的第一个使用OOP样式的Python脚本提供一些反馈.这是一个Munin插件,根据插件的名称(dell_fans,dell_temps)绘制平均风扇速度或平均机箱温度的图形.
I would like some feedback on my first Python script that makes use of OOP style. This is a Munin plugin that graphs average fan speed or average chassis temp depending on the name of the plugin (dell_fans, dell_temps).
一个小时左右之前,我提交了程序版本将风扇速度插件添加到stackoverflow,以获得将其转换为OOP样式的帮助.然后,我基于此构建了两个脚本.任何反馈,建议,更正将非常有帮助.我想纠正我在固执之前可能存在的任何误解.
An hour or so ago I submitted a procedural version of the fan speed plugin to stackoverflow to get help converting it to an OOP style. I then built off of that to combine the two scripts. Any feedback, suggestions, corrections would be very helpful. I would like to correct any misconceptions I may have before they cement.
更新:已修改为具有通用基类.还有其他建议吗?
Update: Modified to have common base class. Any other suggestions?
import sys
import subprocess
class Statistics(object):
def __init__(self, command):
self.command = command.split()
def average(self):
data = subprocess.Popen(self.command,stdout=subprocess.PIPE).stdout.readlines()
count = total = 0
for item in data:
if "Reading" in item:
# Extract variable length fan speed, without regex.
total += float(item.split(":")[1].split()[0])
count += 1
# Sometimes omreport returns zero output if omsa services aren't started.
if not count or not total:
raise ValueError("No output from omreport. Is OMSA services started?")
avg = (total / count)
return avg
def print_autoconfig(self):
print "autoconfig goes here"
class Fanspeed(Statistics):
def __init__(self, command):
Statistics.__init__(self, command)
def print_config(self):
print "graph_title Average Fan Speed"
print "graph_args --base 1000 -l 0"
print "graph_vlabel speed (RPM)"
print "graph_category Chassis"
print "graph_info This graph shows the average speed of all fans"
print "graph_period second"
print "data.label speed"
print "data.info Average fan speed for the five minutes."
class Temps(Statistics):
def __init__(self, command):
Statistics.__init__(self, command)
def print_config(self):
print "graph_title Average Temperature"
print "graph_args --upper-limit 120 -l 0"
print "graph_vlabel Celsius"
print "graph_category Chassis"
print "graph_info This graph shows the avg temp of all sensors."
print "graph_period second"
print "data.label temp"
print "data.info Average chassis temperature for the five minutes."
if __name__ == '__main__':
# Munin populates sys.argv[1] with "" (an empty argument), lets remove it.
sys.argv = [x for x in sys.argv if x]
if "fans" in sys.argv[0]:
cmd = "/usr/sbin/omreport chassis fans"
omdata = Fanspeed(cmd)
elif "temps" in sys.argv[0]:
cmd = "/usr/sbin/omreport chassis temps"
omdata = Temps(cmd)
else:
print >> sys.stderr, "Change filename to dell_fans or dell_temps."
sys.exit(1)
if len(sys.argv) > 1:
if sys.argv[1].lower() == "autoconfig":
omdata.print_autoconfig()
elif sys.argv[1].lower() == "config":
omdata.print_config()
else:
try:
average = omdata.average()
print "data.value %s" % average
except OSError, e:
print >> sys.stderr, "Error running '%s', %s" % (cmd, e)
sys.exit(1)
except ValueError, e:
# Sometimes omreport returns zero output if omsa services aren't started.
print >> sys.stderr, 'Error: "omreport chassis fans" returned 0 output.'
print >> sys.stderr, 'OMSA running? Try: "srvadmin-services.sh status".'
sys.exit(1)
推荐答案
Temps
是FanSpeed
吗?这是对子类是否合适的试金石(例如,大象是动物,汽车不是动物-因此,可能有Animal
的子类来模拟大象,而不是Animal
的子类是合适的)汽车的模型.
Is Temps
a FanSpeed
? That's the litmus test for whether subclassing is appropriate (e.g. an elephant is an animal, a car is not an animal - so it might be appropriate to have a subclass of Animal
which models an elephant, but not a subclass of Animal
which models a car).
听起来他们在建模两个不同的事物-是的,为他们创建一个通用的基类.
It sounds like they're modelling two different things - so yes, create a common base class for them.
这篇关于Python:对第一个OOP样式脚本的反馈/更正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!