| Conditions | 11 |
| Total Lines | 58 |
| Code Lines | 43 |
| 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 -*- |
||
| 89 | def update(self, |
||
| 90 | stats, |
||
| 91 | duration=3): |
||
| 92 | """Display issue |
||
| 93 | """ |
||
| 94 | self.print_version() |
||
| 95 | |||
| 96 | for plugin in sorted(stats._plugins): |
||
| 97 | if stats._plugins[plugin].is_disabled(): |
||
| 98 | continue |
||
| 99 | try: |
||
| 100 | # Update the stats |
||
| 101 | stats._plugins[plugin].update() |
||
| 102 | except Exception as e: |
||
| 103 | pass |
||
| 104 | |||
| 105 | time.sleep(3) |
||
| 106 | |||
| 107 | for plugin in sorted(stats._plugins): |
||
| 108 | if stats._plugins[plugin].is_disabled(): |
||
| 109 | # If current plugin is disable |
||
| 110 | # then continue to next plugin |
||
| 111 | result = colors.NO + '[N/A]'.rjust(19 - len(plugin)) |
||
| 112 | message = colors.NO |
||
| 113 | self.print_issue(plugin, result, message) |
||
| 114 | continue |
||
| 115 | # Start the counter |
||
| 116 | counter = Counter() |
||
| 117 | counter.reset() |
||
| 118 | stat = None |
||
| 119 | stat_error = None |
||
| 120 | try: |
||
| 121 | # Update the stats |
||
| 122 | stats._plugins[plugin].update() |
||
| 123 | # Get the stats |
||
| 124 | stat = stats.get_plugin(plugin).get_export() |
||
| 125 | except Exception as e: |
||
| 126 | stat_error = e |
||
| 127 | if stat_error is None: |
||
| 128 | result = (colors.GREEN + |
||
| 129 | '[OK] ' + |
||
| 130 | colors.BLUE + |
||
| 131 | ' {:.5f}s '.format(counter.get())).rjust(41 - len(plugin)) |
||
| 132 | if isinstance(stat, list) and len(stat) > 0 and 'key' in stat[0]: |
||
| 133 | key = 'key={} '.format(stat[0]['key']) |
||
| 134 | message = colors.ORANGE + key + colors.NO + str(stat)[0:TERMINAL_WIDTH-41-len(key)] |
||
| 135 | else: |
||
| 136 | message = colors.NO + str(stat)[0:TERMINAL_WIDTH-41] |
||
| 137 | else: |
||
| 138 | result = (colors.RED + |
||
| 139 | '[ERROR]' + |
||
| 140 | colors.BLUE + |
||
| 141 | ' {:.5f}s '.format(counter.get())).rjust(41 - len(plugin)) |
||
| 142 | message = colors.NO + str(stat_error)[0:TERMINAL_WIDTH-41] |
||
| 143 | self.print_issue(plugin, result, message) |
||
| 144 | |||
| 145 | # Return True to exit directly (no refresh) |
||
| 146 | return True |
||
| 147 |