##// END OF EJS Templates
compat: use 'key' argument instead of 'cmp' when sorting a list
Alejandro Santos -
r9032:1fa80c54 default
parent child Browse files
Show More
@@ -143,8 +143,8 b' def churn(ui, repo, *pats, **opts):'
143 if not rate:
143 if not rate:
144 return
144 return
145
145
146 sortfn = ((not opts.get('sort')) and (lambda a, b: cmp(b[1], a[1])) or None)
146 sortkey = ((not opts.get('sort')) and (lambda x: -x[1]) or None)
147 rate.sort(sortfn)
147 rate.sort(key=sortkey)
148
148
149 maxcount = float(max([v for k, v in rate]))
149 maxcount = float(max([v for k, v in rate]))
150 maxname = max([len(k) for k, v in rate])
150 maxname = max([len(k) for k, v in rate])
@@ -12,13 +12,6 b' import cPickle as pickle'
12 from mercurial import util
12 from mercurial import util
13 from mercurial.i18n import _
13 from mercurial.i18n import _
14
14
15 def listsort(list, key):
16 "helper to sort by key in Python 2.3"
17 try:
18 list.sort(key=key)
19 except TypeError:
20 list.sort(lambda l, r: cmp(key(l), key(r)))
21
22 class logentry(object):
15 class logentry(object):
23 '''Class logentry has the following attributes:
16 '''Class logentry has the following attributes:
24 .author - author name as CVS knows it
17 .author - author name as CVS knows it
@@ -419,7 +412,7 b' def createlog(ui, directory=None, root="'
419 if len(log) % 100 == 0:
412 if len(log) % 100 == 0:
420 ui.status(util.ellipsis('%d %s' % (len(log), e.file), 80)+'\n')
413 ui.status(util.ellipsis('%d %s' % (len(log), e.file), 80)+'\n')
421
414
422 listsort(log, key=lambda x:(x.rcs, x.revision))
415 log.sort(key=lambda x: (x.rcs, x.revision))
423
416
424 # find parent revisions of individual files
417 # find parent revisions of individual files
425 versions = {}
418 versions = {}
@@ -435,7 +428,7 b' def createlog(ui, directory=None, root="'
435 if cache:
428 if cache:
436 if log:
429 if log:
437 # join up the old and new logs
430 # join up the old and new logs
438 listsort(log, key=lambda x:x.date)
431 log.sort(key=lambda x: x.date)
439
432
440 if oldlog and oldlog[-1].date >= log[0].date:
433 if oldlog and oldlog[-1].date >= log[0].date:
441 raise logerror('Log cache overlaps with new log entries,'
434 raise logerror('Log cache overlaps with new log entries,'
@@ -484,7 +477,7 b' def createchangeset(ui, log, fuzz=60, me'
484
477
485 # Merge changesets
478 # Merge changesets
486
479
487 listsort(log, key=lambda x:(x.comment, x.author, x.branch, x.date))
480 log.sort(key=lambda x: (x.comment, x.author, x.branch, x.date))
488
481
489 changesets = []
482 changesets = []
490 files = set()
483 files = set()
@@ -1541,7 +1541,7 b' class queue(object):'
1541 raise util.Abort(_('option "-r" not valid when importing '
1541 raise util.Abort(_('option "-r" not valid when importing '
1542 'files'))
1542 'files'))
1543 rev = cmdutil.revrange(repo, rev)
1543 rev = cmdutil.revrange(repo, rev)
1544 rev.sort(lambda x, y: cmp(y, x))
1544 rev.sort(reverse=True)
1545 if (len(files) > 1 or len(rev) > 1) and patchname:
1545 if (len(files) > 1 or len(rev) > 1) and patchname:
1546 raise util.Abort(_('option "-n" not valid when importing multiple '
1546 raise util.Abort(_('option "-n" not valid when importing multiple '
1547 'patches'))
1547 'patches'))
@@ -2334,7 +2334,7 b' def select(ui, repo, *args, **opts):'
2334 if ui.verbose:
2334 if ui.verbose:
2335 guards['NONE'] = noguards
2335 guards['NONE'] = noguards
2336 guards = guards.items()
2336 guards = guards.items()
2337 guards.sort(lambda a, b: cmp(a[0][1:], b[0][1:]))
2337 guards.sort(key=lambda x: x[0][1:])
2338 if guards:
2338 if guards:
2339 ui.note(_('guards in series file:\n'))
2339 ui.note(_('guards in series file:\n'))
2340 for guard, count in guards:
2340 for guard, count in guards:
@@ -1709,14 +1709,14 b' class localrepository(repo.repository):'
1709
1709
1710 # A function generating function. Sets up an environment for the
1710 # A function generating function. Sets up an environment for the
1711 # inner function.
1711 # inner function.
1712 def cmp_by_rev_func(revlog):
1712 def revkey(revlog):
1713 # Compare two nodes by their revision number in the environment's
1713 # Key to sort a node by it's revision number in the environment's
1714 # revision history. Since the revision number both represents the
1714 # revision history. Since the revision number both represents the
1715 # most efficient order to read the nodes in, and represents a
1715 # most efficient order to read the nodes in, and represents a
1716 # topological sorting of the nodes, this function is often useful.
1716 # topological sorting of the nodes, this function is often useful.
1717 def cmp_by_rev(a, b):
1717 def revlog_sort_key(x):
1718 return cmp(revlog.rev(a), revlog.rev(b))
1718 return revlog.rev(x)
1719 return cmp_by_rev
1719 return revlog_sort_key
1720
1720
1721 # If we determine that a particular file or manifest node must be a
1721 # If we determine that a particular file or manifest node must be a
1722 # node that the recipient of the changegroup will already have, we can
1722 # node that the recipient of the changegroup will already have, we can
@@ -1724,7 +1724,7 b' class localrepository(repo.repository):'
1724 # prunes them from the set of missing nodes.
1724 # prunes them from the set of missing nodes.
1725 def prune_parents(revlog, hasset, msngset):
1725 def prune_parents(revlog, hasset, msngset):
1726 haslst = list(hasset)
1726 haslst = list(hasset)
1727 haslst.sort(cmp_by_rev_func(revlog))
1727 haslst.sort(key=revkey(revlog))
1728 for node in haslst:
1728 for node in haslst:
1729 parentlst = [p for p in revlog.parents(node) if p != nullid]
1729 parentlst = [p for p in revlog.parents(node) if p != nullid]
1730 while parentlst:
1730 while parentlst:
@@ -1874,7 +1874,7 b' class localrepository(repo.repository):'
1874 add_extra_nodes(1, msng_mnfst_set)
1874 add_extra_nodes(1, msng_mnfst_set)
1875 msng_mnfst_lst = msng_mnfst_set.keys()
1875 msng_mnfst_lst = msng_mnfst_set.keys()
1876 # Sort the manifestnodes by revision number.
1876 # Sort the manifestnodes by revision number.
1877 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1877 msng_mnfst_lst.sort(key=revkey(mnfst))
1878 # Create a generator for the manifestnodes that calls our lookup
1878 # Create a generator for the manifestnodes that calls our lookup
1879 # and data collection functions back.
1879 # and data collection functions back.
1880 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1880 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
@@ -1912,7 +1912,7 b' class localrepository(repo.repository):'
1912 yield changegroup.chunkheader(len(fname))
1912 yield changegroup.chunkheader(len(fname))
1913 yield fname
1913 yield fname
1914 # Sort the filenodes by their revision #
1914 # Sort the filenodes by their revision #
1915 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1915 msng_filenode_lst.sort(key=revkey(filerevlog))
1916 # Create a group generator and only pass in a changenode
1916 # Create a group generator and only pass in a changenode
1917 # lookup function as we need to collect no information
1917 # lookup function as we need to collect no information
1918 # from filenodes.
1918 # from filenodes.
@@ -26,12 +26,10 b' class Stats(object):'
26 """XXX docstring"""
26 """XXX docstring"""
27 if crit not in profiler_entry.__dict__:
27 if crit not in profiler_entry.__dict__:
28 raise ValueError("Can't sort by %s" % crit)
28 raise ValueError("Can't sort by %s" % crit)
29 self.data.sort(lambda b, a: cmp(getattr(a, crit),
29 self.data.sort(key=lambda x: getattr(x, crit), reverse=True)
30 getattr(b, crit)))
31 for e in self.data:
30 for e in self.data:
32 if e.calls:
31 if e.calls:
33 e.calls.sort(lambda b, a: cmp(getattr(a, crit),
32 e.calls.sort(key=lambda x: getattr(x, crit), reverse=True)
34 getattr(b, crit)))
35
33
36 def pprint(self, top=None, file=None, limit=None, climit=None):
34 def pprint(self, top=None, file=None, limit=None, climit=None):
37 """XXX docstring"""
35 """XXX docstring"""
@@ -325,10 +325,6 b' class patchfile(object):'
325 # looks through the hash and finds candidate lines. The
325 # looks through the hash and finds candidate lines. The
326 # result is a list of line numbers sorted based on distance
326 # result is a list of line numbers sorted based on distance
327 # from linenum
327 # from linenum
328 def sorter(a, b):
329 vala = abs(a - linenum)
330 valb = abs(b - linenum)
331 return cmp(vala, valb)
332
328
333 try:
329 try:
334 cand = self.hash[l]
330 cand = self.hash[l]
@@ -337,7 +333,7 b' class patchfile(object):'
337
333
338 if len(cand) > 1:
334 if len(cand) > 1:
339 # resort our list of potentials forward then back.
335 # resort our list of potentials forward then back.
340 cand.sort(sorter)
336 cand.sort(key=lambda x: abs(x - linenum))
341 return cand
337 return cand
342
338
343 def hashlines(self):
339 def hashlines(self):
General Comments 0
You need to be logged in to leave comments. Login now