def test_infer(self): self.assertEqual(strconv.infer('-3'), 'int') self.assertEqual(strconv.infer(''), 'float') self.assertEqual(strconv.infer('+0.4'), 'float') self.assertEqual(strconv.infer('true'), 'bool') self.assertEqual(strconv.infer('3/20/2013'), 'datetime') self.assertEqual(strconv.infer('5:40 PM'), 'time') self.assertEqual(strconv.infer('March 4, 2013 5:40 PM'), 'datetime')
def test_infer(self): self.assertEqual(strconv.infer('-3'), 'int') self.assertEqual(strconv.infer('+0.4'), 'float') self.assertEqual(strconv.infer('true'), 'bool') self.assertEqual(strconv.infer('3/20/2013'), 'date') self.assertEqual(strconv.infer('5:40 PM'), 'time') self.assertEqual(strconv.infer('March 4, 2013 5:40 PM'), 'datetime') self.assertEqual(strconv.infer('2018-12-01 00:00:00'), 'datetime') self.assertEqual(strconv.infer('March 4, 2013 12:00 PM'), 'datetime') # Midnight self.assertEqual(strconv.convert_datetime('2013-03-01 00:00:00'), datetime(2013, 3, 1, 0, 0, 0)) self.assertEqual(strconv.convert_datetime('2018/03/01 00:00:00'), datetime(2018, 3, 1, 0, 0, 0))
def test_infer(self): self.assertEqual(strconv.infer('-3'), 'int') self.assertEqual(strconv.infer('+0.4'), 'float') self.assertEqual(strconv.infer('true'), 'bool') self.assertEqual(strconv.infer('3/20/2013'), 'datetime') self.assertEqual(strconv.infer('5:40 PM'), 'time') self.assertEqual(strconv.infer('March 4, 2013 5:40 PM'), 'datetime')
def test_infer_converted(self): self.assertEqual(strconv.infer('-3', converted=True), int) self.assertEqual(strconv.infer('+0.4', converted=True), float) self.assertEqual(strconv.infer('true', converted=True), bool) self.assertEqual(strconv.infer('3/20/2013', converted=True), date) self.assertEqual(strconv.infer('5:40 PM', converted=True), time) self.assertEqual(strconv.infer('March 4, 2013 5:40 PM', converted=True), datetime)
def inferType(key, value): if isinstance(value, list): res = [] for v in value: if type(v) is str: v = str(v.decode('string_escape')) inferred = strconv.infer(v) #print 'List - ', key, ': ', inferred if (inferred is None) or re.match('^date', inferred): res.append(str(v)) else: res.append(str(strconv.convert(v))) #print key, ' result: ', [type(r) for r in res] res = res[0] if len(res) == 1 else ";".join(res) else: if type(value) is str: value = str(value.decode('string_escape')) inferred = strconv.infer(value) #print 'Single - ', key, ': ', inferred if (inferred is None) or re.match('^date', inferred): res = value else: res = strconv.convert(value) #print key, ' result: ', type(res) return res
def analyze_line(line, d): fields = line.split("\t") name = fields[0] value = fields[1].strip() value_type = strconv.infer(value, converted=True) if value_type not in [str, int, float]: value_type = str # print("{} - {}".format(name, value)) if name not in d.keys(): d[name] = (value_type, set([value_type(value)])) else: current_type = d[name][0] # not agreement between types if current_type != value_type: # if one of the two is a string...str always wins if value_type == str or current_type == str: d[name] = (str, d[name][1]) # between int and float...float always wins elif (current_type == int and value_type == float) or \ (current_type == float and value_type == int): d[name] = (float, d[name][1]) d[name][1].add(value_type(value))
"and": lambda *args: reduce(lambda total, arg: total and arg, args, True), "or": lambda *args: reduce(lambda total, arg: total or arg, args, False), "?:": lambda a, b, c: b if a else c, "if": if_, "log": lambda a: logger.info(a) or a, "in": lambda a, b: a in b if "__contains__" in dir(b) else False, "cat": lambda *args: "".join(str(arg) for arg in args), "+": plus, "*": lambda *args: reduce(lambda total, arg: total * float(arg), args, 1), "-": minus, "/": lambda a, b=None: a if b is None else float(a) / float(b), "min": lambda *args: min(args), "max": lambda *args: max(args), "merge": merge, "count": lambda *args: sum(1 if a else 0 for a in args), "type": lambda a, b: strconv.infer(type(a)) is b, } def jsonLogic(tests, data=None): """Executes the json-logic with given data.""" # You've recursed to a primitive, stop! if tests is None or not isinstance(tests, dict): return tests data = data or {} operator = list(tests.keys())[0] values = tests[operator] # Easy syntax for unary operators, like {"var": "x"} instead of strict