From 01b45b7e1c9e574c222c1179040f414ed9bcbd57 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Mon, 23 Dec 2024 12:53:19 -0800 Subject: [PATCH] Refactor to use pybase64 for faster encode/decode --- neon_mq_connector/utils/network_utils.py | 7 ++++--- requirements/requirements.txt | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/neon_mq_connector/utils/network_utils.py b/neon_mq_connector/utils/network_utils.py index 9e6d6ff..e20f4f2 100644 --- a/neon_mq_connector/utils/network_utils.py +++ b/neon_mq_connector/utils/network_utils.py @@ -27,9 +27,10 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import json -import base64 import socket +from pybase64 import b64encode, b64decode + """ These utils are duplicated from neon_utils.socket_utils to avoid a circular dependency. @@ -44,7 +45,7 @@ def b64_to_dict(data: bytes, charset: str = "utf-8") -> dict: @return decoded dictionary """ - return eval(json.loads(base64.b64decode(data).decode(charset))) + return eval(json.loads(b64decode(data).decode(charset))) def dict_to_b64(data: dict, charset: str = "utf-8") -> bytes: @@ -55,7 +56,7 @@ def dict_to_b64(data: dict, charset: str = "utf-8") -> bytes: @return base64 encoded string """ - return base64.b64encode(json.dumps(str(data)).encode(charset)) + return b64encode(json.dumps(str(data)).encode(charset)) def check_port_is_open(addr: str, port: int) -> bool: diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 5ff8a05..75a5404 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,3 +1,4 @@ pika==1.2.0 ovos-config~=0.0,>=0.0.8 ovos-utils~=0.0,>=0.0.32 +pybase64~=1.4 \ No newline at end of file