Merged revisions 75070 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r75070 | ezio.melotti | 2009-09-26 14:20:53 +0300 (Sat, 26 Sep 2009) | 1 line

  #7000: document "sep" in capwords. Add a few tests
........
This commit is contained in:
Ezio Melotti 2009-09-26 12:33:22 +00:00
parent 2c6a949e43
commit a40bdda937
3 changed files with 16 additions and 9 deletions

View File

@ -565,10 +565,12 @@ rule:
Helper functions Helper functions
---------------- ----------------
.. function:: capwords(s) .. function:: capwords(s[, sep])
Split the argument into words using :func:`split`, capitalize each word using Split the argument into words using :meth:`str.split`, capitalize each word
:func:`capitalize`, and join the capitalized words using :func:`join`. Note using :meth:`str.capitalize`, and join the capitalized words using
that this replaces runs of whitespace characters by a single space, and removes :meth:`str.join`. If the optional second argument *sep* is absent
leading and trailing whitespace. or ``None``, runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise *sep* is used to
split and join the words.

View File

@ -29,15 +29,17 @@ printable = digits + ascii_letters + punctuation + whitespace
# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
def capwords(s, sep=None): def capwords(s, sep=None):
"""capwords(s, [sep]) -> string """capwords(s [,sep]) -> string
Split the argument into words using split, capitalize each Split the argument into words using split, capitalize each
word using capitalize, and join the capitalized words using word using capitalize, and join the capitalized words using
join. Note that this replaces runs of whitespace characters by join. If the optional second argument sep is absent or None,
a single space. runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise
sep is used to split and join the words.
""" """
return (sep or ' ').join([x.capitalize() for x in s.split(sep)]) return (sep or ' ').join(x.capitalize() for x in s.split(sep))
#################################################################### ####################################################################

View File

@ -22,6 +22,9 @@ class ModuleTest(unittest.TestCase):
self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi') self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi')
self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi') self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi')
self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi') self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi')
self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def')
self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
def test_formatter(self): def test_formatter(self):
fmt = string.Formatter() fmt = string.Formatter()