| Conditions | 11 |
| Total Lines | 68 |
| Code Lines | 48 |
| 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_processcount.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 -*- |
||
| 91 | def msg_curse(self, args=None, max_width=None): |
||
| 92 | """Return the dict to display in the curse interface.""" |
||
| 93 | # Init the return message |
||
| 94 | ret = [] |
||
| 95 | |||
| 96 | # Only process if stats exist and display plugin enable... |
||
| 97 | if args.disable_process: |
||
| 98 | msg = "PROCESSES DISABLED (press 'z' to display)" |
||
| 99 | ret.append(self.curse_add_line(msg)) |
||
| 100 | return ret |
||
| 101 | |||
| 102 | if not self.stats: |
||
| 103 | return ret |
||
| 104 | |||
| 105 | # Display the filter (if it exists) |
||
| 106 | if glances_processes.process_filter is not None: |
||
| 107 | msg = 'Processes filter:' |
||
| 108 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 109 | msg = ' {} '.format(glances_processes.process_filter) |
||
| 110 | if glances_processes.process_filter_key is not None: |
||
| 111 | msg += 'on column {} '.format(glances_processes.process_filter_key) |
||
| 112 | ret.append(self.curse_add_line(msg, "FILTER")) |
||
| 113 | msg = '(\'ENTER\' to edit, \'E\' to reset)' |
||
| 114 | ret.append(self.curse_add_line(msg)) |
||
| 115 | ret.append(self.curse_new_line()) |
||
| 116 | |||
| 117 | # Build the string message |
||
| 118 | # Header |
||
| 119 | msg = 'TASKS' |
||
| 120 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 121 | # Compute processes |
||
| 122 | other = self.stats['total'] |
||
| 123 | msg = '{:>4}'.format(self.stats['total']) |
||
| 124 | ret.append(self.curse_add_line(msg)) |
||
| 125 | |||
| 126 | if 'thread' in self.stats: |
||
| 127 | msg = ' ({} thr),'.format(self.stats['thread']) |
||
| 128 | ret.append(self.curse_add_line(msg)) |
||
| 129 | |||
| 130 | if 'running' in self.stats: |
||
| 131 | other -= self.stats['running'] |
||
| 132 | msg = ' {} run,'.format(self.stats['running']) |
||
| 133 | ret.append(self.curse_add_line(msg)) |
||
| 134 | |||
| 135 | if 'sleeping' in self.stats: |
||
| 136 | other -= self.stats['sleeping'] |
||
| 137 | msg = ' {} slp,'.format(self.stats['sleeping']) |
||
| 138 | ret.append(self.curse_add_line(msg)) |
||
| 139 | |||
| 140 | msg = ' {} oth '.format(other) |
||
| 141 | ret.append(self.curse_add_line(msg)) |
||
| 142 | |||
| 143 | # Display sort information |
||
| 144 | msg = 'Programs' if self.args.programs else 'Threads' |
||
| 145 | try: |
||
| 146 | sort_human = self.sort_for_human[glances_processes.sort_key] |
||
| 147 | except KeyError: |
||
| 148 | sort_human = glances_processes.sort_key |
||
| 149 | if glances_processes.auto_sort: |
||
| 150 | msg += ' sorted automatically' |
||
| 151 | ret.append(self.curse_add_line(msg)) |
||
| 152 | msg = ' by {}'.format(sort_human) |
||
| 153 | else: |
||
| 154 | msg += ' sorted by {}'.format(sort_human) |
||
| 155 | ret.append(self.curse_add_line(msg)) |
||
| 156 | |||
| 157 | # Return the message with decoration |
||
| 158 | return ret |
||
| 159 |