Skip to content

Commit

Permalink
Use key parameter in sort() for sorting purposes
Browse files Browse the repository at this point in the history
Signed-off-by: Qin Zhang (张琴) <qin.zhang@citrix.com>
  • Loading branch information
qinz0822 committed Nov 3, 2023
1 parent c0c47ac commit eb7eccc
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion XSConsoleData.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def convertPIF(inPIF):
pif['metrics']['device_name'] = Lang('<Unknown>')

# Sort PIFs by device name for consistent order
self.data['host']['PIFs'].sort(lambda x, y : cmp(x['device'], y['device']))
self.data['host']['PIFs'].sort(key=lambda pif: pif['device'])

def convertVBD(inVBD):
retVBD = self.session.xenapi.VBD.get_record(inVBD)
Expand Down
4 changes: 2 additions & 2 deletions XSConsoleDataUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def DeviceList(cls, inWritableOnly):
name = "%-50s%10.10s%10.10s" % (nameDesc[:50], nameLabel[:10], nameSize[:10])
retVal.append(Struct(name = name, vdi = vdi))

retVal.sort(lambda x, y : cmp(x.vdi['name_label'], y.vdi['name_label']))
retVal.sort(key=lambda data: data.vdi['name_label'])

return retVal

Expand Down Expand Up @@ -466,7 +466,7 @@ def SRList(cls, inMode = None, inCapabilities = None):
dataSR['opaqueref'] = sr.HotOpaqueRef().OpaqueRef()
retVal.append( Struct(name = name, sr = dataSR) )

retVal.sort(lambda x, y : cmp(x.name, y.name))
retVal.sort(key=lambda data: data.name)

return retVal

Expand Down
6 changes: 1 addition & 5 deletions XSConsoleImporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,8 @@ def ActivateNamedPlugIn(cls, inName, *inParams):
@classmethod
def CallReadyHandlers(cls):
# Sort plugins in descending priority order with a default of 1000
def CmpPlugin(x, y):
return cmp(y.get('readyhandlerpriority', 1000),
x.get('readyhandlerpriority', 1000))

plugins = cls.plugIns.values()
plugins.sort(CmpPlugin)
plugins.sort(key=lambda p: p.get('readyhandlerpriority', 1000), reverse=True)

for plugin in plugins:
handler = plugin.get('readyhandler', None)
Expand Down
2 changes: 1 addition & 1 deletion XSConsoleMenus.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def AddChoiceDef(self, inChoiceDef, inPriority = None):
inChoiceDef.priority = priority # FIXME (modifies input parameter)
self.choiceDefs.append(inChoiceDef)

self.choiceDefs.sort(lambda x, y : cmp(x.priority, y.priority))
self.choiceDefs.sort(key=lambda c: c.priority)

def AddChoice(self, name, onAction = None, onEnter = None, priority = None, statusUpdateHandler = None, handle = None):
choiceDef = ChoiceDef(name, onAction, onEnter, priority, statusUpdateHandler, handle)
Expand Down
2 changes: 1 addition & 1 deletion plugins-base/XSFeatureSRCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __init__(self, inSRHandle):

choiceList = [ name for name in allowedOps if name in SRUtils.AllowedOperations() ]

choiceList.sort(lambda x, y: cmp(SRUtils.OperationPriority(x), SRUtils.OperationPriority(y)))
choiceList.sort(key=lambda choice: SRUtils.OperationPriority(choice))

self.controlMenu = Menu()
for choice in choiceList:
Expand Down
2 changes: 1 addition & 1 deletion plugins-base/XSFeatureSRInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def MenuRegenerator(cls, inList, inMenu):
srList = [ sr for sr in HotAccessor().visible_sr if sr.other_config({}).get('xensource_internal', '') != 'true' ]

# Sort list by SR shared flag then name
srList.sort(lambda x, y: cmp(y.shared(False), x.shared(False)) or cmp (x.name_label(''), y.name_label()))
srList.sort(key=lambda sr: (-sr.shared(False), sr.name_label('')))

srUtils = Importer.GetResource('SRUtils')
for sr in srList:
Expand Down
4 changes: 2 additions & 2 deletions plugins-base/XSFeatureVMCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(self, inVMHandle):

choiceList = [ name for name in allowedOps if name in VMUtils.AllowedOperations() ]

choiceList.sort(lambda x, y: cmp(VMUtils.OperationPriority(x), VMUtils.OperationPriority(y)))
choiceList.sort(key=lambda choice: VMUtils.OperationPriority(choice))

self.controlMenu = Menu()
for choice in choiceList:
Expand All @@ -146,7 +146,7 @@ def BuildPane(self):

if self.state == 'MIGRATE':
hosts = VMUtils.GetPossibleHostAccessors(self.vmHandle)
hosts.sort(lambda x, y: cmp(x.name_label(), y.name_label()))
hosts.sort(key=lambda host: host.name_label())
self.hostMenu = Menu()
residentHost = HotAccessor().vm[self.vmHandle].resident_on()
for host in hosts:
Expand Down
2 changes: 1 addition & 1 deletion plugins-base/XSFeatureVMInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def MenuRegenerator(cls, inList, inMenu):
# inList is a list of HotOpaqueRef objects
vmList = [ HotAccessor().vm[x] for x in inList ]
# Sort list by VM name
vmList.sort(lambda x,y: cmp(x.name_label(''), y.name_label('')))
vmList.sort(key=lambda vm: vm.name_label(''))

for vm in vmList:
nameLabel = vm.name_label(Lang('<Unknown>'))
Expand Down

0 comments on commit eb7eccc

Please sign in to comment.