Skip to content

Commit faa312a

Browse files
Marc Hartmayerbonzini
authored andcommitted
tools/kvm_stat: simplify the sortkey function
The 'sortkey' function references a value in its enclosing scope (closure). This is not common practice for a sort key function so let's replace it. Additionally, the function 'sorted' has already a parameter for reversing the result therefore the inversion of the values is unneeded. The check for stats[x][1] is also superfluous as it's ensured that this value is initialized with 0. Signed-off-by: Marc Hartmayer <mhartmay@linux.vnet.ibm.com> Tested-by: Stefan Raspl <raspl@linux.vnet.ibm.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 95e057e commit faa312a

1 file changed

Lines changed: 8 additions & 15 deletions

File tree

‎tools/kvm/kvm_stat/kvm_stat‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,30 +1080,23 @@ class Tui(object):
10801080
self.screen.move(row, 0)
10811081
self.screen.clrtobot()
10821082
stats = self.stats.get(self._display_guests)
1083-
1084-
def sortCurAvg(x):
1085-
# sort by current events if available
1086-
if stats[x][1]:
1087-
return (-stats[x][1], -stats[x][0])
1088-
else:
1089-
return (0, -stats[x][0])
1090-
1091-
def sortTotal(x):
1092-
# sort by totals
1093-
return (0, -stats[x][0])
10941083
total = 0.
10951084
for key in stats.keys():
10961085
if key.find('(') is -1:
10971086
total += stats[key][0]
10981087
if self._sorting == SORT_DEFAULT:
1099-
sortkey = sortCurAvg
1088+
def sortkey((_k, v)):
1089+
# sort by (delta value, overall value)
1090+
return (v[1], v[0])
11001091
else:
1101-
sortkey = sortTotal
1092+
def sortkey((_k, v)):
1093+
# sort by overall value
1094+
return v[0]
1095+
11021096
tavg = 0
1103-
for key in sorted(stats.keys(), key=sortkey):
1097+
for key, values in sorted(stats.items(), key=sortkey, reverse=True):
11041098
if row >= self.screen.getmaxyx()[0] - 1:
11051099
break
1106-
values = stats[key]
11071100
if not values[0] and not values[1]:
11081101
break
11091102
if values[0] is not None:

0 commit comments

Comments
 (0)