mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Added utils.sorted.
This commit is contained in:
parent
0aa5f2e5f7
commit
88581d99e4
@ -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
|
||||||
|
@ -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
|
||||||
@ -213,6 +212,14 @@ class UtilsTest(unittest.TestCase):
|
|||||||
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')
|
||||||
self.assertEqual(utils.nItems(1, 'tool'), '1 tool')
|
self.assertEqual(utils.nItems(1, 'tool'), '1 tool')
|
||||||
|
Loading…
Reference in New Issue
Block a user