New \"spaces\" parameter in function \"unicode.translateString\" allows to specify a replacement string for spaces still present in converted result
authorborax
Fri, 14 Aug 2009 01:02:19 +0200
changeset 11 89da289790ea
parent 10 e8b562758753
child 12 2956fca1709a
New \"spaces\" parameter in function \"unicode.translateString\" allows to specify a replacement string for spaces still present in converted result
ztfy/utils/doctests/README.txt
ztfy/utils/unicode.py
--- a/ztfy/utils/doctests/README.txt	Thu Aug 13 19:59:05 2009 +0200
+++ b/ztfy/utils/doctests/README.txt	Fri Aug 14 01:02:19 2009 +0200
@@ -45,6 +45,21 @@
     >>> unicode.translateString(sample, escapeSlashes=True)
     u'test.txt'
 
+To remove remaining spaces or convert them to another character, you can use the "spaces" parameter
+which can contain any string to be used instead of initial spaces:
+
+    >>> sample = 'C:\\Program Files\\My Application\\test.txt'
+    >>> unicode.translateString(sample, spaces=' ')
+    u'cprogram filesmy applicationtest.txt'
+    >>> unicode.translateString(sample, spaces='-')
+    u'cprogram-filesmy-applicationtest.txt'
+
+Spaces replacement is made in the last step, so using it with "escapeSlashes" parameter only affects
+the final result:
+
+    >>> unicode.translateString(sample, escapeSlashes=True, spaces='-')
+    u'test.txt'
+
 
 Dates functions
 ---------------
--- a/ztfy/utils/unicode.py	Thu Aug 13 19:59:05 2009 +0200
+++ b/ztfy/utils/unicode.py	Fri Aug 14 01:02:19 2009 +0200
@@ -82,7 +82,7 @@
 _fillUnicodeTransTable()
 
 
-def translateString(s, escapeSlashes=False, forceLower=True) :
+def translateString(s, escapeSlashes=False, forceLower=True, spaces=' ') :
     """Remove extended characters from string and replace them with 'basic' ones
     
     @param s: text to be cleaned.
@@ -103,6 +103,8 @@
     s = ''.join([a for a in s.translate(_unicodeTransTable) if a.replace(' ','-') in (string.ascii_letters + string.digits + '_-.')])
     if forceLower:
         s = s.lower()
+    if spaces != ' ':
+        s = s.replace(' ', spaces)
     return s