utils.str.format: Add support for sets for %L + add test case. Closes GH-1104

This commit is contained in:
Valentin Lorentz 2015-05-14 07:14:29 +00:00
parent 2008956968
commit 3f9118a1a6
2 changed files with 15 additions and 4 deletions

View File

@ -478,15 +478,14 @@ def format(s, *args, **kwargs):
return has(args.pop()) return has(args.pop())
elif char == 'L': elif char == 'L':
t = args.pop() t = args.pop()
if isinstance(t, list) or (sys.version_info[0] >= 3 and if isinstance(t, tuple) and len(t) == 2:
(isinstance(t, map) or isinstance(t, filter))):
return commaAndify(t)
elif isinstance(t, tuple) and len(t) == 2:
if not isinstance(t[0], list): if not isinstance(t[0], list):
raise ValueError('Invalid list for %%L in format: %s' % t) raise ValueError('Invalid list for %%L in format: %s' % t)
if not isinstance(t[1], basestring): if not isinstance(t[1], basestring):
raise ValueError('Invalid string for %%L in format: %s' % t) raise ValueError('Invalid string for %%L in format: %s' % t)
return commaAndify(t[0], And=t[1]) return commaAndify(t[0], And=t[1])
elif hasattr(t, '__iter__'):
return commaAndify(t)
else: else:
raise ValueError('Invalid value for %%L in format: %s' % t) raise ValueError('Invalid value for %%L in format: %s' % t)
elif char == 'p': elif char == 'p':

View File

@ -539,6 +539,18 @@ class FormatTestCase(SupyTestCase):
'I have 3 kinds of fruit: ' 'I have 3 kinds of fruit: '
'apples, oranges, and watermelon.') '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): class RingBufferTestCase(SupyTestCase):
def testInit(self): def testInit(self):
self.assertRaises(ValueError, RingBuffer, -1) self.assertRaises(ValueError, RingBuffer, -1)