def _read_var_info(self): """ Read Data variables metadata from JSON file and specifies static variable name sets listed above. """ assert self.VARINFO_FILE_NAME is not None assert self.VARINFO_FILE_PATH is not None file_path = os.path.join(self.VARINFO_FILE_PATH, self.VARINFO_FILE_NAME) if os.path.isfile(file_path): with open(file_path) as pfile: json_text = pfile.read() vardict = json_to_dict(json_text) else: # find file in conda package vardict = read_egg_json(self.VARINFO_FILE_NAME) # pragma: no cover self.INTEGER_READ_VARS = set(k for k, v in vardict['read'].items() if v['type'] == 'int') FLOAT_READ_VARS = set(k for k, v in vardict['read'].items() if v['type'] == 'float') self.MUST_READ_VARS = set(k for k, v in vardict['read'].items() if v.get('required')) self.USABLE_READ_VARS = self.INTEGER_READ_VARS | FLOAT_READ_VARS INT_CALCULATED_VARS = set(k for k, v in vardict['calc'].items() if v['type'] == 'int') FLOAT_CALCULATED_VARS = set(k for k, v in vardict['calc'].items() if v['type'] == 'float') FIXED_CALCULATED_VARS = set(k for k, v in vardict['calc'].items() if v['type'] == 'unchanging_float') self.CALCULATED_VARS = (INT_CALCULATED_VARS | FLOAT_CALCULATED_VARS | FIXED_CALCULATED_VARS) self.CHANGING_CALCULATED_VARS = FLOAT_CALCULATED_VARS self.INTEGER_VARS = self.INTEGER_READ_VARS | INT_CALCULATED_VARS
def __init__(self): # convert JSON in DEFAULTS_FILE_NAME into self._vals dictionary assert self.DEFAULTS_FILE_NAME is not None assert self.DEFAULTS_FILE_PATH is not None file_path = os.path.join(self.DEFAULTS_FILE_PATH, self.DEFAULTS_FILE_NAME) if os.path.isfile(file_path): with open(file_path) as pfile: json_text = pfile.read() vals = json_to_dict(json_text) else: # find file in conda package vals = read_egg_json(self.DEFAULTS_FILE_NAME) # pragma: no cover # add leading underscore character to each parameter name self._vals = OrderedDict() for pname in vals: self._vals['_' + pname] = vals[pname] del vals # declare several scalar variables self._current_year = 0 self._start_year = 0 self._end_year = 0 self._num_years = 0 self._last_known_year = 0 # declare optional _inflation_rates and _wage_growth_rates self._inflation_rates = list() self._wage_growth_rates = list() self._wage_indexed = None # declare removed and redefined parameters self._removed = None self._redefined = None # declare parameter warning/error variables self.parameter_warnings = '' self.parameter_errors = ''
def read_var_info(): """ Read Records variables metadata from JSON file; returns dictionary and specifies static varname sets listed below. """ var_info_path = os.path.join(Records.CUR_PATH, Records.VAR_INFO_FILENAME) if os.path.exists(var_info_path): with open(var_info_path) as vfile: json_text = vfile.read() vardict = json_to_dict(json_text) else: # cannot call read_egg_ function in unit tests vardict = read_egg_json( Records.VAR_INFO_FILENAME) # pragma: no cover Records.INTEGER_READ_VARS = set(k for k, v in vardict['read'].items() if v['type'] == 'int') FLOAT_READ_VARS = set(k for k, v in vardict['read'].items() if v['type'] == 'float') Records.MUST_READ_VARS = set(k for k, v in vardict['read'].items() if v.get('required')) Records.USABLE_READ_VARS = Records.INTEGER_READ_VARS | FLOAT_READ_VARS INT_CALCULATED_VARS = set(k for k, v in vardict['calc'].items() if v['type'] == 'int') FLOAT_CALCULATED_VARS = set(k for k, v in vardict['calc'].items() if v['type'] == 'float') FIXED_CALCULATED_VARS = set(k for k, v in vardict['calc'].items() if v['type'] == 'unchanging_float') Records.CALCULATED_VARS = (INT_CALCULATED_VARS | FLOAT_CALCULATED_VARS | FIXED_CALCULATED_VARS) Records.CHANGING_CALCULATED_VARS = FLOAT_CALCULATED_VARS Records.INTEGER_VARS = Records.INTEGER_READ_VARS | INT_CALCULATED_VARS return vardict
def _params_dict_from_json_file(cls): """ Read DEFAULTS_FILENAME file and return complete dictionary. Parameters ---------- nothing: void Returns ------- params: dictionary containing complete contents of DEFAULTS_FILENAME file. """ if cls.DEFAULTS_FILENAME is None: msg = 'DEFAULTS_FILENAME must be overridden by inheriting class' raise NotImplementedError(msg) path = os.path.join(os.path.abspath(os.path.dirname(__file__)), cls.DEFAULTS_FILENAME) if os.path.exists(path): with open(path) as pfile: json_text = pfile.read() params_dict = json_to_dict(json_text) else: # cannot call read_egg_ function in unit tests params_dict = read_egg_json( cls.DEFAULTS_FILENAME) # pragma: no cover return params_dict
def __init__(self): # convert JSON in DEFAULTS_FILE_NAME into self._vals dictionary assert self.DEFAULTS_FILE_NAME is not None assert self.DEFAULTS_FILE_PATH is not None file_path = os.path.join(self.DEFAULTS_FILE_PATH, self.DEFAULTS_FILE_NAME) if os.path.isfile(file_path): with open(file_path) as pfile: json_text = pfile.read() vals = json_to_dict(json_text) else: # find file in conda package vals = read_egg_json(self.DEFAULTS_FILE_NAME) # pragma: no cover # add leading underscore character to each parameter name self._vals = OrderedDict() for pname in vals: self._vals['_' + pname] = vals[pname] del vals # declare parameter warning/error variables self.parameter_warnings = '' self.parameter_errors = ''
def _read_json_revision(obj, topkey): """ Read JSON revision specified by obj and topkey returning a single revision dictionary suitable for use with the Parameters._update method. The obj function argument can be None or a string, where the string contains a local filename, a URL beginning with 'http' pointing to a valid JSON file hosted online, or valid JSON text. The topkey argument must be a string containing the top-level key in a compound-revision JSON text for which a revision dictionary is returned. If the specified topkey is not among the top-level JSON keys, the obj is assumed to be a non-compound-revision JSON text for the specified topkey. """ # embedded function used only in _read_json_revision staticmethod def convert_year_to_int(syr_dict): """ Converts specified syr_dict, which has string years as secondary keys, into a dictionary with the same structure but having integer years as secondary keys. """ iyr_dict = dict() for pkey, sdict in syr_dict.items(): assert isinstance(pkey, str) iyr_dict[pkey] = dict() assert isinstance(sdict, dict) for skey, val in sdict.items(): assert isinstance(skey, str) year = int(skey) iyr_dict[pkey][year] = val return iyr_dict # end of embedded function # process the main function arguments if obj is None: return dict() if not isinstance(obj, str): raise ValueError('obj is neither None nor a string') if not isinstance(topkey, str): raise ValueError('topkey={} is not a string'.format(topkey)) if os.path.isfile(obj): if not obj.endswith('.json'): msg = 'obj does not end with ".json": {}' raise ValueError(msg.format(obj)) txt = open(obj, 'r').read() elif obj.startswith('http'): if not obj.endswith('.json'): msg = 'obj does not end with ".json": {}' raise ValueError(msg.format(obj)) req = requests.get(obj) req.raise_for_status() txt = req.text else: txt = obj # strip out //-comments without changing line numbers json_txt = re.sub('//.*', ' ', txt) # convert JSON text into a Python dictionary full_dict = json_to_dict(json_txt) # check top-level key contents of dictionary if topkey in full_dict.keys(): single_dict = full_dict[topkey] else: single_dict = full_dict # convert string year to integer year in dictionary and return return convert_year_to_int(single_dict)