From 3f9118a1a6cc9421b959fac48e5062ae98e43953 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 14 May 2015 07:14:29 +0000 Subject: [PATCH] utils.str.format: Add support for sets for %L + add test case. Closes GH-1104 --- src/utils/str.py | 7 +++---- test/test_utils.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/utils/str.py b/src/utils/str.py index d9ca25f66..5f822e56f 100644 --- a/src/utils/str.py +++ b/src/utils/str.py @@ -478,15 +478,14 @@ def format(s, *args, **kwargs): return has(args.pop()) elif char == 'L': t = args.pop() - if isinstance(t, list) or (sys.version_info[0] >= 3 and - (isinstance(t, map) or isinstance(t, filter))): - return commaAndify(t) - elif isinstance(t, tuple) and len(t) == 2: + if isinstance(t, tuple) and len(t) == 2: if not isinstance(t[0], list): raise ValueError('Invalid list for %%L in format: %s' % t) if not isinstance(t[1], basestring): raise ValueError('Invalid string for %%L in format: %s' % t) return commaAndify(t[0], And=t[1]) + elif hasattr(t, '__iter__'): + return commaAndify(t) else: raise ValueError('Invalid value for %%L in format: %s' % t) elif char == 'p': diff --git a/test/test_utils.py b/test/test_utils.py index debad420f..ab41ea404 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -539,6 +539,18 @@ class FormatTestCase(SupyTestCase): 'I have 3 kinds of fruit: ' 'apples, oranges, and watermelon.') + def testPercentL(self): + self.assertIn(format('%L', {'apples', 'oranges', 'watermelon'}), { + 'apples, oranges, and watermelon', + 'oranges, apples, and watermelon', + 'apples, watermelon, and oranges', + 'oranges, watermelon, and apples', + 'watermelon, apples, and oranges', + 'watermelon, oranges, and apples'}) + + self.assertEqual(format('%L', + (['apples', 'oranges', 'watermelon'], 'or')), + 'apples, oranges, or watermelon') class RingBufferTestCase(SupyTestCase): def testInit(self): self.assertRaises(ValueError, RingBuffer, -1)