Added utils.sorted.

This commit is contained in:
Jeremy Fincher 2003-12-09 14:46:12 +00:00
parent 0aa5f2e5f7
commit 88581d99e4
2 changed files with 15 additions and 1 deletions

View File

@ -377,6 +377,13 @@ def sortBy(f, L, cmp=cmp):
for (i, elt) in enumerate(L): for (i, elt) in enumerate(L):
L[i] = L[i][2] L[i] = L[i][2]
def sorted(iterable, *args, **kwargs):
"""Returns a sorted list made from iterable. All other args are given to
list.sort unchanged."""
L = list(iterable)
L.sort(*args, **kwargs)
return L
def mktemp(suffix=''): def mktemp(suffix=''):
"""Gives a decent random string, suitable for a filename.""" """Gives a decent random string, suitable for a filename."""
import sha import sha

View File

@ -34,7 +34,6 @@ import sets
import utils import utils
class UtilsTest(unittest.TestCase): class UtilsTest(unittest.TestCase):
def testPluralize(self): def testPluralize(self):
f = utils.pluralize f = utils.pluralize
@ -212,6 +211,14 @@ class UtilsTest(unittest.TestCase):
L = ['supybot', 'Supybot'] L = ['supybot', 'Supybot']
utils.sortBy(str.lower, L) utils.sortBy(str.lower, L)
self.assertEqual(L, ['supybot', 'Supybot']) self.assertEqual(L, ['supybot', 'Supybot'])
def testSorted(self):
L = ['a', 'c', 'b']
self.assertEqual(utils.sorted(L), ['a', 'b', 'c'])
self.assertEqual(L, ['a', 'c', 'b'])
def mycmp(x, y):
return -cmp(x, y)
self.assertEqual(utils.sorted(L, mycmp), ['c', 'b', 'a'])
def testNItems(self): def testNItems(self):
self.assertEqual(utils.nItems(1, 'tool', 'crazy'), '1 crazy tool') self.assertEqual(utils.nItems(1, 'tool', 'crazy'), '1 crazy tool')