Conditions | 15 |
Total Lines | 75 |
Code Lines | 42 |
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.glances_memswap.Plugin.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 -*- |
||
59 | @GlancesPlugin._check_decorator |
||
60 | @GlancesPlugin._log_result_decorator |
||
61 | def update(self): |
||
62 | """Update swap memory stats using the input method.""" |
||
63 | # Init new stats |
||
64 | stats = self.get_init_value() |
||
65 | |||
66 | if self.input_method == 'local': |
||
67 | # Update stats using the standard system lib |
||
68 | # Grab SWAP using the psutil swap_memory method |
||
69 | try: |
||
70 | sm_stats = psutil.swap_memory() |
||
71 | except RuntimeError: |
||
72 | # Crash on startup on Illumos when no swap is configured #1767 |
||
73 | pass |
||
74 | else: |
||
75 | # Get all the swap stats (copy/paste of the psutil documentation) |
||
76 | # total: total swap memory in bytes |
||
77 | # used: used swap memory in bytes |
||
78 | # free: free swap memory in bytes |
||
79 | # percent: the percentage usage |
||
80 | # sin: the number of bytes the system has swapped in from disk (cumulative) |
||
81 | # sout: the number of bytes the system has swapped out from disk |
||
82 | # (cumulative) |
||
83 | for swap in ['total', 'used', 'free', 'percent', |
||
84 | 'sin', 'sout']: |
||
85 | if hasattr(sm_stats, swap): |
||
86 | stats[swap] = getattr(sm_stats, swap) |
||
87 | elif self.input_method == 'snmp': |
||
88 | # Update stats using SNMP |
||
89 | if self.short_system_name == 'windows': |
||
90 | # Mem stats for Windows OS are stored in the FS table |
||
91 | try: |
||
92 | fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name], |
||
93 | bulk=True) |
||
94 | except KeyError: |
||
95 | self.reset() |
||
96 | else: |
||
97 | for fs in fs_stat: |
||
98 | # The virtual memory concept is used by the operating |
||
99 | # system to extend (virtually) the physical memory and |
||
100 | # thus to run more programs by swapping unused memory |
||
101 | # zone (page) to a disk file. |
||
102 | if fs == 'Virtual Memory': |
||
103 | stats['total'] = int( |
||
104 | fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit']) |
||
105 | stats['used'] = int( |
||
106 | fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit']) |
||
107 | stats['percent'] = float( |
||
108 | stats['used'] * 100 / stats['total']) |
||
109 | stats['free'] = stats['total'] - stats['used'] |
||
110 | break |
||
111 | else: |
||
112 | stats = self.get_stats_snmp(snmp_oid=snmp_oid['default']) |
||
113 | |||
114 | if stats['total'] == '': |
||
115 | self.reset() |
||
116 | return stats |
||
117 | |||
118 | for key in iterkeys(stats): |
||
119 | if stats[key] != '': |
||
120 | stats[key] = float(stats[key]) * 1024 |
||
121 | |||
122 | # used=total-free |
||
123 | stats['used'] = stats['total'] - stats['free'] |
||
124 | |||
125 | # percent: the percentage usage calculated as (total - |
||
126 | # available) / total * 100. |
||
127 | stats['percent'] = float( |
||
128 | (stats['total'] - stats['free']) / stats['total'] * 100) |
||
129 | |||
130 | # Update the stats |
||
131 | self.stats = stats |
||
132 | |||
133 | return self.stats |
||
134 | |||
187 |