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: params_dict = json.load(pfile, object_pairs_hook=collect.OrderedDict) else: # cannot call read_egg_ function in unit tests params_dict = read_egg_json( cls.DEFAULTS_FILENAME) # pragma: no cover return params_dict
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 read_var_info(): """ Read Records variables metadata from JSON file; returns dictionary and specifies static varname sets listed below. """ if os.path.exists(Records.VAR_INFO_PATH): with open(Records.VAR_INFO_PATH) as vfile: vardict = json.load(vfile) else: vardict = read_egg_json(Records.VAR_INFO_FILENAME) 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 __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 __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 test_read_egg_json(): with pytest.raises(ValueError): read_egg_json('bad_filename')
def test_read_egg_json(): with pytest.raises(ValueError): read_egg_json('bad_filename')