| Conditions | 13 |
| Total Lines | 66 |
| Code Lines | 45 |
| 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.outputs.glances_stdout_issue.GlancesStdoutIssue.update() 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 -*- |
||
| 71 | def update(self, stats, duration=3): |
||
| 72 | """Display issue""" |
||
| 73 | self.print_version() |
||
| 74 | |||
| 75 | for plugin in sorted(stats._plugins): |
||
| 76 | if stats._plugins[plugin].is_disabled(): |
||
| 77 | continue |
||
| 78 | try: |
||
| 79 | # Update the stats |
||
| 80 | stats._plugins[plugin].update() |
||
| 81 | except Exception: |
||
| 82 | pass |
||
| 83 | |||
| 84 | time.sleep(2) |
||
| 85 | |||
| 86 | counter_total = Counter() |
||
| 87 | for plugin in sorted(stats._plugins): |
||
| 88 | if stats._plugins[plugin].is_disabled(): |
||
| 89 | # If current plugin is disable |
||
| 90 | # then continue to next plugin |
||
| 91 | result = colors.NO + '[NA]'.rjust(18 - len(plugin)) |
||
| 92 | message = colors.NO |
||
| 93 | self.print_issue(plugin, result, message) |
||
| 94 | continue |
||
| 95 | # Start the counter |
||
| 96 | counter = Counter() |
||
| 97 | counter.reset() |
||
| 98 | stat = None |
||
| 99 | stat_error = None |
||
| 100 | try: |
||
| 101 | # Update the stats |
||
| 102 | stats._plugins[plugin].update() |
||
| 103 | # Get the stats |
||
| 104 | stat = stats.get_plugin(plugin).get_export() |
||
| 105 | # Hide private information |
||
| 106 | if plugin == 'ip': |
||
| 107 | for key in stat.keys(): |
||
| 108 | stat[key] = '***' |
||
| 109 | except Exception as e: |
||
| 110 | stat_error = e |
||
| 111 | if stat_error is None: |
||
| 112 | result = (colors.GREEN + '[OK] ' + colors.BLUE + ' {:.5f}s '.format(counter.get())).rjust( |
||
| 113 | 41 - len(plugin) |
||
| 114 | ) |
||
| 115 | if isinstance(stat, list) and len(stat) > 0 and 'key' in stat[0]: |
||
| 116 | key = 'key={} '.format(stat[0]['key']) |
||
| 117 | stat_output = pprint.pformat([stat[0]], compact=True, width=120, depth=3) |
||
| 118 | message = colors.ORANGE + key + colors.NO + '\n' + stat_output[0:-1] + ', ...' + stat_output[-1] |
||
| 119 | else: |
||
| 120 | message = '\n' + colors.NO + pprint.pformat(stat, compact=True, width=120, depth=2) |
||
| 121 | else: |
||
| 122 | result = (colors.RED + '[ERROR]' + colors.BLUE + ' {:.5f}s '.format(counter.get())).rjust( |
||
| 123 | 41 - len(plugin) |
||
| 124 | ) |
||
| 125 | message = colors.NO + str(stat_error)[0 : TERMINAL_WIDTH - 41] |
||
| 126 | |||
| 127 | # Display the result |
||
| 128 | self.print_issue(plugin, result, message) |
||
| 129 | |||
| 130 | # Display total time need to update all plugins |
||
| 131 | sys.stdout.write('=' * TERMINAL_WIDTH + '\n') |
||
| 132 | print("Total time to update all stats: {}{:.5f}s{}".format(colors.BLUE, counter_total.get(), colors.NO)) |
||
| 133 | sys.stdout.write('=' * TERMINAL_WIDTH + '\n') |
||
| 134 | |||
| 135 | # Return True to exit directly (no refresh) |
||
| 136 | return True |
||
| 137 |