mirror of
https://github.com/faucetsdn/ryu.git
synced 2026-01-25 10:32:03 +01:00
utils: Unify output str format of hex_array()
This patch simplifies hex_array() and unifies its output into '0x%02x' format. Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
parent
97906ee740
commit
27befc18fa
@ -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))
|
||||
|
||||
30
ryu/utils.py
30
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user