commit ab466a766063ed7196dda2e8122b5fc5ab2e75fb
parent 168326e184cc6251c1c27e003dc1127503b579a9
Author: ThomasV <thomasv@electrum.org>
Date: Mon, 13 Nov 2017 09:21:20 +0100
Merge pull request #3289 from JustinTArthur/simplify-bytes-funcs
Simplify bytes/hex conversion functions now that we're Python 3+ only.
Diffstat:
1 file changed, 8 insertions(+), 29 deletions(-)
diff --git a/lib/util.py b/lib/util.py
@@ -291,44 +291,23 @@ def to_bytes(something, encoding='utf8'):
else:
raise TypeError("Not a string or bytes like object")
-bfh_builder = lambda x: bytes.fromhex(x)
-
-def hfu(x):
- """
- py2-py3 aware wrapper for str.encode('hex')
- :param x: str
- :return: str
- """
- assert_bytes(x)
- return binascii.hexlify(x)
+bfh = bytes.fromhex
+hfu = binascii.hexlify
-def bfh(x):
- """
- py2-py3 aware wrapper to "bytes.fromhex()" func
- :param x: str
- :rtype: bytes
+def bh2u(x):
"""
- if isinstance(x, str):
- return bfh_builder(x)
- # TODO: check for iterator interface
- elif isinstance(x, (list, tuple, map)):
- return [bfh(sub) for sub in x]
- else:
- raise TypeError('Unexpected type: ' + str(type(x)))
+ str with hex representation of a bytes-like object
+ >>> x = bytes((1, 2, 10))
+ >>> bh2u(x)
+ '01020A'
-def bh2u(x):
- """
- unicode with hex representation of bytes()
- e.g. x = bytes([1, 2, 10])
- bh2u(x) -> '01020A'
:param x: bytes
:rtype: str
"""
- assert_bytes(x)
- return binascii.hexlify(x).decode('ascii')
+ return hfu(x).decode('ascii')
def user_dir():