faster sorting syntax in python, try/except for py 2.3 backwards compat

ls.sort(key = lambda v: v.foo)
rather then
  ls.sort(lambda a,b: cmp(a.foo, b.foo))
This commit is contained in:
Campbell Barton 2006-12-14 14:53:32 +00:00
parent 4b99060cc3
commit b995593eee
8 changed files with 52 additions and 18 deletions

@ -162,7 +162,9 @@ def convexHull(point_list_2d):
# Get a local list copy of the points and sort them lexically. # Get a local list copy of the points and sort them lexically.
points = [(p, i) for i, p in enumerate(point_list_2d)] points = [(p, i) for i, p in enumerate(point_list_2d)]
points.sort(lambda a,b: cmp((a[0].x, a[0].y), (b[0].x, b[0].y)))
try: points.sort(key = lambda a: (a[0].x, a[0].y))
except: points.sort(lambda a,b: cmp((a[0].x, a[0].y), (b[0].x, b[0].y)))
# Build upper half of the hull. # Build upper half of the hull.
upper = [points[0], points[1]] # cant remove these. upper = [points[0], points[1]] # cant remove these.

@ -498,7 +498,9 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
# END BOUNDRY. Can remove # END BOUNDRY. Can remove
# sort by collapse weight # sort by collapse weight
collapse_edges.sort(lambda ced1, ced2: cmp(ced1.collapse_weight, ced2.collapse_weight)) # edges will be used for sorting try: collapse_edges.sort(key = lambda ced: ced.collapse_weight) # edges will be used for sorting
except: collapse_edges.sort(lambda ced1, ced2: cmp(ced1.collapse_weight, ced2.collapse_weight)) # edges will be used for sorting
vert_collapsed= [0]*len(verts) vert_collapsed= [0]*len(verts)

@ -63,7 +63,8 @@ class vertList:
that use too much that use too much
Lambada based sort Lambada based sort
''' '''
self.verts.sort(lambda A, B: cmp(max(A.x+w, A.y+h) , max(B.x+w, B.y+h))) # Reverse area sort # self.verts.sort(lambda A, B: cmp(max(A.x+w, A.y+h) , max(B.x+w, B.y+h))) # Reverse area sort
self.verts.sort(key = lambda b: max(b.x+w, b.y+h) ) # Reverse area sort
class box: class box:
@ -378,7 +379,9 @@ class boxList:
# Sort boxes by area # Sort boxes by area
def sortArea(self): def sortArea(self):
self.boxes.sort(lambda A, B: cmp(A.area, B.area) ) # Reverse area sort # uvlist.sort(key=lambda v: v.uv.x) #X coord sort
# self.boxes.sort(lambda A, B: cmp(A.area, B.area) ) # Reverse area sort
self.boxes.sort(key=lambda b: b.area ) # Reverse area sort
# BLENDER only # BLENDER only
def draw(self): def draw(self):

@ -171,7 +171,9 @@ def worldspace_verts_idx(me, ob):
verts_zsort= [ (i, v.co*mat) for i, v in enumerate(me.verts) ] verts_zsort= [ (i, v.co*mat) for i, v in enumerate(me.verts) ]
# Sorts along the Z Axis so we can optimize the getsnap. # Sorts along the Z Axis so we can optimize the getsnap.
verts_zsort.sort(lambda a,b: cmp(a[1].z, b[1].z,)) try: verts_zsort.sort(key = lambda a: a[1].z)
except: verts_zsort.sort(lambda a,b: cmp(a[1].z, b[1].z,))
return verts_zsort return verts_zsort

@ -152,7 +152,8 @@ def mesh_mirror(me, PREF_MIRROR_LOCATION, PREF_XMID_SNAP, PREF_MAX_DIST, PREF_XZ
# Now we have a list of the pairs we might use, lets find the best and do them first. # Now we have a list of the pairs we might use, lets find the best and do them first.
# de-selecting as we go. so we can makke sure not to mess it up. # de-selecting as we go. so we can makke sure not to mess it up.
mirror_pairs.sort(lambda a,b: cmp(a[0], b[0])) try: mirror_pairs.sort(key = lambda a: a[0])
except: mirror_pairs.sort(lambda a,b: cmp(a[0], b[0]))
for dist, v1,v2 in mirror_pairs: # dist, neg, pos for dist, v1,v2 in mirror_pairs: # dist, neg, pos
if v1.sel and v2.sel: if v1.sel and v2.sel:

@ -47,7 +47,7 @@ A pop-up will provide further options, if the results of a method are not adequa
# ***** END GPL LICENCE BLOCK ***** # ***** END GPL LICENCE BLOCK *****
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Made by Ideasman/Campbell 2005/06/15 - ideasman@linuxmail.org # Made by Ideasman/Campbell 2005/06/15 - cbarton@metavr.com
import Blender import Blender
from Blender import * from Blender import *
@ -184,7 +184,12 @@ class edgeLoop:
global CULL_METHOD global CULL_METHOD
if CULL_METHOD == 1: # Shortest edge if CULL_METHOD == 1: # Shortest edge
eloopCopy = self.edges[:] eloopCopy = self.edges[:]
eloopCopy.sort(lambda e1, e2: cmp(e1.length, e2.length )) # Length sort, smallest first
# Length sort, smallest first
try: eloopCopy.sort(key = lambda e1: e1.length)
except: eloopCopy.sort(lambda e1, e2: cmp(e1.length, e2.length ))
# Dont use atm
#eloopCopy.sort(lambda e1, e2: cmp(e1.angle*e1.length, e2.angle*e2.length)) # Length sort, smallest first #eloopCopy.sort(lambda e1, e2: cmp(e1.angle*e1.length, e2.angle*e2.length)) # Length sort, smallest first
#eloopCopy.sort(lambda e1, e2: cmp(e1.angle, e2.angle)) # Length sort, smallest first #eloopCopy.sort(lambda e1, e2: cmp(e1.angle, e2.angle)) # Length sort, smallest first
eloopCopy = eloopCopy[:cullNum] eloopCopy = eloopCopy[:cullNum]

@ -193,8 +193,10 @@ def island2Edge(island):
length_sorted_edges = [(Vector(key[0]), Vector(key[1]), value) for key, value in edges.iteritems() if value != 0] length_sorted_edges = [(Vector(key[0]), Vector(key[1]), value) for key, value in edges.iteritems() if value != 0]
length_sorted_edges.sort(lambda A, B: cmp(B[2], A[2]))
try: length_sorted_edges.sort(key = lambda A: -A[2]) # largest first
except: length_sorted_edges.sort(lambda A, B: cmp(B[2], A[2]))
# Its okay to leave the length in there. # Its okay to leave the length in there.
#for e in length_sorted_edges: #for e in length_sorted_edges:
# e.pop(2) # e.pop(2)
@ -435,12 +437,18 @@ def mergeUvIslands(islandList):
# Sort by island bounding box area, smallest face area first. # Sort by island bounding box area, smallest face area first.
# no.. chance that to most simple edge loop first. # no.. chance that to most simple edge loop first.
decoratedIslandListAreaSort =decoratedIslandList[:] decoratedIslandListAreaSort =decoratedIslandList[:]
decoratedIslandListAreaSort.sort(lambda A, B: cmp(A[3], B[3]))
try: decoratedIslandListAreaSort.sort(key = lambda A: A[3])
except: decoratedIslandListAreaSort.sort(lambda A, B: cmp(A[3], B[3]))
# sort by efficiency, Least Efficient first. # sort by efficiency, Least Efficient first.
decoratedIslandListEfficSort = decoratedIslandList[:] decoratedIslandListEfficSort = decoratedIslandList[:]
decoratedIslandListEfficSort.sort(lambda A, B: cmp(B[2], A[2])) # decoratedIslandListEfficSort.sort(lambda A, B: cmp(B[2], A[2]))
try: decoratedIslandListEfficSort.sort(key = lambda A: -A[2])
except: decoratedIslandListEfficSort.sort(lambda A, B: cmp(B[2], A[2]))
# ================================================== THESE CAN BE TWEAKED. # ================================================== THESE CAN BE TWEAKED.
# This is a quality value for the number of tests. # This is a quality value for the number of tests.
# from 1 to 4, generic quality value is from 1 to 100 # from 1 to 4, generic quality value is from 1 to 100
@ -584,7 +592,10 @@ def mergeUvIslands(islandList):
sourceIsland[6][:] = [] # Empty sourceIsland[6][:] = [] # Empty
# Sort by edge length, reverse so biggest are first. # Sort by edge length, reverse so biggest are first.
targetIsland[6].sort(lambda B,A: cmp(A[2], B[2] ))
try: targetIsland[6].sort(key = lambda A: A[2])
except: targetIsland[6].sort(lambda B,A: cmp(A[2], B[2] ))
targetIsland[7].extend(sourceIsland[7]) targetIsland[7].extend(sourceIsland[7])
offset= Vector(boxLeft, boxBottom, 0) offset= Vector(boxLeft, boxBottom, 0)
@ -808,7 +819,10 @@ def packIslands(islandList):
#print '\tWriting Packed Data to faces' #print '\tWriting Packed Data to faces'
Window.DrawProgressBar(0.8, 'Writing Packed Data to faces') Window.DrawProgressBar(0.8, 'Writing Packed Data to faces')
packedLs.sort(lambda A, B: cmp(A[0] , B[0])) # Sort by ID, so there in sync again
# Sort by ID, so there in sync again
try: packedLs.sort(lambda key = A: A[0])
except: packedLs.sort(lambda A, B: cmp(A[0] , B[0]))
islandIdx = len(islandList) islandIdx = len(islandList)
# Having these here avoids devide by 0 # Having these here avoids devide by 0
@ -946,7 +960,8 @@ def main():
if USER_SHARE_SPACE: if USER_SHARE_SPACE:
# Sort by data name so we get consistand results # Sort by data name so we get consistand results
obList.sort(lambda ob1, ob2: cmp( ob1.getData(name_only=1), ob2.getData(name_only=1) )) try: obList.sort(key = lambda ob: ob.getData(name_only=1))
except: obList.sort(lambda ob1, ob2: cmp( ob1.getData(name_only=1), ob2.getData(name_only=1) ))
collected_islandList= [] collected_islandList= []
@ -980,7 +995,9 @@ def main():
# Make a Face List that is sorted by area. # Make a Face List that is sorted by area.
# meshFaces = [] # meshFaces = []
meshFaces.sort( lambda a, b: cmp(b.area , a.area) ) # Biggest first. # meshFaces.sort( lambda a, b: cmp(b.area , a.area) ) # Biggest first.
try: meshFaces.sort( lambda a: -a.area )
except: meshFaces.sort( lambda a, b: cmp(b.area , a.area) )
# remove all zero area faces # remove all zero area faces
while meshFaces and meshFaces[-1].area <= SMALL_NUM: while meshFaces and meshFaces[-1].area <= SMALL_NUM:

@ -62,7 +62,9 @@ def mostUsedImage(imageList): # Returns the image most used in the list.
# Now a list of tuples, (imageName, {imageCount, image}) # Now a list of tuples, (imageName, {imageCount, image})
imageCount = imageCount.items() imageCount = imageCount.items()
imageCount.sort(lambda a,b: cmp(a[1], b[1])) try: imageCount.sort(key=lambda a: a[1])
except: imageCount.sort(lambda a,b: cmp(a[1], b[1]))
return imageCount[-1][1]['blenderImage'] return imageCount[-1][1]['blenderImage']