Completed
Push — master ( d1f0a7...3b2ece )
by Edward
21:04 queued 05:38
created

testpacks.checks.actions.checks.print_load_avg()   F

Complexity

Conditions 11

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 36
rs 3.1765
cc 11

How to fix   Complexity   

Complexity

Complex classes like testpacks.checks.actions.checks.print_load_avg() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/python
2
3
import sys
4
5
6
def print_load_avg(args):
7
    period = args[1]
8
9
    loadavg_file = '/proc/loadavg'
10
    cpuinfo_file = '/proc/cpuinfo'
11
    cpus = 0
12
13
    try:
14
        fh = open(loadavg_file, 'r')
15
        load = fh.readline().split()[0:3]
16
        fh.close()
17
    except:
18
        sys.stderr.write('Error opening %s\n' % loadavg_file)
19
        sys.exit(2)
20
21
    try:
22
        fh = open(cpuinfo_file, 'r')
23
        for line in fh:
24
            if 'processor' in line:
25
                cpus += 1
26
        fh.close()
27
    except:
28
        sys.stderr.write('Error opeing %s\n' % cpuinfo_file)
29
30
    one_min = '1 min load/core: %s' % str(float(load[0]) / cpus)
31
    five_min = '5 min load/core: %s' % str(float(load[1]) / cpus)
32
    fifteen_min = '15 min load/core: %s' % str(float(load[2]) / cpus)
33
34
    if period == '1' or period == 'one':
35
        print(one_min)
36
    elif period == '5' or period == 'five':
37
        print(five_min)
38
    elif period == '15' or period == 'fifteen':
39
        print(fifteen_min)
40
    else:
41
        print(one_min + " " + five_min + " " + fifteen_min)
42
43
44
if __name__ == '__main__':
45
    print_load_avg(sys.argv)
46