From bfb305e2b8cec0523d2f3769b374667d5c7303ef Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 20 Mar 2014 21:17:18 +0000 Subject: [PATCH] Utilities: Use number comparison for integers and floats in @sort. --- plugins/Utilities/plugin.py | 8 +++++--- plugins/Utilities/test.py | 2 ++ src/commands.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/plugins/Utilities/plugin.py b/plugins/Utilities/plugin.py index 122bb7ffb..50d999a54 100644 --- a/plugins/Utilities/plugin.py +++ b/plugins/Utilities/plugin.py @@ -110,8 +110,10 @@ class Utilities(callbacks.Plugin): Sorts the arguments given. """ - irc.reply(' '.join(sorted(things))) - sort = wrap(sort, [many('anything')]) + irc.reply(' '.join(map(str, sorted(things)))) + # Keep ints as ints, floats as floats, without comparing between numbers + # and strings. + sort = wrap(sort, [first(many(first('int', 'float')), many('anything'))]) @internationalizeDocstring def sample(self, irc, msg, args, num, things): @@ -148,7 +150,7 @@ class Utilities(callbacks.Plugin): tokens = callbacks.tokenize(text) allTokens = commands + tokens self.Proxy(irc, msg, allTokens) - apply = wrap(apply, ['something', many('anything')]) + apply = wrap(apply, ['something', many('something')]) Class = Utilities diff --git a/plugins/Utilities/test.py b/plugins/Utilities/test.py index 1459d9f75..339a0b57d 100644 --- a/plugins/Utilities/test.py +++ b/plugins/Utilities/test.py @@ -64,6 +64,8 @@ class UtilitiesTestCase(PluginTestCase): def testSort(self): self.assertResponse('sort abc cab cba bca', 'abc bca cab cba') + self.assertResponse('sort 2 12 42 7 2', '2 2 7 12 42') + self.assertResponse('sort 2 8 12.2 12.11 42 7 2', '2 2 7 8 12.11 12.2 42') def testSample(self): self.assertResponse('sample 1 a', 'a') diff --git a/src/commands.py b/src/commands.py index 6611b0435..7f93d8326 100644 --- a/src/commands.py +++ b/src/commands.py @@ -243,7 +243,7 @@ def _int(s): try: return int(s, base) except ValueError: - if base == 10: + if base == 10 and '.' not in s: try: return int(float(s)) except OverflowError: