From 142dede5e309bff5c6b53b1e984fe5de4687fbea Mon Sep 17 00:00:00 2001 From: Daniel Van Der Maden Date: Wed, 1 Jan 2020 14:51:12 -0800 Subject: [PATCH] [util] Add kwargs to `json_loads` and add test --- pyhmy/util.py | 4 ++-- tests/util-pyhmy/test_util.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/util-pyhmy/test_util.py diff --git a/pyhmy/util.py b/pyhmy/util.py index 03d691e..ac35e81 100644 --- a/pyhmy/util.py +++ b/pyhmy/util.py @@ -101,7 +101,7 @@ def download_cli(bin_name="hmy", replace=True, verbose=True): os.chdir(old_cwd) -def json_load(string): +def json_load(string, **kwargs): """ :param string: The JSON string to load :returns A dictionary loaded from a JSON string to a dictionary. @@ -110,7 +110,7 @@ def json_load(string): Note that this prints the failed input should an error arise. """ try: - return json.loads(string) + return json.loads(string, **kwargs) except Exception as e: print(f"{Typgpy.FAIL}Could not parse input: '{string}'{Typgpy.ENDC}") raise e from e diff --git a/tests/util-pyhmy/test_util.py b/tests/util-pyhmy/test_util.py new file mode 100644 index 0000000..3e86935 --- /dev/null +++ b/tests/util-pyhmy/test_util.py @@ -0,0 +1,22 @@ +import decimal +import json + +import pytest + +from pyhmy import util + + +def test_json_load(): + dec = util.json_load('1.1', parse_float=decimal.Decimal) + assert isinstance(dec, decimal.Decimal) + assert float(dec) == 1.1 + ref_dict = { + 'test': 'val', + 'arr': [ + 1, + 2, + 3 + ] + } + loaded_dict = util.json_load(json.dumps(ref_dict)) + assert str(ref_dict) == str(loaded_dict)