def test_type_bool_val(): assert try_type_val('TRUE', BOOL) is True assert try_type_val('yes', BOOL) is True assert try_type_val(1, BOOL) is True assert try_type_val(1.0, BOOL) is True assert try_type_val('false', BOOL) is False assert try_type_val('no', BOOL) is False assert try_type_val(0, BOOL) is False assert try_type_val(0.1, BOOL) is False assert try_type_val('abona;a;lnsbaioh', BOOL) is False
def _ask_until_correct(prop_args, prop_nm): atype = None if hasattr(prop_args.props[prop_nm], ATYPE): atype = prop_args.props[prop_nm].atype while True: answer = input(_get_question(prop_args, prop_nm)) if not answer: return prop_args.props[prop_nm].val try: typed_answer = try_type_val(answer, atype) except ValueError: print( "Input of invalid type. Should be {atype}".format(atype=atype)) continue if not prop_args._answer_within_bounds(prop_nm, typed_answer): print( "Input must be between {lowval} and {hival} inclusive.".format( lowval=prop_args.props[prop_nm].lowval, hival=prop_args.props[prop_nm].hival)) continue return typed_answer
def set_props_from_dict(prop_args, prop_dict): """ Dict Example: { "prop_name_1": { "val": 1, "question": "What value should this property have?", "atype": "int", "hival": 10, "lowval": 0 }, "prop_name_2": { "val": "Hello World." } } """ if prop_dict is None: return for prop_nm in prop_dict: atype = prop_dict[prop_nm].get(ATYPE, STR) val = try_type_val(prop_dict[prop_nm].get(VALUE, None), atype) question = prop_dict[prop_nm].get(QUESTION, None) hival = prop_dict[prop_nm].get(HIVAL, None) lowval = prop_dict[prop_nm].get(LOWVAL, None) prop_args.props[prop_nm] = Prop(val=val, question=question, atype=atype, hival=hival, lowval=lowval)
def set_props_from_cl(prop_args): args, _ = parser.parse_known_args() cl_dict = vars(args)['my_dict'] if not cl_dict: return for prop_nm in cl_dict: arg = cl_dict[prop_nm] if prop_nm in prop_args: arg = try_type_val(arg, prop_args.props[prop_nm].atype) prop_args[prop_nm] = arg
def test_type_null_val(): bool_none = try_type_val(None, BOOL) float_none = try_type_val(None, FLT) int_none = try_type_val(None, INT) string_none = try_type_val(None, STR) complex_none = try_type_val(None, CMPLX) assert isinstance(bool_none, bool) assert bool_none is False assert isinstance(float_none, float) assert float_none == 0.0 assert isinstance(int_none, int) assert int_none == 0 assert isinstance(string_none, str) assert string_none == '' assert isinstance(complex_none, complex) assert complex_none == 0j
def test_type_cmplx_val(): assert try_type_val('3+9j', CMPLX) == 3 + 9j
def test_type_flt_val(): assert try_type_val('2.3', FLT) == 2.3
def test_type_int_val(): assert try_type_val('2', INT) == 2