Ticket #6393: 6393.patch

File 6393.patch, 2.0 KB (added by Francis Sylvain, 8 years ago)
  • python/text.py

     
    206206        if buf.find(p) != -1:
    207207            return True
    208208
     209def itemizedList(things, delimiter):
     210    """
     211    Produce a string containing each thing in C{things} separated by a comma,
     212    with the last couple being separated by C{delimiter}
     213   
     214    @rtype: C{str}
     215    """
     216    if len(things) == 1:
     217        return str(things[0])
     218    else:
     219        return "%s %s %s" % (', '.join(things[:-1]), delimiter,things[-1])
  • test/test_text.py

     
    240240    def test_insensitive(self):
    241241        self.assertEqual(True, text.strFile("ThIs is A test STRING", self.io, False))
    242242
     243class ItemizedListTest(unittest.TestCase):
     244   
     245    def test_oneWord(self):
     246        sample = ['One']
     247        expected = 'One'
     248        result = text.itemizeList(sample, 'and')
     249        self.assertEqual(expected, result)
     250
     251    def test_twoWords(self):
     252        sample = ['One', 'Two']
     253        expected = 'One and Two'
     254        result = text.itemizeList(sample, 'and')
     255        self.assertEqual(expected, result)
     256
     257    def test_threeWords(self):
     258        sample = ['One', 'Two', 'Three']
     259        expected = 'One, Two and Three'
     260        result = text.itemizeList(sample, 'and')
     261        self.assertEqual(expected, result)
     262
     263    def test_fourWords(self):
     264        sample = ['One', 'Two', 'Three', 'Four']
     265        expected = 'One, Two, Three or Four'
     266        result = text.itemizeList(sample, 'or')
     267        self.assertEqual(expected, result)
     268 No newline at end of file
  • topfiles/6393.feature

     
     1Added an itemizedList method to twisted.python.text