Use operator.itemgetter instead of lambda when possible

This commit is contained in:
desbma 2014-12-11 13:47:57 +01:00 committed by Alessio Sergi
parent 7cf0a31997
commit 58ad1e7dff
5 changed files with 15 additions and 8 deletions

View File

@ -19,6 +19,7 @@
# Import Python lib
import collections
import operator
import re
# Import psutil
@ -709,11 +710,11 @@ class GlancesProcesses(object):
process[sortedby][3],
reverse=sortedreverse)
except Exception:
self.processlist.sort(key=lambda process: process['cpu_percent'],
self.processlist.sort(key=operator.itemgetter('cpu_percent'),
reverse=sortedreverse)
else:
# Others sorts
self.processlist.sort(key=lambda process: process[sortedby],
self.processlist.sort(key=operator.itemgetter(sortedby),
reverse=sortedreverse)
return self.processlist

View File

@ -19,6 +19,8 @@
"""Disk I/O plugin."""
import operator
import psutil
# Import Glances libs
@ -134,7 +136,7 @@ class Plugin(GlancesPlugin):
msg = '{0:>7}'.format(_("W/s"))
ret.append(self.curse_add_line(msg))
# Disk list (sorted by name)
for i in sorted(self.stats, key=lambda diskio: diskio['disk_name']):
for i in sorted(self.stats, key=operator.itemgetter('disk_name')):
# Do not display hidden interfaces
if self.is_hide(i['disk_name']):
continue

View File

@ -19,6 +19,8 @@
"""File system plugin."""
import operator
import psutil
from glances.plugins.glances_plugin import GlancesPlugin
@ -181,7 +183,7 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg))
# Disk list (sorted by name)
for i in sorted(self.stats, key=lambda fs: fs['mnt_point']):
for i in sorted(self.stats, key=operator.itemgetter('mnt_point')):
# New line
ret.append(self.curse_new_line())
if i['device_name'] == '' or i['device_name'] == 'none':

View File

@ -20,6 +20,7 @@
"""Network plugin."""
import base64
import operator
import psutil
@ -223,7 +224,7 @@ class Plugin(GlancesPlugin):
msg = '{0:>7}'.format(_("Tx/s"))
ret.append(self.curse_add_line(msg))
# Interface list (sorted by name)
for i in sorted(self.stats, key=lambda network: network['interface_name']):
for i in sorted(self.stats, key=operator.itemgetter('interface_name')):
# Do not display hidden interfaces
if self.is_hide(i['interface_name']):
continue

View File

@ -20,6 +20,7 @@
"""Process list plugin."""
# Import sys libs
import operator
import os
from datetime import timedelta
@ -429,7 +430,7 @@ class Plugin(GlancesPlugin):
process[sortedby][3],
reverse=sortedreverse)
except Exception:
self.stats.sort(key=lambda process: process['cpu_percent'],
self.stats.sort(key=operator.itemgetter('cpu_percent'),
reverse=sortedreverse)
else:
# Others sorts
@ -437,10 +438,10 @@ class Plugin(GlancesPlugin):
self.stats.set_sorting(sortedby, sortedreverse)
else:
try:
self.stats.sort(key=lambda process: process[sortedby],
self.stats.sort(key=operator.itemgetter(sortedby),
reverse=sortedreverse)
except (KeyError, TypeError):
self.stats.sort(key=lambda process: process['name'],
self.stats.sort(key=operator.itemgetter('name'),
reverse=False)
return self.stats