Conditions | 10 |
Total Lines | 94 |
Code Lines | 62 |
Lines | 94 |
Ratio | 100 % |
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.programlist.PluginModel._msg_curse_sum() 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 | # |
||
234 | View Code Duplication | def _msg_curse_sum(self, ret, sep_char='_', mmm=None, args=None): |
|
235 | """ |
||
236 | Build the sum message (only when filter is on) and add it to the ret dict. |
||
237 | |||
238 | :param ret: list of string where the message is added |
||
239 | :param sep_char: define the line separation char |
||
240 | :param mmm: display min, max, mean or current (if mmm=None) |
||
241 | :param args: Glances args |
||
242 | """ |
||
243 | ret.append(self.curse_new_line()) |
||
244 | if mmm is None: |
||
245 | ret.append(self.curse_add_line(sep_char * 69)) |
||
246 | ret.append(self.curse_new_line()) |
||
247 | # CPU percent sum |
||
248 | msg = ' ' |
||
249 | msg += self.layout_stat['cpu'].format(self._sum_stats('cpu_percent', mmm=mmm)) |
||
250 | ret.append(self.curse_add_line(msg, decoration=self._mmm_deco(mmm))) |
||
251 | # MEM percent sum |
||
252 | msg = self.layout_stat['mem'].format(self._sum_stats('memory_percent', mmm=mmm)) |
||
253 | ret.append(self.curse_add_line(msg, decoration=self._mmm_deco(mmm))) |
||
254 | # VIRT and RES memory sum |
||
255 | if ( |
||
256 | 'memory_info' in self.stats[0] |
||
257 | and self.stats[0]['memory_info'] is not None |
||
258 | and self.stats[0]['memory_info'] != '' |
||
259 | ): |
||
260 | # VMS |
||
261 | msg = self.layout_stat['virt'].format( |
||
262 | self.auto_unit(self._sum_stats('memory_info', sub_key='vms', mmm=mmm), low_precision=False) |
||
263 | ) |
||
264 | ret.append(self.curse_add_line(msg, decoration=self._mmm_deco(mmm), optional=True)) |
||
265 | # RSS |
||
266 | msg = self.layout_stat['res'].format( |
||
267 | self.auto_unit(self._sum_stats('memory_info', sub_key='rss', mmm=mmm), low_precision=False) |
||
268 | ) |
||
269 | ret.append(self.curse_add_line(msg, decoration=self._mmm_deco(mmm), optional=True)) |
||
270 | else: |
||
271 | msg = self.layout_header['virt'].format('') |
||
272 | ret.append(self.curse_add_line(msg)) |
||
273 | msg = self.layout_header['res'].format('') |
||
274 | ret.append(self.curse_add_line(msg)) |
||
275 | # PID |
||
276 | msg = self.layout_header['nprocs'].format('') |
||
277 | ret.append(self.curse_add_line(msg)) |
||
278 | # USER |
||
279 | msg = self.layout_header['user'].format('') |
||
280 | ret.append(self.curse_add_line(msg)) |
||
281 | # TIME+ |
||
282 | msg = self.layout_header['time'].format('') |
||
283 | ret.append(self.curse_add_line(msg, optional=True)) |
||
284 | # THREAD |
||
285 | msg = self.layout_header['thread'].format('') |
||
286 | ret.append(self.curse_add_line(msg)) |
||
287 | # NICE |
||
288 | msg = self.layout_header['nice'].format('') |
||
289 | ret.append(self.curse_add_line(msg)) |
||
290 | # STATUS |
||
291 | msg = self.layout_header['status'].format('') |
||
292 | ret.append(self.curse_add_line(msg)) |
||
293 | # IO read/write |
||
294 | if 'io_counters' in self.stats[0] and mmm is None: |
||
295 | # IO read |
||
296 | io_rs = int( |
||
297 | (self._sum_stats('io_counters', 0) - self._sum_stats('io_counters', sub_key=2, mmm=mmm)) |
||
298 | / self.stats[0]['time_since_update'] |
||
299 | ) |
||
300 | if io_rs == 0: |
||
301 | msg = self.layout_stat['ior'].format('0') |
||
302 | else: |
||
303 | msg = self.layout_stat['ior'].format(self.auto_unit(io_rs, low_precision=True)) |
||
304 | ret.append(self.curse_add_line(msg, decoration=self._mmm_deco(mmm), optional=True, additional=True)) |
||
305 | # IO write |
||
306 | io_ws = int( |
||
307 | (self._sum_stats('io_counters', 1) - self._sum_stats('io_counters', sub_key=3, mmm=mmm)) |
||
308 | / self.stats[0]['time_since_update'] |
||
309 | ) |
||
310 | if io_ws == 0: |
||
311 | msg = self.layout_stat['iow'].format('0') |
||
312 | else: |
||
313 | msg = self.layout_stat['iow'].format(self.auto_unit(io_ws, low_precision=True)) |
||
314 | ret.append(self.curse_add_line(msg, decoration=self._mmm_deco(mmm), optional=True, additional=True)) |
||
315 | else: |
||
316 | msg = self.layout_header['ior'].format('') |
||
317 | ret.append(self.curse_add_line(msg, optional=True, additional=True)) |
||
318 | msg = self.layout_header['iow'].format('') |
||
319 | ret.append(self.curse_add_line(msg, optional=True, additional=True)) |
||
320 | if mmm is None: |
||
321 | msg = '< {}'.format('current') |
||
322 | ret.append(self.curse_add_line(msg, optional=True)) |
||
323 | else: |
||
324 | msg = f'< {mmm}' |
||
325 | ret.append(self.curse_add_line(msg, optional=True)) |
||
326 | msg = '(\'M\' to reset)' |
||
327 | ret.append(self.curse_add_line(msg, optional=True)) |
||
328 |