Conditions | 22 |
Total Lines | 130 |
Code Lines | 96 |
Lines | 22 |
Ratio | 16.92 % |
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_cpu.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 -*- |
||
215 | def msg_curse(self, args=None, max_width=None): |
||
216 | """Return the list to display in the UI.""" |
||
217 | # Init the return message |
||
218 | ret = [] |
||
219 | |||
220 | # Only process if stats exist and plugin not disable |
||
221 | if not self.stats or self.args.percpu or self.is_disable(): |
||
222 | return ret |
||
223 | |||
224 | # Build the string message |
||
225 | # If user stat is not here, display only idle / total CPU usage (for |
||
226 | # exemple on Windows OS) |
||
227 | idle_tag = 'user' not in self.stats |
||
228 | |||
229 | # Header |
||
230 | msg = '{}'.format('CPU') |
||
231 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
232 | trend_user = self.get_trend('user') |
||
233 | trend_system = self.get_trend('system') |
||
234 | if trend_user is None or trend_user is None: |
||
235 | trend_cpu = None |
||
236 | else: |
||
237 | trend_cpu = trend_user + trend_system |
||
238 | msg = ' {:4}'.format(self.trend_msg(trend_cpu)) |
||
239 | ret.append(self.curse_add_line(msg)) |
||
240 | # Total CPU usage |
||
241 | msg = '{:5.1f}%'.format(self.stats['total']) |
||
242 | if idle_tag: |
||
243 | ret.append(self.curse_add_line( |
||
244 | msg, self.get_views(key='total', option='decoration'))) |
||
245 | else: |
||
246 | ret.append(self.curse_add_line(msg)) |
||
247 | # Nice CPU |
||
248 | if 'nice' in self.stats: |
||
249 | msg = ' {:8}'.format('nice:') |
||
250 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='nice', option='optional'))) |
||
251 | msg = '{:5.1f}%'.format(self.stats['nice']) |
||
252 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='nice', option='optional'))) |
||
253 | # ctx_switches |
||
254 | if 'ctx_switches' in self.stats: |
||
255 | msg = ' {:8}'.format('ctx_sw:') |
||
256 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='ctx_switches', option='optional'))) |
||
257 | msg = '{:>5}'.format(self.auto_unit(int(self.stats['ctx_switches'] // self.stats['time_since_update']), |
||
258 | min_symbol='K')) |
||
259 | ret.append(self.curse_add_line( |
||
260 | msg, self.get_views(key='ctx_switches', option='decoration'), |
||
261 | optional=self.get_views(key='ctx_switches', option='optional'))) |
||
262 | |||
263 | # New line |
||
264 | ret.append(self.curse_new_line()) |
||
265 | # User CPU |
||
266 | View Code Duplication | if 'user' in self.stats: |
|
267 | msg = '{:8}'.format('user:') |
||
268 | ret.append(self.curse_add_line(msg)) |
||
269 | msg = '{:5.1f}%'.format(self.stats['user']) |
||
270 | ret.append(self.curse_add_line( |
||
271 | msg, self.get_views(key='user', option='decoration'))) |
||
272 | elif 'idle' in self.stats: |
||
273 | msg = '{:8}'.format('idle:') |
||
274 | ret.append(self.curse_add_line(msg)) |
||
275 | msg = '{:5.1f}%'.format(self.stats['idle']) |
||
276 | ret.append(self.curse_add_line(msg)) |
||
277 | # IRQ CPU |
||
278 | if 'irq' in self.stats: |
||
279 | msg = ' {:8}'.format('irq:') |
||
280 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='irq', option='optional'))) |
||
281 | msg = '{:5.1f}%'.format(self.stats['irq']) |
||
282 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='irq', option='optional'))) |
||
283 | # interrupts |
||
284 | if 'interrupts' in self.stats: |
||
285 | msg = ' {:8}'.format('inter:') |
||
286 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='interrupts', option='optional'))) |
||
287 | msg = '{:>5}'.format(int(self.stats['interrupts'] // self.stats['time_since_update'])) |
||
288 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='interrupts', option='optional'))) |
||
289 | |||
290 | # New line |
||
291 | ret.append(self.curse_new_line()) |
||
292 | # System CPU |
||
293 | View Code Duplication | if 'system' in self.stats and not idle_tag: |
|
294 | msg = '{:8}'.format('system:') |
||
295 | ret.append(self.curse_add_line(msg)) |
||
296 | msg = '{:5.1f}%'.format(self.stats['system']) |
||
297 | ret.append(self.curse_add_line( |
||
298 | msg, self.get_views(key='system', option='decoration'))) |
||
299 | else: |
||
300 | msg = '{:8}'.format('core:') |
||
301 | ret.append(self.curse_add_line(msg)) |
||
302 | msg = '{:>6}'.format(self.stats['nb_log_core']) |
||
303 | ret.append(self.curse_add_line(msg)) |
||
304 | # IOWait CPU |
||
305 | if 'iowait' in self.stats: |
||
306 | msg = ' {:8}'.format('iowait:') |
||
307 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='iowait', option='optional'))) |
||
308 | msg = '{:5.1f}%'.format(self.stats['iowait']) |
||
309 | ret.append(self.curse_add_line( |
||
310 | msg, self.get_views(key='iowait', option='decoration'), |
||
311 | optional=self.get_views(key='iowait', option='optional'))) |
||
312 | # soft_interrupts |
||
313 | if 'soft_interrupts' in self.stats: |
||
314 | msg = ' {:8}'.format('sw_int:') |
||
315 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='soft_interrupts', option='optional'))) |
||
316 | msg = '{:>5}'.format(int(self.stats['soft_interrupts'] // self.stats['time_since_update'])) |
||
317 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='soft_interrupts', option='optional'))) |
||
318 | |||
319 | # New line |
||
320 | ret.append(self.curse_new_line()) |
||
321 | # Idle CPU |
||
322 | if 'idle' in self.stats and not idle_tag: |
||
323 | msg = '{:8}'.format('idle:') |
||
324 | ret.append(self.curse_add_line(msg)) |
||
325 | msg = '{:5.1f}%'.format(self.stats['idle']) |
||
326 | ret.append(self.curse_add_line(msg)) |
||
327 | # Steal CPU usage |
||
328 | if 'steal' in self.stats: |
||
329 | msg = ' {:8}'.format('steal:') |
||
330 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='steal', option='optional'))) |
||
331 | msg = '{:5.1f}%'.format(self.stats['steal']) |
||
332 | ret.append(self.curse_add_line( |
||
333 | msg, self.get_views(key='steal', option='decoration'), |
||
334 | optional=self.get_views(key='steal', option='optional'))) |
||
335 | # syscalls |
||
336 | # syscalls: number of system calls since boot. Always set to 0 on Linux. (do not display) |
||
337 | if 'syscalls' in self.stats and not LINUX: |
||
338 | msg = ' {:8}'.format('syscal:') |
||
339 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='syscalls', option='optional'))) |
||
340 | msg = '{:>5}'.format(int(self.stats['syscalls'] // self.stats['time_since_update'])) |
||
341 | ret.append(self.curse_add_line(msg, optional=self.get_views(key='syscalls', option='optional'))) |
||
342 | |||
343 | # Return the message with decoration |
||
344 | return ret |
||
345 |