From 78a1ee0ff51ba5c519a66238390e247e51d596ab Mon Sep 17 00:00:00 2001 From: Carl Drews Date: Tue, 13 Aug 2024 09:07:37 -0600 Subject: [PATCH] Added logic to catch unexpected JSON config - eg, missing keys. --- .../music_box_evolving_conditions.py | 9 ++++++--- src/acom_music_box/music_box_reaction_list.py | 15 ++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/acom_music_box/music_box_evolving_conditions.py b/src/acom_music_box/music_box_evolving_conditions.py index a6a90777..ab05ce87 100644 --- a/src/acom_music_box/music_box_evolving_conditions.py +++ b/src/acom_music_box/music_box_evolving_conditions.py @@ -108,9 +108,12 @@ def from_config_JSON(cls, path_to_json ,config_JSON, species_list, reaction_list # Check if 'evolving conditions' is a key in the JSON config if 'evolving conditions' in config_JSON: - # Construct the path to the evolving conditions file - evolving_conditions_path = os.path.dirname(path_to_json) + "/" + list(config_JSON['evolving conditions'].keys())[0] - evolving_conditions = EvolvingConditions.read_conditions_from_file( evolving_conditions_path, species_list, reaction_list) + if len(config_JSON['evolving conditions'].keys()) > 0: + # Construct the path to the evolving conditions file + evolving_conditions_path = (os.path.dirname(path_to_json) + "/" + + list(config_JSON['evolving conditions'].keys())[0]) + evolving_conditions = EvolvingConditions.read_conditions_from_file( + evolving_conditions_path, species_list, reaction_list) return evolving_conditions diff --git a/src/acom_music_box/music_box_reaction_list.py b/src/acom_music_box/music_box_reaction_list.py index 512ed5ba..9f106fb1 100644 --- a/src/acom_music_box/music_box_reaction_list.py +++ b/src/acom_music_box/music_box_reaction_list.py @@ -5,6 +5,9 @@ from .music_box_reactant import Reactant from .music_box_product import Product +import logging +logger = logging.getLogger(__name__) + class ReactionList: """ Represents a list of chemical reactions. @@ -114,12 +117,14 @@ def get_reactants_from_JSON(self, reaction, species_list): """ reactants = [] - for reactant, reactant_info in reaction['reactants'].items(): - match = filter(lambda x: x.name == reactant, species_list.species) - species = next(match, None) - quantity = reactant_info['qty'] if 'qty' in reactant_info else None + if ('reactants' in reaction.keys()): + for reactant, reactant_info in reaction['reactants'].items(): + match = filter(lambda x: x.name == reactant, species_list.species) + species = next(match, None) + #logger.info("reactant = {} species = {}".format(reactant, species)) + quantity = reactant_info['qty'] if 'qty' in reactant_info else None - reactants.append(Reactant(species, quantity)) + reactants.append(Reactant(species, quantity)) return reactants @classmethod