misc.py 967 B

1234567891011121314151617181920212223
  1. import json
  2. def parFileCheck(parFile, parNames):
  3. """
  4. Checks if each dictionary in the list of dictionaries in parFile contains all the keys in parNames. Assertions
  5. are used to raise errors if problems are detected.
  6. :param parFile: a valid file path containing the list of dictionaries
  7. :param parNames: list of parameter names expected
  8. :return: list of dictionaries in parFile
  9. """
  10. with open(parFile, 'r') as fle:
  11. parsList = json.load(fle)
  12. assert type(parsList) == list, 'Parameter file does not contain a list of dictionaries as is the requirement.'
  13. for parInd, par in enumerate(parsList):
  14. assert type(par) == dict, 'Parameter set # {} in {} not a list'.format(parInd, parFile)
  15. for pn in parNames:
  16. assert pn in par, 'Parameter {} not found in ' \
  17. 'parameter set # {} of {} is improper'.format(pn, parInd, parFile)
  18. return parsList