def _And(self, ast): argtypes = self.visit(ast.parameters) try: for t in argtypes: unify(t, T.Bool, self.tcon) except InferenceError, exn: raise InferenceError, "logic expression requires boolean arguments %s" % ast
def _If(self, ast): tb = self.visit(ast.test()) t1 = self.visit(ast.body()) t2 = self.visit(ast.orelse()) try: unify(tb, T.Bool, self.tcon) except InferenceError, exn: raise InferenceError, "conditional must have boolean test %s" % ast
def _Subscript(self, ast): valtype = self.visit(ast.value()) idxtype = self.visit(ast.slice()) restype = self.tcon.fresh_typevar() unify(idxtype, T.Int, self.tcon) unify(valtype, T.Seq(restype), self.tcon) return restype
def _Apply(self, ast): fntype = self.visit(ast.function()) argtypes = self.visit(ast.arguments()) restype = self.tcon.fresh_typevar() inst = T.Fn(T.Tuple(*argtypes), restype) unify(fntype, inst, self.tcon) outputType = resolve_type(restype, self.tcon) return outputType
def _Map(self, ast): fn, args = ast.parameters[0], ast.parameters[1:] # Type the function we're applying fntype = self.visit(fn) argtypes = self.visit(args) items = [self.tcon.fresh_typevar() for x in argtypes] for t, seq in zip(items, argtypes): unify(T.Seq(t), seq, self.tcon) # Unify function type with element types restype = self.tcon.fresh_typevar() unify(fntype, T.Fn(T.Tuple(*items), restype), self.tcon) return T.Seq(resolve(restype, self.tcon.environment))
def _Closure(self, ast): fnType = self.visit(ast.parameters[0]) if isinstance(fnType, T.Polytype): fnTypeM = fnType.monotype() else: fnTypeM = fnType closedArgTypes = self.visit(ast.variables) fnArgTypes = fnTypeM.parameters[0].parameters closedFnArgTypes = fnArgTypes[-len(closedArgTypes):] openFnArgTypes = fnArgTypes[0:-len(closedArgTypes)] items = [self.tcon.fresh_typevar() for x in fnArgTypes] for t, item in zip(items[-len(closedArgTypes):], closedArgTypes): unify(t, item, self.tcon) restype = self.tcon.fresh_typevar() unify(fnType, T.Fn(T.Tuple(*items), restype), self.tcon) outputType = T.Fn(T.Tuple(*items[0:-len(closedArgTypes)]), restype) return resolve_type(outputType, self.tcon)
def typeBlock(self, block): restype = self.tcon.fresh_typevar() for stmt in block: t = self.visit(stmt) if isinstance(t, T.Type): unify(restype, t, self.tcon) def buildType(binder): if isinstance(binder, AST.Name): id = binder.id type = self.tcon.typings[id] return type subtypes = [buildType(x) for x in binder.parameters] tupleType = T.Tuple(*subtypes) return tupleType for stmt in block: if isinstance(stmt, AST.Bind): indicatedType = buildType(stmt.binder()) normalizedType = normalize_type(indicatedType, self.tcon) stmt.type = normalizedType return restype
def run(input, column, columns): log = logger.get_logger("geoharm.py") log.info("Geocoding...") if (".xls" in input) or (".xlsx" in input): data = pd.read_excel("./data/{}".format(input)) elif ".csv" in input: data = pd.read_csv("./data/{}".format(input)) else: sys.exit( "Cannot determine the filetype of input, is it .csv, .xls or .xlsx?" ) log.info("Dataset size: {}".format(len(data))) if columns is "": with open("./data/{}.txt".format(columns), "r") as myfile: columns = [line.split(", ") for line in myfile.readlines()][0] data = unify(data, columns) output, missing = geolocate(data, column) log.info("Number of missing geolocations: {}".format(len(missing))) input = input.split(".")[0] output.to_csv("./results/{}/{}_geo.csv".format(input, input)) with open('./results/{}/{}_missing.txt'.format(input, input), mode='wt', encoding='utf-8') as myfile: myfile.write('\n'.join(missing)) log.info( "Geolocating finished. Output saved to ./results/{}/".format(input))
def _Cond(self, ast): tb = self.expression_type(ast.test()) try: unify(tb, T.Bool, self.tcon) except InferenceError, exn: raise InferenceError, "conditional must have boolean test %s" % ast
def _While(self, ast): conditionType = self.expression_type(ast.parameters[0]) unify(conditionType, T.Bool, self.tcon) bodyType = self.typeBlock(ast.parameters[1]) return bodyType