Ticket #6393: 6393.3.patch

File 6393.3.patch, 4.7 KB (added by Francis Sylvain, 8 years ago)
  • twisted/python/text.py

     
    206206        if buf.find(p) != -1:
    207207            return True
    208208
     209def toPhrase(things, finalDelimiter, delimiter=', '):
     210    """
     211    Produce a string containing each thing in C{things},
     212    separated by a C{delimiter}, with the last couple being separated
     213    by C{finalDelimiter}
     214   
     215    @param things: The elements of the resulting phrase
     216    @type things: An object that has a length and can be iterated over
     217   
     218    @param finalDelimiter: What to put between the last two things
     219    @type finalDelimiter: C{str}
     220   
     221    @param delimiter: The separator to use between each thing
     222    @type delimiter: C{str}
     223   
     224    @return: The resulting phrase
     225    @rtype: C{str}
     226    """
     227    if not hasattr(things, '__len__') or isinstance(things, basestring):
     228        raise TypeError("Things must be a collection of objects")
     229    if not things:
     230        return ''
     231    if len(things) == 1:
     232        return str(things[0])
     233    if len(things) == 2:
     234        return "%s %s %s" % (str(things[0]), finalDelimiter, str(things[1]))
     235    else:
     236        strThings = []
     237        for thing in things:
     238            strThings.append(str(thing))
     239        return "%s%s%s %s" % (delimiter.join(strThings[:-1]), delimiter, finalDelimiter, strThings[-1])
  • twisted/test/test_text.py

     
    240240    def test_insensitive(self):
    241241        self.assertEqual(True, text.strFile("ThIs is A test STRING", self.io, False))
    242242
     243
     244
     245class ToPhraseTest(unittest.TestCase):
     246    """
     247    Input is transformed into a string representation of the list,
     248    with each item separated by delimiter (defaulting to a comma) and the final
     249    two being separated by a final delimiter
     250    """
     251   
     252    def test_empty(self):
     253        """
     254        If things is empty, an empty string is returned
     255        """
     256        sample = []
     257        expected = ''
     258        result = text.toPhrase(sample, 'and')
     259        self.assertEqual(expected, result)
     260   
     261   
     262    def test_oneWord(self):
     263        """
     264        With a single item, the item is returned
     265        """
     266        sample = ['One']
     267        expected = 'One'
     268        result = text.toPhrase(sample, 'and')
     269        self.assertEqual(expected, result)
     270
     271
     272    def test_twoWords(self):
     273        """
     274        Two words are separated by the final delimiter
     275        """
     276        sample = ['One', 'Two']
     277        expected = 'One and Two'
     278        result = text.toPhrase(sample, 'and')
     279        self.assertEqual(expected, result)
     280
     281
     282    def test_threeWords(self):
     283        """
     284        With more than two words, the first two are separated by the delimiter
     285        """
     286        sample = ['One', 'Two', 'Three']
     287        expected = 'One, Two, and Three'
     288        result = text.toPhrase(sample, 'and')
     289        self.assertEqual(expected, result)
     290
     291
     292    def test_fourWords(self):
     293        """
     294        If a delimiter is specified, it is used instead of the default comma
     295        """
     296        sample = ['One', 'Two', 'Three', 'Four']
     297        expected = 'One; Two; Three; or Four'
     298        result = text.toPhrase(sample, 'or', delimiter='; ')
     299        self.assertEqual(expected, result)
     300
     301
     302    def test_notString(self):
     303        """
     304        If something in things is not a string, it is converted into one
     305        """
     306        sample = [1,2,'three']
     307        expected = '1, 2, and three'
     308        result = text.toPhrase(sample, 'and')
     309        self.assertEqual(expected, result)
     310       
     311       
     312    def test_stringTypeError(self):
     313        """
     314        If things is a string, a TypeError is raised
     315        """
     316        sample = "One, two, three"
     317        self.assertRaises(TypeError, text.toPhrase, sample, 'and')
     318
     319
     320    def test_iteratorTypeError(self):
     321        """
     322        If things is an iterator, a TypeError is raised
     323        """
     324        sample = iter([1, 2, 3])
     325        self.assertRaises(TypeError, text.toPhrase, sample, 'and')
     326
     327
     328    def test_generatorTypeError(self):
     329        """
     330        If things is a generator, a TypeError is raised
     331        """
     332        def sample():
     333            for i in range(2):
     334                yield i
     335        self.assertRaises(TypeError, text.toPhrase, sample, 'and')
  • twisted/topfiles/6393.feature

     
     1Added an itemizedList method to twisted.python.text