| Conditions | 21 |
| Total Lines | 73 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 glances.plugins.percpu.PluginModel.msg_curse() 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 -*- |
||
| 140 | def msg_curse(self, args=None, max_width=None): |
||
| 141 | """Return the dict to display in the curse interface.""" |
||
| 142 | # Init the return message |
||
| 143 | ret = [] |
||
| 144 | |||
| 145 | # Only process if stats exist... |
||
| 146 | if not self.stats or not self.args.percpu or self.is_disabled(): |
||
| 147 | return ret |
||
| 148 | |||
| 149 | # Define the default header |
||
| 150 | header = ['user', 'system', 'idle', 'iowait', 'steal'] |
||
| 151 | |||
| 152 | # Build the string message |
||
| 153 | if self.is_disabled('quicklook'): |
||
| 154 | msg = '{:5}'.format('CPU') |
||
| 155 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 156 | header.insert(0, 'total') |
||
| 157 | |||
| 158 | # Per CPU stats displayed per line |
||
| 159 | for stat in header: |
||
| 160 | if stat not in self.stats[0]: |
||
| 161 | continue |
||
| 162 | msg = '{:>7}'.format(stat) |
||
| 163 | ret.append(self.curse_add_line(msg)) |
||
| 164 | |||
| 165 | # Manage the maximum number of CPU to display (related to enhancement request #2734) |
||
| 166 | if len(self.stats) > self.max_cpu_display: |
||
| 167 | # If the number of CPU is > max_cpu_display then sort and display top 'n' |
||
| 168 | percpu_list = sorted(self.stats, key=lambda x: x['total'], reverse=True) |
||
| 169 | else: |
||
| 170 | percpu_list = self.stats |
||
| 171 | |||
| 172 | # Per CPU stats displayed per column |
||
| 173 | for cpu in percpu_list[0 : self.max_cpu_display]: |
||
| 174 | ret.append(self.curse_new_line()) |
||
| 175 | if self.is_disabled('quicklook'): |
||
| 176 | try: |
||
| 177 | cpu_id = cpu[cpu['key']] |
||
| 178 | if cpu_id < 10: |
||
| 179 | msg = 'CPU{:1} '.format(cpu_id) |
||
| 180 | else: |
||
| 181 | msg = '{:4} '.format(cpu_id) |
||
| 182 | except TypeError: |
||
| 183 | # TypeError: string indices must be integers (issue #1027) |
||
| 184 | msg = '{:4} '.format('?') |
||
| 185 | ret.append(self.curse_add_line(msg)) |
||
| 186 | for stat in header: |
||
| 187 | if stat not in self.stats[0]: |
||
| 188 | continue |
||
| 189 | try: |
||
| 190 | msg = '{:6.1f}%'.format(cpu[stat]) |
||
| 191 | except TypeError: |
||
| 192 | msg = '{:>6}%'.format('?') |
||
| 193 | ret.append(self.curse_add_line(msg, self.get_alert(cpu[stat], header=stat))) |
||
| 194 | |||
| 195 | # Add a new line with sum of all others CPU |
||
| 196 | if len(self.stats) > self.max_cpu_display: |
||
| 197 | ret.append(self.curse_new_line()) |
||
| 198 | if self.is_disabled('quicklook'): |
||
| 199 | ret.append(self.curse_add_line('CPU* ')) |
||
| 200 | for stat in header: |
||
| 201 | if stat not in self.stats[0]: |
||
| 202 | continue |
||
| 203 | cpu_stat = sum([i[stat] for i in percpu_list[0 : self.max_cpu_display]]) / len( |
||
| 204 | [i[stat] for i in percpu_list[0 : self.max_cpu_display]] |
||
| 205 | ) |
||
| 206 | try: |
||
| 207 | msg = '{:6.1f}%'.format(cpu_stat) |
||
| 208 | except TypeError: |
||
| 209 | msg = '{:>6}%'.format('?') |
||
| 210 | ret.append(self.curse_add_line(msg, self.get_alert(cpu_stat, header=stat))) |
||
| 211 | |||
| 212 | return ret |
||
| 213 |