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