def read_var_info(): ''' Read Assets variables metadata from JSON file; returns dictionary and specifies static varname sets listed below. ''' var_info_path = os.path.join(Assets.CUR_PATH, Assets.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( Assets.VAR_INFO_FILENAME) # pragma: no cover Assets.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') Assets.MUST_READ_VARS = set(k for k, v in vardict['read'].items() if v.get('required')) Assets.USABLE_READ_VARS = Assets.INTEGER_READ_VARS | FLOAT_READ_VARS Assets.INTEGER_VARS = Assets.INTEGER_READ_VARS return vardict
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 test_json_to_dict_exception(): ''' Test of utils.json_to_dict() function ''' json_string = """{ "CIT_rate" } }""" with pytest.raises(Exception): assert utils.json_to_dict(json_string)
def test_json_to_dict(): ''' Test of utils.json_to_dict() function ''' json_string = """{ "read": { "asset_name": { "type": "string", "desc": "Description of asset" }, "assets": { "type": "float", "desc": "Dollar value of assets" } } }""" test_dict = utils.json_to_dict(json_string) assert test_dict['read']['asset_name']['type'] == 'string'
def read_var_info(): """ Read Assets variables metadata from JSON file; returns dictionary and specifies static varname sets listed below. """ var_info_path = os.path.join(Assets.CUR_PATH, Assets.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( Assets.VAR_INFO_FILENAME) # pragma: no cover Assets.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') Assets.MUST_READ_VARS = set(k for k, v in vardict['read'].items() if v.get('required')) Assets.USABLE_READ_VARS = Assets.INTEGER_READ_VARS | FLOAT_READ_VARS Assets.INTEGER_VARS = Assets.INTEGER_READ_VARS return vardict