def test_drought_rate_data_multiple(): logging.debug("started") year = 2020 logging.debug(f"year={year}") stdf = utils.load_stations() stations = stdf.index.get_level_values('station') stations_selected = stations[stations.str.startswith('FR')] logging.debug(f"{len(stations_selected)} stations selected") tstarted = datetime.utcnow() for stid in tqdm(stations_selected, total=len(stations_selected)): logging.debug(f"stid={stid}") rdf, cprcp, curr_drought_rate, curr_fillrate, curr_fillrate_cdf = utils.drought_rate_data( stid, year) tcompleted = datetime.utcnow() logging.debug(f"rdf=DataFrame({rdf.shape})") logging.debug(f"cprcp=DataFrame({cprcp.shape})") logging.debug(f"curr_drought_rate={curr_drought_rate}") logging.debug(f"curr_fillrate={curr_fillrate}") logging.debug(f"curr_fillrate_cdf={curr_fillrate_cdf}") logging.debug(f"completed in {(tcompleted - tstarted)}")
def main(): # Declare bot updater and command dispatcher. updater = Updater(utils.BOT_TOKEN, use_context=True) dp = updater.dispatcher dp.add_handler(CommandHandler('stations', get_stations)) dp.add_handler(CommandHandler('time', get_timetable)) dp.add_handler(CommandHandler('help', command_help)) # Load stations from JSON file. global station_data station_data = utils.load_stations() if station_data is None: logger.error("Could not open train stations data file.") return # Start the Telegram bot. updater.start_polling() logger.info("RenfyBot has been correctly started.") # Run until SIGINT. updater.idle()
def cat_me(number): stations = load_stations() # set init positions cats, owners = set_init_positions(number, stations) number_cats_found = 0 owners_moves = [] # look for cats for i in range(100000): for obj_id in range(len(cats)): cat_found_in_station = False cat = find_cat_owner_by_id(cats, obj_id) owner = find_cat_owner_by_id(owners, obj_id) if not cat: continue current_owner_station = owner.move(stations) cat.move(stations) for pair in current_owner_station.owners_found_cats_in_station(): cat_found_in_station = True number_cats_found = number_cats_found + 1 print 'Owner {} found cat {} - {} is now closed.'.format( pair, pair, current_owner_station) # close station if cat_found_in_station: owners_moves.append(owner.moves) stations = current_owner_station.close_station(stations) cats.remove(cat) owners.remove(owner) print 'Total number of cats: {}'.format(number) print 'Number of cats found: {}'.format(number_cats_found) print 'Average number of movements required to find a cat: {}'.format( float(sum(owners_moves))/max(len(owners_moves), 1))
def get_perimeter(stdf: pd.DataFrame) -> float: perimeter = stdf.perimeter_km[stdf.dispatched_at.isnull()].min() return perimeter logging.basicConfig( level=logging.INFO, format=constants.logfmt, handlers=[logging.StreamHandler()], ) HOST = '' PORT = 50007 engine = utils.sql_engine() stations = utils.load_stations() logging.info("calculating stations TODO with perimeter") stations_todo = utils.get_stations_noref(engine, stations) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) pm = get_perimeter(stations_todo) logging.info( f"server listening on {HOST} {PORT}, {len(stations_todo)} stations todo, perimeter={pm:.1f}km." ) while stations_todo['dispatched_at'].isnull().sum( ) > 0: # after one request is served, listen for another one s.listen(1) conn, addr = s.accept() with conn: logging.debug(f"Connected by {addr}") while True: # until the socket is terminated by the worker
def test_load_stations(): sdf = utils.load_stations() cz = sdf['country_code'] == 'EZ' for s in sdf.loc[cz, :].itertuples(): logging.debug(s) assert {'station', 'country_code'}.issubset(set(sdf.columns))