Conditions | 18 |
Total Lines | 66 |
Code Lines | 43 |
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 -*- |
||
129 | def msg_curse(self, args=None, max_width=10): |
||
130 | """Return the list to display in the UI.""" |
||
131 | # Init the return message |
||
132 | ret = [] |
||
133 | |||
134 | # Only process if stats exist... |
||
135 | if not self.stats or self.is_disable(): |
||
136 | return ret |
||
137 | |||
138 | # Define the data: Bar (default behavor) or Sparkline |
||
139 | sparkline_tag = False |
||
140 | if self.args.sparkline and self.history_enable(): |
||
141 | data = Sparkline(max_width) |
||
142 | sparkline_tag = data.available |
||
143 | if not sparkline_tag: |
||
144 | # Fallback to bar if Sparkline module is not installed |
||
145 | data = Bar(max_width, |
||
146 | percentage_char=self.get_conf_value('percentage_char', |
||
147 | default=['|'])[0]) |
||
148 | |||
149 | # Build the string message |
||
150 | if 'cpu_name' in self.stats and 'cpu_hz_current' in self.stats and 'cpu_hz' in self.stats: |
||
151 | msg_name = '{} - '.format(self.stats['cpu_name']) |
||
152 | msg_freq = '{:.2f}/{:.2f}GHz'.format(self._hz_to_ghz(self.stats['cpu_hz_current']), |
||
153 | self._hz_to_ghz(self.stats['cpu_hz'])) |
||
154 | if len(msg_name + msg_freq) - 6 <= max_width: |
||
155 | ret.append(self.curse_add_line(msg_name)) |
||
156 | ret.append(self.curse_add_line(msg_freq)) |
||
157 | ret.append(self.curse_new_line()) |
||
158 | for key in ['cpu', 'mem', 'swap']: |
||
159 | if key == 'cpu' and args.percpu: |
||
160 | if sparkline_tag: |
||
161 | raw_cpu = self.get_raw_history(item='percpu', nb=data.size) |
||
162 | for cpu_index, cpu in enumerate(self.stats['percpu']): |
||
163 | if sparkline_tag: |
||
164 | # Sparkline display an history |
||
165 | data.percents = [i[1][cpu_index]['total'] for i in raw_cpu] |
||
166 | # A simple padding in order to align metrics to the right |
||
167 | data.percents += [None] * (data.size - len(data.percents)) |
||
168 | else: |
||
169 | # Bar only the last value |
||
170 | data.percent = cpu['total'] |
||
171 | if cpu[cpu['key']] < 10: |
||
172 | msg = '{:3}{} '.format(key.upper(), cpu['cpu_number']) |
||
173 | else: |
||
174 | msg = '{:4} '.format(cpu['cpu_number']) |
||
175 | ret.extend(self._msg_create_line(msg, data, key)) |
||
176 | ret.append(self.curse_new_line()) |
||
177 | else: |
||
178 | if sparkline_tag: |
||
179 | # Sparkline display an history |
||
180 | data.percents = [i[1] for i in self.get_raw_history(item=key, nb=data.size)] |
||
181 | # A simple padding in order to align metrics to the right |
||
182 | data.percents += [None] * (data.size - len(data.percents)) |
||
183 | else: |
||
184 | # Bar only the last value |
||
185 | data.percent = self.stats[key] |
||
186 | msg = '{:4} '.format(key.upper()) |
||
187 | ret.extend(self._msg_create_line(msg, data, key)) |
||
188 | ret.append(self.curse_new_line()) |
||
189 | |||
190 | # Remove the last new line |
||
191 | ret.pop() |
||
192 | |||
193 | # Return the message with decoration |
||
194 | return ret |
||
195 | |||
211 |