Conditions | 12 |
Total Lines | 61 |
Lines | 60 |
Ratio | 98.36 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Netstat.check() 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 | # -*- coding: utf-8 -*- |
||
66 | def check(self): |
||
67 | """Return detailed network statitistics proc file net/netstat. |
||
68 | |||
69 | Note: ECT1Pkts and ECT0Pkts relate to ECT congestion notifications. |
||
70 | |||
71 | :rtype: plumd.Result |
||
72 | """ |
||
73 | # what metrics do we want to record? |
||
74 | result = plumd.Result("netstat") |
||
75 | |||
76 | # read the proc file |
||
77 | dat = None |
||
78 | with open(self.proc_file, 'r') as f: |
||
79 | dat = f.read().strip().split("\n") |
||
80 | |||
81 | # timestamp for Differential calculations |
||
82 | ts = time.time() |
||
83 | |||
84 | # split values into lists |
||
85 | dlist = deque([entry.split() for entry in dat]) |
||
86 | |||
87 | # put lists into key: value dict |
||
88 | metrics = {} |
||
89 | while dlist: |
||
90 | headers = dlist.popleft() |
||
91 | values = dlist.popleft() |
||
92 | # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes : |
||
93 | metrics[headers[0][:-1]] = dict(zip(headers, values)) |
||
94 | |||
95 | # record gauges |
||
96 | for ext, mnames in self.gauges.items(): |
||
97 | if ext not in metrics: |
||
98 | self.log.warn("netstat: unknown extension: {0}".format(ext)) |
||
99 | del self.gauges[ext] |
||
100 | continue |
||
101 | values = metrics[ext] |
||
102 | for mname in mnames: |
||
103 | if mname in values: |
||
104 | mstr = "{0}.{1}".format(ext, mname) |
||
105 | result.add(plumd.Int(mstr, values[mname])) |
||
106 | else: |
||
107 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
||
108 | self.gauges[ext].remove(mname) |
||
109 | |||
110 | # record rates |
||
111 | for ext, mnames in self.rates.items(): |
||
112 | if ext not in metrics: |
||
113 | self.log.warn("netstat: unknown extension: {0}".format(ext)) |
||
114 | del self.rates[ext] |
||
115 | continue |
||
116 | values = metrics[ext] |
||
117 | for mname in mnames: |
||
118 | if mname in values: |
||
119 | mstr = "{0}.{1}".format(ext, mname) |
||
120 | mval = self.calc.per_second(mstr, float(values[mname]), ts) |
||
121 | result.add(plumd.Float(mstr, mval)) |
||
122 | else: |
||
123 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
||
124 | self.rates[ext].remove(mname) |
||
125 | |||
126 | return [result] |
||
127 |