| Conditions | 21 |
| Total Lines | 69 |
| Code Lines | 45 |
| 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.glances_quicklook.Plugin.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 -*- |
||
| 108 | def msg_curse(self, args=None, max_width=10): |
||
| 109 | """Return the list to display in the UI.""" |
||
| 110 | # Init the return message |
||
| 111 | ret = [] |
||
| 112 | |||
| 113 | # Only process if stats exist... |
||
| 114 | if not self.stats or self.is_disable(): |
||
| 115 | return ret |
||
| 116 | |||
| 117 | # Define the data: Bar (default behavor) or Sparkline |
||
| 118 | sparkline_tag = False |
||
| 119 | if self.args.sparkline and self.history_enable() and not self.args.client: |
||
| 120 | data = Sparkline(max_width) |
||
| 121 | sparkline_tag = data.available |
||
| 122 | if not sparkline_tag: |
||
| 123 | # Fallback to bar if Sparkline module is not installed |
||
| 124 | data = Bar(max_width, |
||
| 125 | percentage_char=self.get_conf_value('percentage_char', |
||
| 126 | default=['|'])[0]) |
||
| 127 | |||
| 128 | # Build the string message |
||
| 129 | if 'cpu_name' in self.stats and 'cpu_hz_current' in self.stats and 'cpu_hz' in self.stats: |
||
| 130 | msg_name = self.stats['cpu_name'] |
||
| 131 | if self.stats['cpu_hz_current'] and self.stats['cpu_hz']: |
||
| 132 | msg_freq = ' - {:.2f}/{:.2f}GHz'.format(self._hz_to_ghz(self.stats['cpu_hz_current']), |
||
| 133 | self._hz_to_ghz(self.stats['cpu_hz'])) |
||
| 134 | else: |
||
| 135 | msg_freq = '' |
||
| 136 | if len(msg_name + msg_freq) - 6 <= max_width: |
||
| 137 | ret.append(self.curse_add_line(msg_name)) |
||
| 138 | ret.append(self.curse_add_line(msg_freq)) |
||
| 139 | ret.append(self.curse_new_line()) |
||
| 140 | for key in ['cpu', 'mem', 'swap']: |
||
| 141 | if key == 'cpu' and args.percpu: |
||
| 142 | if sparkline_tag: |
||
| 143 | raw_cpu = self.get_raw_history(item='percpu', nb=data.size) |
||
|
|
|||
| 144 | for cpu_index, cpu in enumerate(self.stats['percpu']): |
||
| 145 | if sparkline_tag: |
||
| 146 | # Sparkline display an history |
||
| 147 | data.percents = [i[1][cpu_index]['total'] for i in raw_cpu] |
||
| 148 | # A simple padding in order to align metrics to the right |
||
| 149 | data.percents += [None] * (data.size - len(data.percents)) |
||
| 150 | else: |
||
| 151 | # Bar only the last value |
||
| 152 | data.percent = cpu['total'] |
||
| 153 | if cpu[cpu['key']] < 10: |
||
| 154 | msg = '{:3}{} '.format(key.upper(), cpu['cpu_number']) |
||
| 155 | else: |
||
| 156 | msg = '{:4} '.format(cpu['cpu_number']) |
||
| 157 | ret.extend(self._msg_create_line(msg, data, key)) |
||
| 158 | ret.append(self.curse_new_line()) |
||
| 159 | else: |
||
| 160 | if sparkline_tag: |
||
| 161 | # Sparkline display an history |
||
| 162 | data.percents = [i[1] for i in self.get_raw_history(item=key, nb=data.size)] |
||
| 163 | # A simple padding in order to align metrics to the right |
||
| 164 | data.percents += [None] * (data.size - len(data.percents)) |
||
| 165 | else: |
||
| 166 | # Bar only the last value |
||
| 167 | data.percent = self.stats[key] |
||
| 168 | msg = '{:4} '.format(key.upper()) |
||
| 169 | ret.extend(self._msg_create_line(msg, data, key)) |
||
| 170 | ret.append(self.curse_new_line()) |
||
| 171 | |||
| 172 | # Remove the last new line |
||
| 173 | ret.pop() |
||
| 174 | |||
| 175 | # Return the message with decoration |
||
| 176 | return ret |
||
| 177 | |||
| 196 | return hz * 1000000.0 |