| Conditions | 22 |
| Total Lines | 115 |
| Code Lines | 76 |
| 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.GpuPlugin.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 | # |
||
| 187 | def msg_curse(self, args=None, max_width=None): |
||
| 188 | """Return the dict to display in the curse interface.""" |
||
| 189 | # Init the return message |
||
| 190 | ret = [] |
||
| 191 | |||
| 192 | # Only process if stats exist, not empty (issue #871) and plugin not disabled |
||
| 193 | if not self.stats or (self.stats == []) or self.is_disabled(): |
||
| 194 | return ret |
||
| 195 | |||
| 196 | # Check if all GPU have the same name |
||
| 197 | same_name = all(s['name'] == self.stats[0]['name'] for s in self.stats) |
||
| 198 | |||
| 199 | # gpu_stats contain the first GPU in the list |
||
| 200 | gpu_stats = self.stats[0] |
||
| 201 | |||
| 202 | # Header |
||
| 203 | header = '' |
||
| 204 | if len(self.stats) > 1: |
||
| 205 | header += f'{len(self.stats)}' |
||
| 206 | if same_name: |
||
| 207 | header += ' {}'.format(gpu_stats['name']) |
||
| 208 | else: |
||
| 209 | header += ' GPUs' |
||
| 210 | elif same_name: |
||
| 211 | header += '{}'.format(gpu_stats['name']) |
||
| 212 | else: |
||
| 213 | header += 'GPU' |
||
| 214 | msg = header[:17] |
||
| 215 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
| 216 | |||
| 217 | # Build the string message |
||
| 218 | if len(self.stats) == 1 or args.meangpu: |
||
| 219 | # GPU stat summary or mono GPU |
||
| 220 | # New line |
||
| 221 | ret.append(self.curse_new_line()) |
||
| 222 | # GPU PROC |
||
| 223 | try: |
||
| 224 | mean_proc = sum(s['proc'] for s in self.stats if s is not None) / len(self.stats) |
||
| 225 | except TypeError: |
||
| 226 | mean_proc_msg = '{:>4}'.format('N/A') |
||
| 227 | else: |
||
| 228 | mean_proc_msg = f'{mean_proc:>3.0f}%' |
||
| 229 | if len(self.stats) > 1: |
||
| 230 | msg = '{:13}'.format('proc mean:') |
||
| 231 | else: |
||
| 232 | msg = '{:13}'.format('proc:') |
||
| 233 | ret.append(self.curse_add_line(msg)) |
||
| 234 | ret.append( |
||
| 235 | self.curse_add_line( |
||
| 236 | mean_proc_msg, self.get_views(item=gpu_stats[self.get_key()], key='proc', option='decoration') |
||
| 237 | ) |
||
| 238 | ) |
||
| 239 | # New line |
||
| 240 | ret.append(self.curse_new_line()) |
||
| 241 | # GPU MEM |
||
| 242 | try: |
||
| 243 | mean_mem = sum(s['mem'] for s in self.stats if s is not None) / len(self.stats) |
||
| 244 | except TypeError: |
||
| 245 | mean_mem_msg = '{:>4}'.format('N/A') |
||
| 246 | else: |
||
| 247 | mean_mem_msg = f'{mean_mem:>3.0f}%' |
||
| 248 | if len(self.stats) > 1: |
||
| 249 | msg = '{:13}'.format('mem mean:') |
||
| 250 | else: |
||
| 251 | msg = '{:13}'.format('mem:') |
||
| 252 | ret.append(self.curse_add_line(msg)) |
||
| 253 | ret.append( |
||
| 254 | self.curse_add_line( |
||
| 255 | mean_mem_msg, self.get_views(item=gpu_stats[self.get_key()], key='mem', option='decoration') |
||
| 256 | ) |
||
| 257 | ) |
||
| 258 | # New line |
||
| 259 | ret.append(self.curse_new_line()) |
||
| 260 | # GPU TEMPERATURE |
||
| 261 | try: |
||
| 262 | mean_temperature = sum(s['temperature'] for s in self.stats if s is not None) / len(self.stats) |
||
| 263 | except TypeError: |
||
| 264 | mean_temperature_msg = '{:>4}'.format('N/A') |
||
| 265 | else: |
||
| 266 | unit = 'C' |
||
| 267 | if args.fahrenheit: |
||
| 268 | mean_temperature = to_fahrenheit(mean_temperature) |
||
| 269 | unit = 'F' |
||
| 270 | mean_temperature_msg = f'{mean_temperature:>3.0f}{unit}' |
||
| 271 | if len(self.stats) > 1: |
||
| 272 | msg = '{:13}'.format('temp mean:') |
||
| 273 | else: |
||
| 274 | msg = '{:13}'.format('temperature:') |
||
| 275 | ret.append(self.curse_add_line(msg)) |
||
| 276 | ret.append( |
||
| 277 | self.curse_add_line( |
||
| 278 | mean_temperature_msg, |
||
| 279 | self.get_views(item=gpu_stats[self.get_key()], key='temperature', option='decoration'), |
||
| 280 | ) |
||
| 281 | ) |
||
| 282 | else: |
||
| 283 | # Multi GPU |
||
| 284 | # Temperature is not displayed in this mode... |
||
| 285 | for gpu_stats in self.stats: |
||
| 286 | # New line |
||
| 287 | ret.append(self.curse_new_line()) |
||
| 288 | # GPU ID + PROC + MEM + TEMPERATURE |
||
| 289 | id_msg = '{:>7}'.format(gpu_stats['gpu_id']) |
||
| 290 | try: |
||
| 291 | proc_msg = '{:>3.0f}%'.format(gpu_stats['proc']) |
||
| 292 | except (ValueError, TypeError): |
||
| 293 | proc_msg = '{:>4}'.format('N/A') |
||
| 294 | try: |
||
| 295 | mem_msg = '{:>3.0f}%'.format(gpu_stats['mem']) |
||
| 296 | except (ValueError, TypeError): |
||
| 297 | mem_msg = '{:>4}'.format('N/A') |
||
| 298 | msg = f'{id_msg} {proc_msg} mem {mem_msg}' |
||
| 299 | ret.append(self.curse_add_line(msg)) |
||
| 300 | |||
| 301 | return ret |
||
| 302 |