[GRASS-SVN] r71394 - in grass/trunk/lib/python/script: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Aug 11 15:18:29 PDT 2017
Author: zarch
Date: 2017-08-11 15:18:29 -0700 (Fri, 11 Aug 2017)
New Revision: 71394
Modified:
grass/trunk/lib/python/script/testsuite/test_utils.py
grass/trunk/lib/python/script/utils.py
Log:
script.utils: Convert to string/bytes if needed, add doctests and unittests
Modified: grass/trunk/lib/python/script/testsuite/test_utils.py
===================================================================
--- grass/trunk/lib/python/script/testsuite/test_utils.py 2017-08-11 19:33:51 UTC (rev 71393)
+++ grass/trunk/lib/python/script/testsuite/test_utils.py 2017-08-11 22:18:29 UTC (rev 71394)
@@ -43,7 +43,19 @@
"""If the input is bytes we should not touch it for encoding"""
self.assertEqual(b'Příšerný kůň', utils.encode(b'Příšerný kůň'))
+ def test_int(self):
+ """If the input is an integer return bytes"""
+ self.assertEqual(b'1234567890', utils.encode(1234567890))
+ def test_float(self):
+ """If the input is a float return bytes"""
+ self.assertEqual(b'12345.6789', utils.encode(12345.6789))
+
+ def test_none(self):
+ """If the input is a boolean return bytes"""
+ self.assertEqual(b'None', utils.encode(None))
+
+
class TestDecode(TestCase):
"""Tests function `encode` that convert value to unicode."""
@@ -53,7 +65,19 @@
def test_unicode(self):
self.assertEqual(u'text', utils.decode(u'text'))
+ def test_int(self):
+ """If the input is an integer return bytes"""
+ self.assertEqual(u'1234567890', utils.decode(1234567890))
+ def test_float(self):
+ """If the input is a float return bytes"""
+ self.assertEqual(u'12345.6789', utils.decode(12345.6789))
+
+ def test_none(self):
+ """If the input is a boolean return bytes"""
+ self.assertEqual(u'None', utils.decode(None))
+
+
class TestEncodeLcAllC(TestEncode, LcAllC):
pass
Modified: grass/trunk/lib/python/script/utils.py
===================================================================
--- grass/trunk/lib/python/script/utils.py 2017-08-11 19:33:51 UTC (rev 71393)
+++ grass/trunk/lib/python/script/utils.py 2017-08-11 22:18:29 UTC (rev 71394)
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
"""
Useful functions to be used in Python scripts.
@@ -24,6 +25,14 @@
import shlex
import re
+
+try:
+ from builtins import unicode
+except ImportError:
+ # python3
+ unicode = str
+
+
def float_or_dms(s):
"""Convert DMS to float.
@@ -163,11 +172,23 @@
No-op if parameter is not bytes (assumed unicode string).
:param bytes bytes_: the bytes to decode
+
+ Example
+ -------
+
+ >>> decode(b'S\xc3\xbcdtirol')
+ u'Südtirol'
+ >>> decode(u'Südtirol')
+ u'Südtirol'
+ >>> decode(1234)
+ u'1234'
"""
+ if isinstance(bytes_, unicode):
+ return bytes_
if isinstance(bytes_, bytes):
enc = _get_encoding()
return bytes_.decode(enc)
- return bytes_
+ return unicode(bytes_)
def encode(string):
@@ -177,11 +198,23 @@
This ensures garbage in, garbage out.
:param str string: the string to encode
+
+ Example
+ -------
+
+ >>> encode(b'S\xc3\xbcdtirol')
+ b'S\xc3\xbcdtirol'
+ >>> decode(u'Südtirol')
+ b'S\xc3\xbcdtirol'
+ >>> decode(1234)
+ b'1234'
"""
if isinstance(string, bytes):
return string
- enc = _get_encoding()
- return string.encode(enc)
+ if isinstance(string, unicode):
+ enc = _get_encoding()
+ return string.encode(enc)
+ return bytes(string)
def parse_key_val(s, sep='=', dflt=None, val_type=None, vsep=None):
More information about the grass-commit
mailing list