From 3d656a3d537939985f43bf549ca7003abb79f1a4 Mon Sep 17 00:00:00 2001 From: dh00601 <dh00601@surrey.ac.uk> Date: Thu, 30 Dec 2021 10:15:00 +0000 Subject: [PATCH] fixing some bugs for the tests --- badges/test_coverage.svg | 4 +-- binarycpython/utils/dicts.py | 55 ++++++++++++++---------------------- binarycpython/utils/grid.py | 2 ++ 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/badges/test_coverage.svg b/badges/test_coverage.svg index cb3cdc0ec..f86374a02 100644 --- a/badges/test_coverage.svg +++ b/badges/test_coverage.svg @@ -15,7 +15,7 @@ <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> <text x="31.5" y="15" fill="#010101" fill-opacity=".3">coverage</text> <text x="31.5" y="14">coverage</text> - <text x="80" y="15" fill="#010101" fill-opacity=".3">41%</text> - <text x="80" y="14">41%</text> + <text x="80" y="15" fill="#010101" fill-opacity=".3">43%</text> + <text x="80" y="14">43%</text> </g> </svg> diff --git a/binarycpython/utils/dicts.py b/binarycpython/utils/dicts.py index 244d99278..7009d21df 100644 --- a/binarycpython/utils/dicts.py +++ b/binarycpython/utils/dicts.py @@ -482,14 +482,9 @@ def merge_dicts(dict_1: dict, dict_2: dict) -> dict: ): try: new_dict[key] = int(dict_1[key]) + int(dict_2[key]) - except: - print( - "key", - key, - ': Failed to convert string, one of "{}" or "{}" to an int'.format( - dict_1[key], dict_2[key] - ), - ) + except ValueError as e: + msg = "{}: Failed to convert string (either '{}' or '{}') to an int".format(key, dict_1[key], dict_2[key]) + raise ValueError(msg) from e # string-float clash : convert both to floats and save elif ( @@ -500,28 +495,22 @@ def merge_dicts(dict_1: dict, dict_2: dict) -> dict: ): try: new_dict[key] = float(dict_1[key]) + float(dict_2[key]) - except: - print( - "key", - key, - ': Failed to convert string, one of "{}" or "{}" to a float'.format( - dict_1[key], dict_2[key] - ), - ) + except ValueError as e: + msg = "{}: Failed to convert string (either '{}' or '{}') to an float".format(key, dict_1[key], dict_2[key]) + raise ValueError(msg) from e # If the above cases have not dealt with it, then we should raise an error else: - print( - "merge_dicts error: key: {key} value: {value1} type: {type1} and key: {key} value: {value2} type: {type2} are not of the same type and cannot be merged".format( - key=key, - value1=dict_1[key], - type1=type(dict_1[key]), - value2=dict_2[key], - type2=type(dict_2[key]), - ) + msg = "merge_dicts error: key: {key} value: {value1} type: {type1} and key: {key} value: {value2} type: {type2} are not of the same type and cannot be merged".format( + key=key, + value1=dict_1[key], + type1=type(dict_1[key]), + value2=dict_2[key], + type2=type(dict_2[key]), ) - raise ValueError + raise ValueError(msg) + # Here the keys are the same type # Here we check for the cases that we want to explicitly catch. Ints will be added, # floats will be added, lists will be appended (though that might change) and dicts will be # dealt with by calling this function again. @@ -566,16 +555,14 @@ def merge_dicts(dict_1: dict, dict_2: dict) -> dict: new_dict[key] = None else: - print( - "Object types {}: {} ({}), {} ({}) not supported.".format( - key, - dict_1[key], - type(dict_1[key]), - dict_2[key], - type(dict_2[key]), - ) + msg = "Object types {}: {} ({}), {} ({}) not supported.".format( + key, + dict_1[key], + type(dict_1[key]), + dict_2[key], + type(dict_2[key]), ) - raise ValueError + raise ValueError(msg) # return new_dict diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 3f90ab6e3..4aa28736a 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -3,6 +3,8 @@ Module containing the Population grid class object. Here all the functionality of a Population object is defined. +TODO: the save_snapshots and save_snapshot, are they actually distinct? + Tasks: - TODO: add functionality to 'on-init' set arguments - TODO: add functionality to return the initial_abundance_hash -- GitLab