| Conditions | 21 |
| Total Lines | 111 |
| Code Lines | 73 |
| 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.gpu.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 -*- |
||
| 153 | def msg_curse(self, args=None, max_width=None): |
||
| 154 | """Return the dict to display in the curse interface.""" |
||
| 155 | # Init the return message |
||
| 156 | ret = [] |
||
| 157 | |||
| 158 | # Only process if stats exist, not empty (issue #871) and plugin not disabled |
||
| 159 | if not self.stats or (self.stats == []) or self.is_disabled(): |
||
| 160 | return ret |
||
| 161 | |||
| 162 | # Check if all GPU have the same name |
||
| 163 | same_name = all(s['name'] == self.stats[0]['name'] for s in self.stats) |
||
| 164 | |||
| 165 | # gpu_stats contain the first GPU in the list |
||
| 166 | gpu_stats = self.stats[0] |
||
| 167 | |||
| 168 | # Header |
||
| 169 | header = '' |
||
| 170 | if len(self.stats) > 1: |
||
| 171 | header += '{} '.format(len(self.stats)) |
||
| 172 | if same_name: |
||
| 173 | header += '{} {}'.format('GPU', gpu_stats['name']) |
||
| 174 | else: |
||
| 175 | header += '{}'.format('GPU') |
||
| 176 | msg = header[:17] |
||
| 177 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 178 | |||
| 179 | # Build the string message |
||
| 180 | if len(self.stats) == 1 or args.meangpu: |
||
| 181 | # GPU stat summary or mono GPU |
||
| 182 | # New line |
||
| 183 | ret.append(self.curse_new_line()) |
||
| 184 | # GPU PROC |
||
| 185 | try: |
||
| 186 | mean_proc = sum(s['proc'] for s in self.stats if s is not None) / len(self.stats) |
||
| 187 | except TypeError: |
||
| 188 | mean_proc_msg = '{:>4}'.format('N/A') |
||
| 189 | else: |
||
| 190 | mean_proc_msg = '{:>3.0f}%'.format(mean_proc) |
||
| 191 | if len(self.stats) > 1: |
||
| 192 | msg = '{:13}'.format('proc mean:') |
||
| 193 | else: |
||
| 194 | msg = '{:13}'.format('proc:') |
||
| 195 | ret.append(self.curse_add_line(msg)) |
||
| 196 | ret.append( |
||
| 197 | self.curse_add_line( |
||
| 198 | mean_proc_msg, self.get_views(item=gpu_stats[self.get_key()], key='proc', option='decoration') |
||
| 199 | ) |
||
| 200 | ) |
||
| 201 | # New line |
||
| 202 | ret.append(self.curse_new_line()) |
||
| 203 | # GPU MEM |
||
| 204 | try: |
||
| 205 | mean_mem = sum(s['mem'] for s in self.stats if s is not None) / len(self.stats) |
||
| 206 | except TypeError: |
||
| 207 | mean_mem_msg = '{:>4}'.format('N/A') |
||
| 208 | else: |
||
| 209 | mean_mem_msg = '{:>3.0f}%'.format(mean_mem) |
||
| 210 | if len(self.stats) > 1: |
||
| 211 | msg = '{:13}'.format('mem mean:') |
||
| 212 | else: |
||
| 213 | msg = '{:13}'.format('mem:') |
||
| 214 | ret.append(self.curse_add_line(msg)) |
||
| 215 | ret.append( |
||
| 216 | self.curse_add_line( |
||
| 217 | mean_mem_msg, self.get_views(item=gpu_stats[self.get_key()], key='mem', option='decoration') |
||
| 218 | ) |
||
| 219 | ) |
||
| 220 | # New line |
||
| 221 | ret.append(self.curse_new_line()) |
||
| 222 | # GPU TEMPERATURE |
||
| 223 | try: |
||
| 224 | mean_temperature = sum(s['temperature'] for s in self.stats if s is not None) / len(self.stats) |
||
| 225 | except TypeError: |
||
| 226 | mean_temperature_msg = '{:>4}'.format('N/A') |
||
| 227 | else: |
||
| 228 | unit = 'C' |
||
| 229 | if args.fahrenheit: |
||
| 230 | mean_temperature = to_fahrenheit(mean_temperature) |
||
| 231 | unit = 'F' |
||
| 232 | mean_temperature_msg = '{:>3.0f}{}'.format(mean_temperature, unit) |
||
| 233 | if len(self.stats) > 1: |
||
| 234 | msg = '{:13}'.format('temp mean:') |
||
| 235 | else: |
||
| 236 | msg = '{:13}'.format('temperature:') |
||
| 237 | ret.append(self.curse_add_line(msg)) |
||
| 238 | ret.append( |
||
| 239 | self.curse_add_line( |
||
| 240 | mean_temperature_msg, |
||
| 241 | self.get_views(item=gpu_stats[self.get_key()], key='temperature', option='decoration'), |
||
| 242 | ) |
||
| 243 | ) |
||
| 244 | else: |
||
| 245 | # Multi GPU |
||
| 246 | # Temperature is not displayed in this mode... |
||
| 247 | for gpu_stats in self.stats: |
||
| 248 | # New line |
||
| 249 | ret.append(self.curse_new_line()) |
||
| 250 | # GPU ID + PROC + MEM + TEMPERATURE |
||
| 251 | id_msg = '{}'.format(gpu_stats['gpu_id']) |
||
| 252 | try: |
||
| 253 | proc_msg = '{:>3.0f}%'.format(gpu_stats['proc']) |
||
| 254 | except (ValueError, TypeError): |
||
| 255 | proc_msg = '{:>4}'.format('N/A') |
||
| 256 | try: |
||
| 257 | mem_msg = '{:>3.0f}%'.format(gpu_stats['mem']) |
||
| 258 | except (ValueError, TypeError): |
||
| 259 | mem_msg = '{:>4}'.format('N/A') |
||
| 260 | msg = '{}: {} mem: {}'.format(id_msg, proc_msg, mem_msg) |
||
| 261 | ret.append(self.curse_add_line(msg)) |
||
| 262 | |||
| 263 | return ret |
||
| 264 | |||
| 348 |