[PATCH] [Wok] Wok utils method for converting String to Unicode and vice versa.

From: Archana Singh <archus@linux.vnet.ibm.com> Added two utils methods which can be used to convert String to Unicode and Unicode to String. This method does the checking of instance of value passed and accordingly used the encode, decode, str methods. Signed-off-by: Archana Singh <archus@linux.vnet.ibm.com> --- src/wok/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/wok/utils.py b/src/wok/utils.py index 175cf25..0e8ec05 100644 --- a/src/wok/utils.py +++ b/src/wok/utils.py @@ -622,3 +622,27 @@ def upgrade_objectstore_schema(objstore=None, field=None): wok_log.error("Cannot upgrade objectstore schema: %s" % e.args[0]) return False return True + + +def encode_value(val): + """ + Convert the value to string. + If its unicode, use encode otherwise str. + """ + if isinstance(val, unicode): + return val.encode('utf-8') + return str(val) + + +def decode_value(val): + """ + Converts value to unicode, + if its not an instance of unicode. + For doing so convert the val to string, + if its not instance of basestring. + """ + if not isinstance(val, basestring): + val = str(val) + if not isinstance(val, unicode): + val = val.decode('utf-8') + return val -- 2.5.0

Reviewed-By: Ramon Medeiros <ramonn@br.ibm.com> On 04/13/2016 01:10 AM, archus@linux.vnet.ibm.com wrote:
From: Archana Singh <archus@linux.vnet.ibm.com>
Added two utils methods which can be used to convert String to Unicode and Unicode to String. This method does the checking of instance of value passed and accordingly used the encode, decode, str methods.
Signed-off-by: Archana Singh <archus@linux.vnet.ibm.com> --- src/wok/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
diff --git a/src/wok/utils.py b/src/wok/utils.py index 175cf25..0e8ec05 100644 --- a/src/wok/utils.py +++ b/src/wok/utils.py @@ -622,3 +622,27 @@ def upgrade_objectstore_schema(objstore=None, field=None): wok_log.error("Cannot upgrade objectstore schema: %s" % e.args[0]) return False return True + + +def encode_value(val): + """ + Convert the value to string. + If its unicode, use encode otherwise str. + """ + if isinstance(val, unicode): + return val.encode('utf-8') + return str(val) + + +def decode_value(val): + """ + Converts value to unicode, + if its not an instance of unicode. + For doing so convert the val to string, + if its not instance of basestring. + """ + if not isinstance(val, basestring): + val = str(val) + if not isinstance(val, unicode): + val = val.decode('utf-8') + return val
-- Ramon Nunes Medeiros Kimchi Developer Linux Technology Center Brazil IBM Systems & Technology Group Phone : +55 19 2132 7878 ramonn@br.ibm.com
participants (3)
-
Aline Manera
-
archus@linux.vnet.ibm.com
-
Ramon Medeiros