python3: Make ryu.utils.hex_array functional
Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
parent
fb6256ab17
commit
698cf43b03
15
ryu/utils.py
15
ryu/utils.py
@ -33,6 +33,7 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import six
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@ -99,18 +100,24 @@ def round_up(x, y):
|
|||||||
|
|
||||||
|
|
||||||
def _str_to_hex(data):
|
def _str_to_hex(data):
|
||||||
"""Convert string into array of hexes to be printed."""
|
"""Convert str into array of hexes to be printed. (Python2 only)"""
|
||||||
return ' '.join(hex(ord(char)) for char in data)
|
return ' '.join(hex(ord(char)) for char in data)
|
||||||
|
|
||||||
|
|
||||||
def _bytearray_to_hex(data):
|
def _bytearray_to_hex(data):
|
||||||
"""Convert bytearray into array of hexes to be printed."""
|
"""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)
|
return ' '.join(hex(byte) for byte in data)
|
||||||
|
|
||||||
|
|
||||||
def hex_array(data):
|
def hex_array(data):
|
||||||
"""Convert string or bytearray into array of hexes to be printed."""
|
"""Convert binary_type or bytearray into array of hexes to be printed."""
|
||||||
to_hex = {str: _str_to_hex,
|
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}
|
bytearray: _bytearray_to_hex}
|
||||||
try:
|
try:
|
||||||
return to_hex[type(data)](data)
|
return to_hex[type(data)](data)
|
||||||
|
Loading…
Reference in New Issue
Block a user