]> granicus.if.org Git - python/commitdiff
Merged revisions 75074 via svnmerge from
authorEzio Melotti <ezio.melotti@gmail.com>
Sat, 26 Sep 2009 12:35:01 +0000 (12:35 +0000)
committerEzio Melotti <ezio.melotti@gmail.com>
Sat, 26 Sep 2009 12:35:01 +0000 (12:35 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r75074 | ezio.melotti | 2009-09-26 15:33:22 +0300 (Sat, 26 Sep 2009) | 9 lines

  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
  ........
................

Doc/library/string.rst
Lib/string.py
Lib/test/test_string.py

index 2fd70be94fd015efe845be6b8c9bf7686f572937..ae068a95ec26e9d916a9b06bde539fea7c701d5e 100644 (file)
@@ -565,12 +565,14 @@ rule:
 Helper functions
 ----------------
 
-.. function:: capwords(s)
-
-   Split the argument into words using :func:`split`, capitalize each word using
-   :func:`capitalize`, and join the capitalized words using :func:`join`.  Note
-   that this replaces runs of whitespace characters by a single space, and removes
-   leading and trailing whitespace.
+.. function:: capwords(s[, sep])
+
+   Split the argument into words using :meth:`str.split`, capitalize each word
+   using :meth:`str.capitalize`, and join the capitalized words using
+   :meth:`str.join`.  If the optional second argument *sep* is absent
+   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.
 
 
 .. function:: maketrans(frm, to)
index e071a2d1b23edea366504078091da5b410c8c434..a9898e8a06e0e6be66bdae876ac006b212e702d2 100644 (file)
@@ -29,15 +29,17 @@ printable = digits + ascii_letters + punctuation + whitespace
 
 # Capitalize the words in a string, e.g. " aBc  dEf " -> "Abc Def".
 def capwords(s, sep=None):
-    """capwords(s, [sep]) -> string
+    """capwords(s [,sep]) -> string
 
     Split the argument into words using split, capitalize each
     word using capitalize, and join the capitalized words using
-    join. Note that this replaces runs of whitespace characters by
-    a single space.
+    join.  If the optional second argument sep is absent 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.
 
     """
-    return (sep or ' ').join([x.capitalize() for x in s.split(sep)])
+    return (sep or ' ').join(x.capitalize() for x in s.split(sep))
 
 
 # Construct a translation map for bytes.translate
index 743fa6b27fe8df82cbd776e6d5754243f17708cd..b495d69717a58e470174158290c211cd9e7dbbe3 100644 (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 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):
         fmt = string.Formatter()