Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:第一个OOP风格脚本的反馈/更正_Python_Oop - Fatal编程技术网

Python:第一个OOP风格脚本的反馈/更正

Python:第一个OOP风格脚本的反馈/更正,python,oop,Python,Oop,我想要一些关于我的第一个使用OOP风格的Python脚本的反馈。这是一个Munin插件,根据插件名称(dell_风扇、dell_温度)绘制平均风扇速度或平均机箱温度 大约一个小时前,我将fan speed插件添加到stackoverflow,以获得将其转换为OOP风格的帮助。然后我在此基础上构建了两个脚本。任何反馈、建议和更正都会非常有帮助。我想纠正我可能存在的任何误解,然后再加以纠正 更新:修改为具有公共基类。还有其他建议吗 import sys import subprocess clas

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

大约一个小时前,我将fan speed插件添加到stackoverflow,以获得将其转换为OOP风格的帮助。然后我在此基础上构建了两个脚本。任何反馈、建议和更正都会非常有帮助。我想纠正我可能存在的任何误解,然后再加以纠正

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

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
a
FanSpeed
?这是检验子类划分是否合适的试金石(例如,大象是动物,汽车不是动物-因此,可能适合使用模拟大象的
animal
子类,而不是模拟汽车的
animal
子类)


听起来他们在为两个不同的东西建模——所以,是的,为他们创建一个公共基类。

语义上更合适的方法是定义一个主类(例如,
FanStatistics
或任何你想称之为它的东西),在这个主类中定义一般方法,比如
average
方法,并将其子类化为
FanSpeed
FanTemp
。这样你就不会混淆名称,因为温度不是速度的子类或专门化,但速度和温度都是抽象统计数据的专门化。

我觉得你有两种不同类型的统计数据需要建模,风扇速度(
Fanspeed
)和温度(
Temps
)。如果您希望在它们之间共享一些公共功能,那么可以为它们创建公共基类,例如,让我们调用它
Statistic

class Statistic(object):
    def average(self):
        pass # your logic to calculate the average here

class Fanspeed(Statistic):
    pass # your fan speed functionaly here

class Temps(Statistic):
    pass # your temperature functionaly here

明白了,我将创建一个公共基类(非常有意义)。还有其他建议吗?谢谢,我更新了脚本以使用公共基类。你还看到了什么吗?谢谢大家,你们所有人——还有这个网站——对像我这样的初学者来说都是很有价值的资源。