Skip to content

Commit

Permalink
Added logic to catch unexpected JSON config - eg, missing keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-drews committed Aug 13, 2024
1 parent a31a5e7 commit 78a1ee0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/acom_music_box/music_box_evolving_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 10 additions & 5 deletions src/acom_music_box/music_box_reaction_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 78a1ee0

Please sign in to comment.