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