22import json
33import math
44
5- from .exception import CurriculumError
5+ from .exception import CurriculumConfigError , CurriculumLoadingError
66
77import logging
88
@@ -23,14 +23,8 @@ def __init__(self, location, default_reset_parameters):
2323 # The name of the brain should be the basename of the file without the
2424 # extension.
2525 self ._brain_name = os .path .basename (location ).split ("." )[0 ]
26+ self .data = Curriculum .load_curriculum_file (location )
2627
27- try :
28- with open (location ) as data_file :
29- self .data = json .load (data_file )
30- except IOError :
31- raise CurriculumError ("The file {0} could not be found." .format (location ))
32- except UnicodeDecodeError :
33- raise CurriculumError ("There was an error decoding {}" .format (location ))
3428 self .smoothing_value = 0
3529 for key in [
3630 "parameters" ,
@@ -40,7 +34,7 @@ def __init__(self, location, default_reset_parameters):
4034 "signal_smoothing" ,
4135 ]:
4236 if key not in self .data :
43- raise CurriculumError (
37+ raise CurriculumConfigError (
4438 "{0} does not contain a " "{1} field." .format (location , key )
4539 )
4640 self .smoothing_value = 0
@@ -51,12 +45,12 @@ def __init__(self, location, default_reset_parameters):
5145 parameters = self .data ["parameters" ]
5246 for key in parameters :
5347 if key not in default_reset_parameters :
54- raise CurriculumError (
48+ raise CurriculumConfigError (
5549 "The parameter {0} in Curriculum {1} is not present in "
5650 "the Environment" .format (key , location )
5751 )
5852 if len (parameters [key ]) != self .max_lesson_num + 1 :
59- raise CurriculumError (
53+ raise CurriculumConfigError (
6054 "The parameter {0} in Curriculum {1} must have {2} values "
6155 "but {3} were found" .format (
6256 key , location , self .max_lesson_num + 1 , len (parameters [key ])
@@ -117,3 +111,27 @@ def get_config(self, lesson=None):
117111 for key in parameters :
118112 config [key ] = parameters [key ][lesson ]
119113 return config
114+
115+ @staticmethod
116+ def load_curriculum_file (location ):
117+ try :
118+ with open (location ) as data_file :
119+ return Curriculum ._load_curriculum (data_file )
120+ except IOError :
121+ raise CurriculumLoadingError (
122+ "The file {0} could not be found." .format (location )
123+ )
124+ except UnicodeDecodeError :
125+ raise CurriculumLoadingError (
126+ "There was an error decoding {}" .format (location )
127+ )
128+
129+ @staticmethod
130+ def _load_curriculum (fp ):
131+ try :
132+ return json .load (fp )
133+ except json .decoder .JSONDecodeError as e :
134+ raise CurriculumLoadingError (
135+ "Error parsing JSON file. Please check for formatting errors. "
136+ "A tool such as https://jsonlint.com/ can be helpful with this."
137+ ) from e
0 commit comments