Conditions | 8 |
Total Lines | 91 |
Code Lines | 65 |
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:
1 | # -*- coding: utf-8 -*- |
||
156 | def msg_curse(self, args=None, max_width=None): |
||
157 | """Return the list to display in the curse interface.""" |
||
158 | # Init the return message |
||
159 | ret = [] |
||
160 | |||
161 | # Build the header message |
||
162 | ret.append(self.curse_add_line(self.view_data['version'], 'TITLE')) |
||
163 | ret.append(self.curse_add_line(self.view_data['psutil_version'])) |
||
164 | ret.append(self.curse_new_line()) |
||
165 | |||
166 | # Build the configuration file path |
||
167 | if 'configuration_file' in self.view_data: |
||
168 | ret.append(self.curse_add_line(self.view_data['configuration_file'])) |
||
169 | ret.append(self.curse_new_line()) |
||
170 | |||
171 | ret.append(self.curse_new_line()) |
||
172 | |||
173 | # key-shortcuts |
||
174 | # |
||
175 | # Collect all values after the 1st key-msg |
||
176 | # in a list of curse-lines. |
||
177 | # |
||
178 | shortcuts = [] |
||
179 | collecting = False |
||
180 | for k, v in iteritems(self.view_data): |
||
181 | if collecting: |
||
182 | pass |
||
183 | elif k == 'header_sort': |
||
184 | collecting = True |
||
185 | else: |
||
186 | continue |
||
187 | shortcuts.append(self.curse_add_line(v)) |
||
188 | # Divide shortcuts into 2 columns |
||
189 | # and if number of schortcuts is even, |
||
190 | # make the 1st column taller (len+1). |
||
191 | # |
||
192 | nlines = (len(shortcuts) + 1) // 2 |
||
193 | ret.extend( |
||
194 | msg |
||
195 | for triplet in zip( |
||
196 | iter(shortcuts[:nlines]), |
||
197 | chain(shortcuts[nlines:], iter(lambda: self.curse_add_line(''), None)), |
||
198 | iter(self.curse_new_line, None), |
||
199 | ) |
||
200 | for msg in triplet |
||
201 | ) |
||
202 | |||
203 | ret.append(self.curse_new_line()) |
||
204 | ret.append(self.curse_add_line('For an exhaustive list of key bindings:')) |
||
205 | ret.append(self.curse_new_line()) |
||
206 | ret.append(self.curse_add_line('https://glances.readthedocs.io/en/latest/cmds.html#interactive-commands')) |
||
207 | ret.append(self.curse_new_line()) |
||
208 | |||
209 | ret.append(self.curse_new_line()) |
||
210 | ret.append(self.curse_add_line('Colors binding:')) |
||
211 | ret.append(self.curse_new_line()) |
||
212 | for c in [ |
||
213 | 'DEFAULT', |
||
214 | 'UNDERLINE', |
||
215 | 'BOLD', |
||
216 | 'SORT', |
||
217 | 'OK', |
||
218 | 'MAX', |
||
219 | 'FILTER', |
||
220 | 'TITLE', |
||
221 | 'PROCESS', |
||
222 | 'PROCESS_SELECTED', |
||
223 | 'STATUS', |
||
224 | 'NICE', |
||
225 | 'CPU_TIME', |
||
226 | 'CAREFUL', |
||
227 | 'WARNING', |
||
228 | 'CRITICAL', |
||
229 | 'OK_LOG', |
||
230 | 'CAREFUL_LOG', |
||
231 | 'WARNING_LOG', |
||
232 | 'CRITICAL_LOG', |
||
233 | 'PASSWORD', |
||
234 | 'SELECTED', |
||
235 | 'INFO', |
||
236 | 'ERROR', |
||
237 | 'SEPARATOR', |
||
238 | ]: |
||
239 | ret.append(self.curse_add_line(c, decoration=c)) |
||
240 | if c == 'CPU_TIME': |
||
241 | ret.append(self.curse_new_line()) |
||
242 | else: |
||
243 | ret.append(self.curse_add_line(' ')) |
||
244 | |||
245 | # Return the message with decoration |
||
246 | return ret |
||
247 |