Added "encode" and "decode" functions in "unicode" module (with updated doctests) ZTK-1.1
authorThierry Florac <thierry.florac@onf.fr>
Tue, 20 May 2014 15:54:14 +0200
branchZTK-1.1
changeset 256 cb99848d8334
parent 255 da1d22528032
child 257 ac47d86ae3ea
Added "encode" and "decode" functions in "unicode" module (with updated doctests)
src/ztfy/utils/doctests/README.txt
src/ztfy/utils/unicode.py
--- a/src/ztfy/utils/doctests/README.txt	Sat May 10 11:35:13 2014 +0200
+++ b/src/ztfy/utils/doctests/README.txt	Tue May 20 15:54:14 2014 +0200
@@ -60,6 +60,20 @@
     >>> unicode.translateString(sample, escapeSlashes=True, spaces='-')
     u'test.txt'
 
+Unicode module also provides encoding and decoding functions:
+
+    >>> var = 'Chaîne accentuée'
+    >>> unicode.decode(var)
+    u'Cha\xeene accentu\xe9e'
+    >>> unicode.encode(unicode.decode(var)) == var
+    True
+
+    >>> utf = u'Cha\xeene accentu\xe9e'
+    >>> unicode.encode(utf)
+    'Cha\xc3\xaene accentu\xc3\xa9e'
+    >>> unicode.decode(unicode.encode(utf)) == utf
+    True
+
 
 Dates functions
 ---------------
--- a/src/ztfy/utils/unicode.py	Sat May 10 11:35:13 2014 +0200
+++ b/src/ztfy/utils/unicode.py	Tue May 20 15:54:14 2014 +0200
@@ -163,8 +163,16 @@
     return [uninvl(v) for v in value]
 
 
+def encode(value, encoding='utf-8'):
+    """Encode given value with encoding"""
+    return value.encode(encoding) if isinstance(value, unicode) else value
+
+
 def utf8(value):
-    """Convert given value to UTF-8"""
-    if isinstance(value, unicode):
-        value = value.encode('utf8')
-    return value
+    """Encode given value tu UTF-8"""
+    return encode(value, 'utf-8')
+
+
+def decode(value, encoding='utf-8'):
+    """Decode given value with encoding"""
+    return value.decode(encoding) if isinstance(value, str) else value