[util] Add kwargs to `json_loads` and add test

pull/2/head
Daniel Van Der Maden 5 years ago
parent 23c4b72812
commit 142dede5e3
  1. 4
      pyhmy/util.py
  2. 22
      tests/util-pyhmy/test_util.py

@ -101,7 +101,7 @@ def download_cli(bin_name="hmy", replace=True, verbose=True):
os.chdir(old_cwd) os.chdir(old_cwd)
def json_load(string): def json_load(string, **kwargs):
""" """
:param string: The JSON string to load :param string: The JSON string to load
:returns A dictionary loaded from a JSON string to a dictionary. :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. Note that this prints the failed input should an error arise.
""" """
try: try:
return json.loads(string) return json.loads(string, **kwargs)
except Exception as e: except Exception as e:
print(f"{Typgpy.FAIL}Could not parse input: '{string}'{Typgpy.ENDC}") print(f"{Typgpy.FAIL}Could not parse input: '{string}'{Typgpy.ENDC}")
raise e from e raise e from e

@ -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)
Loading…
Cancel
Save