Python: convert script from procedural to OOP style -


I have written that this easy munin plugin has been written for the average fan speed of the graph and I have it in OOP again I want to - strictly as a drill to learn. There is no clue, although where to start, it seems like giving some directions to anyone or even an example of how this script should appear, when it is done, I will call it another script again I will use it again in OOP style; For re-learning purposes.

  Import System Import Subprocess CMD = "/ USR / SBI / Omerport Chassis Fans" .split () #Munein sys.argv [1] to "" (an empty argument), remove it . Sys.argv = [xs.x for X in sys.argv] if len (sys.argv)> 1: If sys.argv [1]. Liquid () == "Autoconfig": print "Autoconfig" alip sys.argv [1]. Flowers () == "Config": Print "Graph_title Average Fan Speed" Print "Article - Base 1000-L" Print "Graph- Level Speed ​​(RPM)" Print "Graphic Chassis" Print "Graph_Inffa This article shows the average of all fans The speed indicates the duration of the "graph" graph - the second printing "speed". The average speed "speed" speed information is five minutes for average fan speed. "Else: Try: Data = Subprocess .POPN (CMD, Standout = Subprances. PPE ) .stdout.readlines () OSERRO, E: Print & gt; & Gt; Sys.stderr, "'% s',% s"% ("" .join (cmd), e) sys.exit (1) calculation = total = 0 error in running for item in data: if the item reads " ": Remove the variable length fan speed, without # ragges. Total + = intestine (item split (":") [1] .split () [0]) count + = 1 # OMOVERPORT sometimes returns omreport zero output when OMS service is not started. If not counting or not total: Print & gt; & Gt; Sys.stderr, 'Error: "Omwellport Chassis fans" returned 0 outputs. Print & gt; & Gt; Sys.stderr, 'Ohmasa going on? Try: "srvadmin-services.sh status". 'Sys.exit (1) average = (total / count) print "speed.value% s"% avg  

You can remake and identify the code and data that runs simultaneously in OOP. After this you can merge into "Classes".

You seem to produce a process above the actual data. Code is running on it I think that you can create a square from it if you want but it is a bit silly :)

So, something like this (clearly completely Untested code):

  import system import subprocess class Fanspeed (object): def __init __ (self, command): self .command = command.split () def average_fan_speed (self): data = Sub process. Popin (CMD, stdout = subprocess.PIPE) .stdout.readlines () Calculation = Total = 0 For items in data: If the "Reading" in the item: # Remove the variable length fan speed, without ragges. Total + = intestine (item split (":") [1] .split () [0]) count + = 1 # OMOVERPORT sometimes returns omreport zero output when OMS service is not started. If not counting or not total: increase the value ("I am not getting any fan." What OMSA services have started? Average = (total / calculated) return average% __main__ == '__main__': # Munin Sys.argv ["" (an empty argument), removes it. Sys.argv = [xs.x for x in sys.argv] if len (sys.argv)> 1: if sys.argv [ 1] .lower () = "Autoconfig": print "autoconfig" elif sys.argv [1]. Flower () == "config": print "graph_title average fan speed" print "graph_args --base 1000ll "Print" graph_vlabel speed (RPM) "print" graphic chassis "Q Untf "graph_info This article shows the average speed of all fans" print "graph_from at the other" print "speed. Label speed" print "speed.info average fan speed for five minutes." And: try: cmd = "/ usr / SBI / Omvelport Chassis fans "acclaimed = Fanspid (CMD) Evergue E = Acclaimed. Overage_fan_speed (except OSError, E: print> Sys.stderr," Error '% s,% s' % (Cmd, e) sys.exit (1) except ValueError, E: # occasionally omreport zero Tput gives prints that had started Omma service & gt; & Gt; Sys.stderr, 'Error: "Omwellport Chassis fans" returned 0 outputs. Print & gt; & Gt; Sys.stderr, 'Ohmasa going on? Try: "srvadmin-services.sh status". 'Sys.exit (1)  

But YMMV is probably a bit obvious.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -