This patch simplifies hex_array() and unifies its output into
'0x%02x' format.

Signed-off-by: IWASE Yusuke <[email protected]>
---
 ryu/tests/unit/test_utils.py | 28 ++++++++++++++++++----------
 ryu/utils.py                 | 30 +++++-------------------------
 2 files changed, 23 insertions(+), 35 deletions(-)

diff --git a/ryu/tests/unit/test_utils.py b/ryu/tests/unit/test_utils.py
index 64886c3..303e291 100644
--- a/ryu/tests/unit/test_utils.py
+++ b/ryu/tests/unit/test_utils.py
@@ -31,19 +31,27 @@ class Test_utils(unittest.TestCase):
         pass
 
     def test_hex_array_string(self):
-        ''' Test string conversion into array of hexes '''
-        expected_result = '0x1 0x2 0x3 0x4'
-        data = b'\01\02\03\04'
+        """
+        Test hex_array() with str type.
+        """
+        expected_result = '0x01 0x02 0x03 0x04'
+        data = b'\x01\x02\x03\x04'
         eq_(expected_result, utils.hex_array(data))
 
     def test_hex_array_bytearray(self):
-        ''' Test bytearray conversion into array of hexes '''
-        expected_result = '0x1 0x2 0x3 0x4'
-        data = bytearray(b'\01\02\03\04')
+        """
+        Test hex_array() with bytearray type.
+        """
+        expected_result = '0x01 0x02 0x03 0x04'
+        data = bytearray(b'\x01\x02\x03\x04')
         eq_(expected_result, utils.hex_array(data))
 
-    def test_hex_array_invalid(self):
-        ''' Test conversion into array of hexes with invalid data type '''
-        expected_result = None
-        data = 1234
+    def test_hex_array_bytes(self):
+        """
+        Test hex_array() with bytes type. (Python3 only)
+        """
+        if six.PY2:
+            return
+        expected_result = '0x01 0x02 0x03 0x04'
+        data = bytes(b'\x01\x02\x03\x04')
         eq_(expected_result, utils.hex_array(data))
diff --git a/ryu/utils.py b/ryu/utils.py
index 4a5bc44..fd8225e 100644
--- a/ryu/utils.py
+++ b/ryu/utils.py
@@ -33,7 +33,6 @@
 import inspect
 import logging
 import os
-import six
 import sys
 import re
 
@@ -99,31 +98,12 @@ def round_up(x, y):
     return ((x + y - 1) // y) * y
 
 
-def _str_to_hex(data):
-    """Convert str into array of hexes to be printed. (Python2 only)"""
-    return ' '.join(hex(ord(char)) for char in data)
-
-
-def _bytearray_to_hex(data):
-    """Convert bytearray into array of hexes to be printed.
-    In Python3, this function works for binary_types, too.
-    """
-    return ' '.join(hex(byte) for byte in data)
-
-
 def hex_array(data):
-    """Convert binary_type or bytearray into array of hexes to be printed."""
-    if six.PY3:
-        to_hex = {six.binary_type: _bytearray_to_hex,
-                  bytearray: _bytearray_to_hex}
-    else:
-        to_hex = {six.binary_type: _str_to_hex,
-                  bytearray: _bytearray_to_hex}
-    try:
-        return to_hex[type(data)](data)
-    except KeyError:
-        LOG.exception('%s is invalid data type', type(data))
-        return None
+    """
+    Convert six.binary_type or bytearray into array of hexes to be printed.
+    """
+    # convert data into bytearray explicitly
+    return ' '.join('0x%02x' % byte for byte in bytearray(data))
 
 
 # the following functions are taken from OpenStack
-- 
1.9.1



------------------------------------------------------------------------------
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to