def _Procedure(self, ast): con = self.context # Create a new type variable for the return type of the procedure restype = con.fresh_typevar() restype.source = None # Get the names and type variables for the formal parameters argnames = [arg.id for arg in flatten(ast.formals())] argtypes = [arg.type for arg in flatten(ast.formals())] # Make the definition of this function visible within its body # to allow for recursive calls con.typings[ast.name().id] = ast.name().type con.begin_scope() # Make the formals visible con.typings.update(dict(zip(argnames, argtypes))) prior_monomorphs = con.monomorphs con.monomorphs = prior_monomorphs | set(argtypes) argtypeit = iter(argtypes) # Construct the formals types def make_type(formal): if hasattr(formal, '__iter__'): return T.Tuple(*[make_type(x) for x in iter(formal)]) else: return argtypeit.next() formals_types = make_type(ast.formals()) # XXX This makes the restriction that recursive invocations of F # in the body of F must have the same type signature. Probably # not strictly necessary, but allowing the more general case # might prove problematic, especially in tail recursive cases. # For now, we will forbid this. con.monomorphs.add(ast.name().type) # Produce all constraints for arguments # Tuple arguments, for example, will produce constraints for a in ast.formals(): for c in self.visit(a): yield c # Produce all the constraints for the body for c in self.visit_block(ast, restype, ast.body()): yield c con.monomorphs = prior_monomorphs M = set(self.context.monomorphs) yield Generalizing(ast.name().type, T.Fn(formals_types, restype), M, ast) con.end_scope()
def _Bind(self, bind): destination = bind.binder() self.box = False self.scalar = False self.rewrite_children(bind) if self.box: self.box_results.update((str(x) for x in flatten(destination))) if self.scalar: for dest in flatten(destination): self.scalar_results[str(dest)] = bind.value() return bind
def _Bind(self, bind): self.destination = bind.binder() self.sync = None self.box = False self.rewrite_children(bind) def record_sync(): for variable in self.declarations.keys(): if not variable in [x.id for x in flatten(self.destination)]: self.declarations[variable] = PT.total if not self.sync and self.pre_box: record_sync() result = [M.PhaseBoundary(list(flatten(self.pre_box))), bind] elif self.sync: record_sync() result = [self.sync, bind] else: result = bind if self.box: self.pre_box = self.destination else: self.pre_box = None return result
def _Apply(self, appl): fn = appl.function() if isinstance(fn, S.Closure): fn_name = fn.body().id else: fn_name = fn.id if fn_name in self.procedures: instantiatedFunction = self.procedures[fn_name] functionArguments = instantiatedFunction.variables[1:] instantiatedArguments = appl.parameters[1:] if isinstance(fn, S.Closure): instantiatedArguments.extend(fn.variables) env = pltools.Environment() for (internal, external) in zip(functionArguments, instantiatedArguments): env[internal.id] = external return_finder = ReturnFinder() return_finder.visit(instantiatedFunction) #XXX HACK. Need to do conditional statement->expression conversion # In order to make inlining possible if return_finder.return_in_conditional: return appl env[return_finder.return_value.id] = self.activeBinding statements = filter(lambda x: not isinstance(x, S.Return), instantiatedFunction.body()) statements = [S.substituted_expression(x, env) for x in \ statements] singleAssignmentInstantiation = single_assignment_conversion( statements, exceptions=set((x.id for x in flatten(self.activeBinding))), M=self.M) self.statements = singleAssignmentInstantiation return None return appl
def _Return(self, stmt): e = self.rewrite(stmt.value()) if isinstance(e, S.Name): # If we're returning one of the procedure formals unchanged, # we need to copy its value into a return variable. # Here is where we check: if e.id not in self.formals: #No need to copy value into a return variable stmt.parameters = [e] self.emit(stmt) return if isinstance(e, S.Tuple): # If we're returning any of the procedure formals unchanged, # we need to copy their value into a return variable # Here is where we check: formals = reduce(lambda a, b: a or b, \ (ei.id in self.formals for ei in flatten(e))) if not formals: stmt.parameters = [e] self.emit(stmt) return ret = S.Name(anonymousReturnValue.id) self.emit(S.Bind(ret, e)) stmt.parameters = [ret] self.emit(stmt)
def _Procedure(self, proc): self.rewrite_children(proc) proc.parameters = list(flatten(proc.parameters)) procedureName = proc.variables[0].id self.procedures[procedureName] = proc return proc
def _Procedure(self, ast): binders = [v.id for v in flatten(ast.variables)] # NOTE: this includes name _ClosureRecursion._Procedure(self, ast) # Take the free variable list, stick it in a set to make sure we don't # duplicate a variable, and then put it back in a list to make sure # it's got a defined ordering, which sets don't have free = list(set([v for v in S.free_variables(ast.body(), binders) if v in self.env])) if free: bound = [S.Name("_K%d" % i) for i in range(len(free))] ast.variables = ast.variables + bound ast.parameters = S.substituted_expression(ast.parameters, dict(zip(free, bound))) # Transform recursive calls of this procedure within its own body. recursive = _ClosureRecursion(self.env) self.env[ast.name().id] = S.Closure(bound, ast.name()) ast.parameters = recursive.rewrite(ast.parameters) # Register rewrite for calls to this procedure in later # parts of the defining scope self.env[ast.name().id] = S.Closure([S.Name(x) for x in free], ast.name()) # else: # self.locally_bound([ast.name()]) return ast
def state_CONVERT_ASK_LANG_MULTI(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_BACK]] if user.get_tmp_variable('IF_MULTI') == True: send_message(user, ux.MSG_ASK_LANGUAGE_MULTI, kb) else: send_message(user, ux.MSG_ASK_LANGUAGE_MONO, kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: redirect_to_state(user, state_ASK_IF_MULTILINGUAL) else: input_list = text_input.replace(' ', '') input_list = [i.lower() for i in input_list.split(',')] check_list = all(elem in iso_list for elem in input_list) if check_list: user.set_tmp_variable('LANG_LIST', input_list) user.set_tmp_variable('COUNTER', 0) redirect_to_state(user, state_CONVERT_ASK_SUBJECT) else: send_message(user, ux.MSG_WRONG_ISO_CODE) redirect_to_state(user, state_ASK_IF_MULTILINGUAL) else: send_message(user, ux.MSG_WRONG_INPUT)
def _Procedure(self, proc): self.currentProcedureName = proc.variables[0].id self.body = [] self.currentBin = self.body self.tailRecursive = False self.rewrite_children(proc) if not self.tailRecursive: return proc def convertRecursion(stmt): if not isinstance(stmt, S.Bind): return stmt if not isinstance(stmt.id, S.Name): return stmt if stmt.id.id == C.anonymousReturnValue.id: arguments = proc.variables[1:] apply = stmt.parameters[0] assert apply.parameters[0].id == self.currentProcedureName newArguments = apply.parameters[1:] return [S.Bind(x, y) for x, y in zip(arguments, newArguments)] else: return stmt recursiveBranch = list(flatten([convertRecursion(x) for x in self.recursiveBranch])) recursiveBranch = filter(lambda x: isinstance(x, S.Bind), recursiveBranch) whileLoop = M.While(self.condition, [recursiveBranch + self.body]) cleanup = self.nonrecursiveBranch proc.parameters = self.body + [whileLoop] + cleanup return proc
def _Procedure(self, proc): if proc.name().id not in self.entry_points: if not self.in_process: proc.master = False proc.entry_point = False return proc self.escaping = set() self.rewrite_children(proc) returns = S.Tuple(*[S.Name(x) for x in self.escaping if x not in self.computed]) proc.parameters.append(S.Return(returns)) for name in returns.parameters: self.computed.add(name.id) proc.returns = returns proc.master = False proc.entry_point = True proc.typings = self.typings return proc phase_args = set() for phase in proc.body(): phase_args = phase_args.union([x.id for x in phase.formals()]) phase_args = phase_args.union((x.id for x in flatten(proc.return_node.parameters))) self.phase_args = phase_args self.in_process = True self.typings = proc.typings self.rewrite_children(proc) self.in_process = False calls = [S.Bind(x.returns, S.Apply(x.name(), x.formals())) \ for x in proc.body()] proc.parameters = proc.parameters + calls + [proc.return_node] proc.master = True return proc
def state_INITIAL(user, message_obj): if message_obj is None: kb = [[ux.BUTTON_SOLVER, ux.BUTTON_GENERATOR], [ux.BUTTON_INFO]] if user.is_admin(): kb[1].insert(1, ux.BUTTON_ADMIN) send_message(user, ux.MSG_INTRO, kb) else: text_input = message_obj.text kb = user.get_keyboard() if text_input: if text_input in utility.flatten(kb): if text_input == ux.BUTTON_INFO: send_message(user, ux.MSG_INFO) elif text_input == ux.BUTTON_SOLVER: redirect_to_state(user, state_SOLVER) elif text_input == ux.BUTTON_GENERATOR: redirect_to_state(user, state_GENERATOR) elif text_input == ux.BUTTON_ADMIN: msg = "*User Stats*:\n" for a in ['telegram', 'twitter']: msg += "- {}: {}\n".format(a, ndb_user.get_user_count(a)) send_message(user, msg) else: restart_user_old_keyboard(user) elif ux.text_is_button(text_input): send_message(user, ux.MSG_WRONG_BUTTON_INPUT, kb) else: send_message(user, ux.MSG_WRONG_INPUT_USE_BUTTONS, kb) else: send_message(user, ux.MSG_WRONG_INPUT_USE_BUTTONS, kb)
def _Procedure(self, stmt): self.push() self.formals = set((x.id for x in flatten(stmt.formals()))) self.rewrite_children(stmt) self.formals = None body = self.pop() stmt.parameters = body self.emit(stmt)
def state_CHANGE_LANGUAGE_INTERFACE_EXERCISE(p, message_obj=None, **kwargs): pux = p.ux() give_instruction = message_obj is None localized_lang = lambda l: pux['LANG_{}'.format(l)] language_list = [ '{} {}'.format(v['flag'], localized_lang(k)) for k,v in sorted(LANGUAGES.items(), key=lambda x:localized_lang(x[0])) ] lang_selection_is_exercise = p.get_variable('lang_selection_type') == 'exercise' if give_instruction: kb = [[b] for b in language_list] kb.insert(0, [pux.BUTTON_BACK]) kb.append([pux.BUTTON_BACK]) if lang_selection_is_exercise: lang = p.language_exercise if p.language_exercise in LANGUAGES else params.default_language_exercise msg_lang = pux.MSG_YOU_ARE_LEARNING_X msg_select_language = pux.MSG_SELECT_NEW_LANGUAGE_EXERCISE else: lang = p.language_interface if p.language_interface in LANGUAGES else params.default_language_interface msg_lang = pux.MSG_INTERFACE_IS_IN_X msg_select_language = pux.MSG_SELECT_NEW_LANGUAGE_INTERFACE lang_info = LANGUAGES[lang] flag_lang = '{} {}'.format(lang_info['flag'],localized_lang(lang)) msg_lang = msg_lang.format(flag_lang) msg = '\n'.join([msg_lang, msg_select_language]) send_message(p, msg, kb) else: text_input = message_obj.text if text_input is None: send_message(p, pux.MSG_NOT_VALID_INPUT) return kb = p.get_keyboard() if text_input in utility.flatten(kb): if text_input in language_list: lang = next( k for k,v in LANGUAGES.items() if text_input.startswith('{} '.format(v['flag'])) ) if lang_selection_is_exercise: p.set_language_exercise(lang) else: p.set_language_interface(lang) api.update_user( p.user_telegram_id(), p.name, language_exercise = p.language_exercise, language_interface = p.language_interface ) restart(p) elif text_input == pux.BUTTON_BACK: redirect_to_state(p, state_CHANGE_LANGUAGE) else: send_message(p, p.ux().MSG_INTERFACE_CHANGED, sleep=2) restart(p) else: send_message(p, pux.MSG_NOT_VALID_INPUT)
def list_of_nearest_pings_to_tripstops(trip_id, date_time=datetime.now()): filename = 'preprocessed/nearest-pings-to-tripstops-{}.pickle'.format( trip_id) try: return read_from_pickle(filename) except: threshold_distance = 50 # If the ping is more than 50m away, it's not 'near' the bus stop trip_pings = get_pings(trip_id=trip_id, newest_datetime=date_time) cleaned_trip_pings = flatten(clean_rep(trip_pings)) trip_pings_lat_lng = [(ping.lat, ping.lng) for ping in cleaned_trip_pings] trip_tripstops = get_tripstops(trip_id=trip_id) trip_tripstops_lat_lng = [(tripstop.lat, tripstop.lng) for tripstop in trip_tripstops.itertuples()] trip_tripstops_x_y = [ p(lng, lat) for lat, lng in trip_tripstops_lat_lng ] kd_tree = get_kd_tree(trip_id, date_time=date_time) ping_indices_per_tripstop = [ kd_tree.query_ball_point(tripstop_x_y, r=threshold_distance) for tripstop_x_y in trip_tripstops_x_y ] sorted_tripstop_to_nearest_ping = [] for tripstop_index, ping_indices in enumerate( ping_indices_per_tripstop): if len(ping_indices) == 0: continue distances = [ latlng_distance(trip_tripstops_lat_lng[tripstop_index], trip_pings_lat_lng[ping_index]) for ping_index in ping_indices ] nearest_pings_timings = [ cleaned_trip_pings[ping_index].time for ping_index in ping_indices ] # Get best ping based on least distance. If tie, get the earlier ping. # TODO: Improve heuristics in choosing the best ping (affinity by time, heading) nearest_ping_index = sorted( list(zip(distances, nearest_pings_timings, ping_indices)))[0][2] nearest_ping_id = cleaned_trip_pings[nearest_ping_index].Index tripstop_id = trip_tripstops.iloc[tripstop_index].name sorted_tripstop_to_nearest_ping.append( (tripstop_id, nearest_ping_id)) # Cache data if it is more than 1 day old latest_ping_time = trip_pings.iloc[-1].time.replace(tzinfo=None) if latest_ping_time < date_time and latest_ping_time.day != date_time.day: write_to_pickle(filename, sorted_tripstop_to_nearest_ping) return sorted_tripstop_to_nearest_ping
def gather(self, suite): self.sources = [] self.gathered = set() for stmt in suite: self.clean.append(self.rewrite(stmt)) while self.sources: stmt = self.sources.pop(0) self.clean.insert(0, self.rewrite(stmt)) return list(flatten(self.clean))
def _Procedure(self, stmt): self.env.begin_scope() for var in flatten(stmt.variables): self.env[var.id] = var result = self.rewrite_children(stmt) self.env.end_scope() return result
def state_GENERATOR(user, message_obj): if message_obj is None: kb = [ [ux.BUTTON_SOLUTION], [ux.BUTTON_BACK], ] clues, solution, explanations = generator.get_ghigliottina() user.set_var( 'GHIGLIOTTINA', { 'CLUES': clues, 'SOLUTION': solution, 'EXPLANATIONS': explanations, 'TIME': utility.get_now_seconds() }) msg = ux.MSG_GENERATOR_INTRO.format(', '.join(clues)) send_message(user, msg, kb) else: kb = user.get_keyboard() if message_obj.text: text_input = message_obj.text solution = user.get_var('GHIGLIOTTINA')['SOLUTION'] explanations = '\n'.join( '- {}'.format(e) for e in user.get_var('GHIGLIOTTINA')['EXPLANATIONS']) if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: restart_user(user) elif text_input == ux.BUTTON_SOLUTION: ellapsed_sec = utility.get_now_seconds() - user.get_var( 'GHIGLIOTTINA')['TIME'] if ellapsed_sec < 60: send_message(user, ux.MSG_TOO_EARLY) else: send_message(user, ux.MSG_SOLUTION.format(solution)) send_typing_action(user, sleep_secs=0.5) send_message(user, ux.MSG_EXPLANATIONS.format(explanations)) send_typing_action(user, sleep_secs=1) redirect_to_state(user, state_CONTINUE) else: restart_user_old_keyboard(user) elif ux.text_is_button(text_input): send_message(user, ux.MSG_WRONG_BUTTON_INPUT) else: correct = text_input.upper() == solution msg = ux.MSG_GUESS_OK if correct else ux.MSG_GUESS_KO send_message(user, msg) if correct: send_typing_action(user, sleep_secs=0.5) send_message(user, ux.MSG_EXPLANATIONS.format(explanations), remove_keyboard=True) send_typing_action(user, sleep_secs=1) redirect_to_state(user, state_CONTINUE) else: send_message(user, ux.MSG_WRONG_INPUT_USE_BUTTONS, kb)
def _Procedure(self, proc): proc_id = proc.name().id self.env[proc_id] = proc_id self.env.begin_scope() for param in flatten(proc.formals()): id = param.id self.env[id] = id self.rewrite_children(proc) self.env.end_scope() return proc
def _Bind(self, bind): destination = bind.binder() if isinstance(destination, S.Tuple): for dest in flatten(destination): self.env[dest.id] = dest.id else: id = destination.id self.env[id] = id self.rewrite_children(bind) return bind
def _Procedure(self, proc): self.locals.begin_scope() for x in proc.formals(): #Tuples may be arguments to procedures #Mark all ids found in each formal argument for y in flatten(x): self.locals[y.id] = True self.rewrite_children(proc) self.locals.end_scope() proc.variables = map(S.mark_user, proc.variables) return proc
def _deliver(self, server, msg, tracker): from_addr, to_addrs = self.__extract_addrs(msg) flattened_msg = flatten(msg) failures = {} try: tracker.update(_('Delivering %s' % msg['Subject'])) failures = server.sendmail(from_addr, to_addrs, flattened_msg) except: tracker.error_delivering(msg) return None return failures
def send_photo_png_data(p, file_data, filename): if p.isTelegramUser(): main_telegram.sendPhotoFromPngImage(p.chat_id, file_data, filename) else: main_fb.sendPhotoData(p, file_data, filename) # send message to show kb kb = p.getLastKeyboard() if kb: msg = 'Opzioni disponibili:' kb_flat = utility.flatten(kb)[:11] # no more than 11 main_fb.sendMessageWithQuickReplies(p, msg, kb_flat)
def _Bind(self, stmt): var = stmt.binder() varNames = [x.id for x in flatten(var)] operation = S.substituted_expression(stmt.value(), self.env) for name in varNames: if name not in self.exceptions: rename = '%s_%s' % (name, self.serial.next()) else: rename = name self.env[name] = S.Name(rename) result = S.Bind(S.substituted_expression(var, self.env), operation) return result
def _Procedure(self, proc): # XXX Phase analysis only works for parallel procedures - fix! if not hasattr(proc, 'context'): return proc if proc.context is not I.distributed: return proc self.declarations = dict(((x.id, PT.total) for x in proc.formals())) self.rewrite_children(proc) proc.parameters = list(flatten(proc.parameters)) self.declarations = None #Construct phase for this procedure? return proc
def send_message(p, msg, kb=None, markdown=True, inline_keyboard=False, one_time_keyboard=False, sleepDelay=False, hide_keyboard=False, force_reply=False, disable_web_page_preview=False): if p.isTelegramUser(): return main_telegram.send_message(p, msg, kb, markdown, inline_keyboard, one_time_keyboard, sleepDelay, hide_keyboard, force_reply, disable_web_page_preview) else: if kb is None: kb = p.getLastKeyboard() if kb: kb_flat = utility.flatten(kb)[:11] # no more than 11 return main_fb.sendMessageWithQuickReplies(p, msg, kb_flat) else: return main_fb.sendMessage(p, msg)
def _Apply(self, apply): for arg in apply.arguments(): if isinstance(arg, S.Name): if arg.id not in self.declarations: self.declarations[arg.id] = PT.none def name_filter(x): if isinstance(x, S.Name): return self.declarations[x.id] else: return PT.total completions = [name_filter(x) for x in apply.arguments()] fn_name = apply.function().id fn_phase = apply.function().cu_phase input_phases, output_completion = \ fn_phase(*completions) for name in flatten(self.destination): self.declarations[name.id] = output_completion sync = [] if hasattr(apply.function(), 'box'): self.box = True for x in apply.arguments(): # XXX Special case for dealing with zip - # The unzip transform pushes tuple args around the AST # This needs to be rethought # Right now it will only work for zip as argument to Box # functions. # The right thing to do is generate host code for this # But we need to transition the entire entry-point procedure # to C++ on the host in order to do this. if hasattr(x, '__iter__'): for xi in x: sync.append(xi.id) else: sync.append(x.id) else: for x, y in zip(apply.arguments(), input_phases): if isinstance(x, S.Name): x_phase = self.declarations[x.id] if x_phase is PT.unknown: self.declarations[x.id] = y elif x_phase < y: sync.append(x.id) if sync: self.sync = M.PhaseBoundary(sync) return apply
def send_photo_url(p, url, kb=None): if p.isTelegramUser(): main_telegram.sendPhotoViaUrlOrId(p.chat_id, url, kb) else: #main_fb.sendPhotoUrl(p.chat_id, url) import requests file_data = requests.get(url).content main_fb.sendPhotoData(p, file_data, 'file.png') # send message to show kb kb = p.getLastKeyboard() if kb: msg = 'Opzioni disponibili:' kb_flat = utility.flatten(kb)[:11] # no more than 11 main_fb.sendMessageWithQuickReplies(p, msg, kb_flat)
def collect_local_typings(suite, M): """ This compiler pass may be used to annotate every Procedure node with a list of typings for its locally declared variables. This pass implicitly assumes that the program has already been run through all single assignment, typing, and flattening phases of the frontend compiler. """ def select(A, kind): return ifilter(lambda x: isinstance(x, kind), A) M.fn_types = dict() for fn in select(suite, AST.Procedure): fntype = fn.name().type fnname = str(fn.name()) if fnname in M.inputTypes: M.fn_types[fnname] = fntype if isinstance(fntype, T.Polytype): fntype = fntype.monotype() input_type_tuple = fntype.parameters[0] input_types = input_type_tuple.parameters localtypes = dict(zip((x.id for x in flatten(fn.formals())), flatten(input_types))) def record_bindings(body): for binding in select(body, AST.Bind): for x in select(AST.walk(binding.binder()), AST.Name): localtypes[x.id] = x.type for cond in select(body, AST.Cond): for cbody in cond.parameters[1:]: record_bindings(cbody) record_bindings(fn.body()) fn.typings = localtypes return suite
def get_operating_trip_ids(date_time=datetime.now()): sql = """ SELECT "tripId" FROM "tripStops" GROUP BY "tripId" HAVING %(date_time)s >= MIN(time) - INTERVAL '15 minutes' AND %(date_time)s <= MAX(time) + INTERVAL '15 minutes' """ data = {'date_time': date_time} records = query(sql, data=data, column_names=['tripId'], pandas_format=False) return flatten(records)
def state_CONVERT_ASK_ONTOLOGY_NAME(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_BACK]] send_message(user, ux.MSG_ASK_ONTOLOGY_NAME, kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: redirect_to_state(user, state_CONVERT_ASK_ONTOLOGY_YES_NO) else: user.set_tmp_variable('ONTOLOGY_NAME', text_input) redirect_to_state(user, state_CONVERT_ASK_ONTOLOGY_LINK) else: send_message(user, ux.MSG_WRONG_INPUT)
def state_INITIAL(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_START_CONVERSION]] send_message(user, ux.MSG_INTRO, kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_START_CONVERSION: user.reset_tmp_variables() redirect_to_state(user, state_ASK_IF_MULTILINGUAL) else: send_message(user, ux.MSG_WRONG_INPUT, kb) else: send_message(user, ux.MSG_WRONG_INPUT)
def state_CONVERT_ASK_SUBJECT(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_BACK]] send_message(user, ux.MSG_ASK_SUBJECT, kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: redirect_to_state(user, state_CONVERT_ASK_LANG) else: user.set_tmp_variable('SUBJECT', text_input) redirect_to_state(user, state_CONVERT_ASK_ID_PREFIX) else: send_message(user, ux.MSG_WRONG_INPUT)
def state_CONVERT_ASK_ID_PREFIX(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_BACK]] subject_upper_2_char = user.get_tmp_variable('SUBJECT').upper()[:2] send_message(user, ux.MSG_ASK_ID_PREFIX.format(subject_upper_2_char), kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: redirect_to_state(user, state_CONVERT_ASK_SUBJECT) else: user.set_tmp_variable('ID_PREFIX', text_input) redirect_to_state(user, state_CONVERT_ASK_ONTOLOGY_YES_NO) else: send_message(user, ux.MSG_WRONG_INPUT)
def get_kd_tree(trip_id, date_time=datetime.now()): kd_tree_filename = 'preprocessed/kdtree-pings-{}.pickle'.format(trip_id) kd_tree = None try: kd_tree = read_from_pickle(kd_tree_filename) except: trip_pings = get_pings(trip_id=trip_id, newest_datetime=date_time) cleaned_trip_pings = flatten(clean_rep(trip_pings)) if len(cleaned_trip_pings) == 0: return None trip_pings_x_y = [p(ping.lng, ping.lat) for ping in cleaned_trip_pings] kd_tree = cKDTree(trip_pings_x_y) # Cache data if it is more than 1 day old latest_ping_time = trip_pings.iloc[-1].time.replace(tzinfo=None) if latest_ping_time < date_time and latest_ping_time.day != date_time.day: write_to_pickle(kd_tree_filename, kd_tree) return kd_tree
def state_CONVERT_ASK_ONTOLOGY_LINK(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_BACK]] send_message(user, ux.MSG_ASK_ONTOLOGY_LINK, kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: redirect_to_state(user, state_CONVERT_ASK_ONTOLOGY_NAME) else: user.set_tmp_variable('ONTOLOGY_LINK', text_input) if user.get_tmp_variable('IF_MULTI') == True: redirect_to_state(user, state_CONVERT_ASK_DOC_MULTI) elif user.get_tmp_variable('IF_MULTI') == False: redirect_to_state(user, state_CONVERT_ASK_DOC_MULTI) else: send_message(user, ux.MSG_WRONG_INPUT)
def _Bind(self, stmt): var = stmt.binder() varNames = [x.id for x in flatten(var)] operation = S.substituted_expression(stmt.value(), self.env) for name in varNames: if self.freeze: if name in self.env: rename = self.env[name] elif name not in self.exceptions: rename = markGenerated('%s_%s' % (name, SingleAssignmentRewrite.serial.next())) else: rename = name elif name not in self.exceptions: rename = markGenerated('%s_%s' % (name, SingleAssignmentRewrite.serial.next())) else: rename = name self.env[name] = S.Name(rename) result = S.Bind(S.substituted_expression(var, self.env), operation) return result
def _Lambda(self, e): _ClosureRecursion._Lambda(self, e) formals = [v.id for v in flatten(e.formals())] # Take the free variable list, stick it in a set to make sure we don't # duplicate a variable, and then put it back in a list to make sure # it's got a defined ordering, which sets don't have free = list(set([v for v in S.free_variables(e.body(), formals) if v in self.env])) if free: bound = [S.Name("_K%d" % i) for i in range(len(free))] body = S.substituted_expression(e.body(), dict(zip(free, bound))) e.parameters = [body] e.variables = e.variables + bound return S.Closure([S.Name(x) for x in free], e) else: return e
def state_ASK_IF_MULTILINGUAL(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_YES, ux.BUTTON_NO],[ux.BUTTON_BACK,]] send_message(user, ux.MSG_ASK_IF_MULTILINGUAL, kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: redirect_to_state(user, state_INITIAL) elif text_input == ux.BUTTON_YES: user.set_tmp_variable('IF_MULTI', True) redirect_to_state(user, state_CONVERT_ASK_LANG_MULTI) elif text_input == ux.BUTTON_NO: user.set_tmp_variable('IF_MULTI', False) redirect_to_state(user, state_CONVERT_ASK_LANG_MULTI) else: send_message(user, ux.MSG_WRONG_INPUT) else: send_message(user, ux.MSG_WRONG_INPUT)
def state_CONVERT_ASK_DOC_MONO(user, message_obj=None, **kwargs): if message_obj is None: kb = [[ux.BUTTON_BACK]] send_message(user, ux.MSG_SEND_CSV, kb) else: text_input = message_obj.text if text_input: kb = user.get_keyboard() if text_input in utility.flatten(kb): if text_input == ux.BUTTON_BACK: ontology_enabled = user.get_tmp_variable('ONTOLOGY_NAME') is not None if ontology_enabled: redirect_to_state(user, state_CONVERT_ASK_ONTOLOGY_LINK) else: redirect_to_state(user, state_CONVERT_ASK_ONTOLOGY_YES_NO) else: send_message(user, ux.MSG_SEND_FILE_NO_TEXT) elif message_obj.document: deal_with_document_request(user, message_obj.document) else: send_message(user, ux.MSG_SEND_FILE_NO_TEXT)
def state_CONTINUE(user, message_obj): if message_obj is None: kb = [[ux.BUTTON_YES, ux.BUTTON_NO]] send_message(user, ux.MSG_CONTINUE, kb) else: kb = user.get_keyboard() if message_obj.text: text_input = message_obj.text if text_input in utility.flatten(kb): if text_input == ux.BUTTON_YES: redirect_to_state(user, state_GENERATOR) elif text_input == ux.BUTTON_NO: restart_user(user) else: restart_user_old_keyboard(user) elif ux.text_is_button(text_input): send_message(user, ux.MSG_WRONG_BUTTON_INPUT) else: send_message(user, ux.MSG_WRONG_INPUT_USE_BUTTONS, kb) else: send_message(user, ux.MSG_WRONG_INPUT_USE_BUTTONS, kb)
def state_CHANGE_LANGUAGE(p, message_obj=None, **kwargs): pux = p.ux() give_instruction = message_obj is None if give_instruction: localized_lang = lambda l: pux['LANG_{}'.format(l)] lang_exercise = p.language_exercise lang_interface = p.language_interface flag_lang_exercise = '{} {}'.format(LANGUAGES[lang_exercise]['flag'],localized_lang(lang_exercise)) flag_lang_interface = '{} {}'.format(LANGUAGES[lang_interface]['flag'],localized_lang(lang_interface)) msg = '\n'.join([ pux.MSG_YOU_ARE_LEARNING_X.format(flag_lang_exercise), pux.MSG_INTERFACE_IS_IN_X.format(flag_lang_interface), '', pux.MSG_CHANGE_LANGUAGE_INTERFACE_EXERCISE ]) kb = [ [pux.BUTTON_LANGUAGE_INTERFACE, pux.BUTTON_LANGUAGE_EXERCISE], [pux.BUTTON_CANCEL], ] send_message(p, msg, kb) else: text_input = message_obj.text if text_input is None: send_message(p, pux.MSG_NOT_VALID_INPUT) return kb = p.get_keyboard() if text_input in utility.flatten(kb): if text_input == pux.BUTTON_LANGUAGE_INTERFACE: p.set_variable('lang_selection_type', 'interface') redirect_to_state(p, state_CHANGE_LANGUAGE_INTERFACE_EXERCISE) elif text_input == pux.BUTTON_LANGUAGE_EXERCISE: p.set_variable('lang_selection_type', 'exercise') redirect_to_state(p, state_CHANGE_LANGUAGE_INTERFACE_EXERCISE) elif text_input == pux.BUTTON_CANCEL: restart(p) else: send_message(p, p.ux().MSG_INTERFACE_CHANGED, sleep=2) restart(p) else: send_message(p, pux.MSG_NOT_VALID_INPUT)
def _Apply(self, apply): functionName = apply.parameters[0].id if functionName in self.procedures: instantiatedFunction = self.procedures[functionName] functionArguments = instantiatedFunction.variables[1:] instantiatedArguments = apply.parameters[1:] env = pltools.Environment() for (internal, external) in zip(functionArguments, instantiatedArguments): env[internal.id] = external return_finder = ReturnFinder(self.activeBinding, env) return_finder.visit(instantiatedFunction) statements = [S.substituted_expression(x, env) for x in \ instantiatedFunction.body() \ if not isinstance(x, S.Return)] singleAssignmentInstantiation = single_assignment_conversion( statements, exceptions=set((x.id for x in flatten(self.activeBinding)))) self.statements = singleAssignmentInstantiation return None return apply
def _Bind(self, x): names = U.flatten(x.binder()) result = list(self.visit(x.value())) for name in names: self.env[name.id] = name.id return result
def _Apply(self, appl): fn = appl.function() if isinstance(fn, S.Closure): fn_name = fn.body().id else: fn_name = fn.id if fn_name in self.procedures: instantiatedFunction = self.procedures[fn_name] functionArguments = instantiatedFunction.variables[1:] instantiatedArguments = appl.parameters[1:] if isinstance(fn, S.Closure): instantiatedArguments.extend(fn.variables) env = pltools.Environment() for (internal, external) in zip(functionArguments, instantiatedArguments): env[internal.id] = external return_finder = ReturnFinder() return_finder.visit(instantiatedFunction) #XXX HACK. Need to do conditional statement->expression conversion # In order to make inlining possible if return_finder.return_in_conditional: return appl env[return_finder.return_value.id] = self.activeBinding statements = filter(lambda x: not isinstance(x, S.Return), instantiatedFunction.body()) statements = [S.substituted_expression(x, env) for x in \ statements] singleAssignmentInstantiation = single_assignment_conversion(statements, exceptions=set((x.id for x in flatten(self.activeBinding))), M=self.M) self.statements = singleAssignmentInstantiation return None return appl
def _Cond(self, cond): body = list(flatten(self.rewrite(cond.body()))) orelse = list(flatten(self.rewrite(cond.orelse()))) return S.Cond(cond.test(), body, orelse)
def inline(s, M): inliner = FunctionInliner(M) inlined = list(flatten(inliner.rewrite(s))) return inlined
def locally_bound(self, B): for v in flatten(B): self.env[v.id] = v.id
def __init__(self, binding, env): self.binding = list(flatten(binding)) self.env = env
def _Return(self, node): val = list(flatten(node.value())) assert(len(val) == len(self.binding)) for b, v in zip(self.binding, val): self.env[v.id] = b
def _Apply(self, apply): functionName = apply.parameters[0].id if functionName in self.procedures: instantiatedFunction = self.procedures[functionName] functionArguments = instantiatedFunction.variables[1:] instantiatedArguments = apply.parameters[1:] env = pltools.Environment() for (internal, external) in zip(functionArguments, instantiatedArguments): env[internal.id] = external return_finder = ReturnFinder(self.activeBinding, env) return_finder.visit(instantiatedFunction) statements = [S.substituted_expression(x, env) for x in \ instantiatedFunction.body() \ if not isinstance(x, S.Return)] singleAssignmentInstantiation = single_assignment_conversion(statements, exceptions=set((x.id for x in flatten(self.activeBinding)))) self.statements = singleAssignmentInstantiation return None return apply
def inline(s): inliner = FunctionInliner() return list(flatten(inliner.rewrite(s)))