gpt4 book ai didi

Python:对第一个 OOP 样式脚本的反馈/更正

转载 作者:太空宇宙 更新时间:2023-11-03 23:50:10 25 4
gpt4 key购买 nike

我想要一些关于我的第一个使用 OOP 风格的 Python 脚本的反馈。这是一个 Munin 插件,可根据插件名称(dell_fans、dell_temps)绘制平均风扇速度或平均机箱温度图。

大约一个小时前我submitted a procedural version的风扇速度插件到 stackoverflow 以获得帮助将其转换为 OOP 样式。然后我以此为基础将这两个脚本结合起来。任何反馈、建议、更正都会非常有帮助。我想在它们巩固之前纠正我可能有的任何误解。

更新:修改为具有公共(public)基类。还有其他建议吗?

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)

最佳答案

TempsFanSpeed 吗?这是子类化是否合适的试金石(例如,大象是动物,汽车不是动物 - 因此,Animal 的子类模拟大象而不是动物可能是合适的Animal 的子类,模拟汽车)。

听起来他们正在为两种不同的事物建模 - 所以是的,为它们创建一个公共(public)基类。

关于Python:对第一个 OOP 样式脚本的反馈/更正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2191639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com