def api_cities(): ''' Return the list of cities. ''' cities = list(srv.get_cities( slug=request.args.get('city_slug'), country=request.args.get('country'), provider=request.args.get('provider'), predictable=request.args.get('predictable'), active=request.args.get('active') )) return jsonify(cities)
def init_station_dataframe(city, station, moment, blank): click.secho('city: {}'.format(city), fg='yellow', bold=True) click.secho('station: {}'.format(station), fg='yellow', bold=True) click.secho('moment: {}'.format(moment), fg='yellow') click.secho('blank: {}'.format(blank), fg='blue', bold=True) SINCE = moment FILTERED_DATES = [SINCE + dt.timedelta(**moment) for moment in MOMENTS] UNTIL = FILTERED_DATES[-1] util.notify('Querying {city} in database...'.format(city=city), 'cyan') # Make sure the city exists try: CITY = next(srv.get_cities(slug=city, serialized=False)) except CityNotFound as exc: util.notify(exc, 'red') sys.exit() # Make sure the city is predictable if not CITY.predictable: util.notify(CityUnpredicable, 'red') sys.exit() util.notify('Querying {city} stations in database...'.format(city=city), 'cyan') # Search city station STATION = next( srv.get_stations(slug=station, city_slug=CITY.slug, serialized=False)) # Get bike updates if not blank: util.notify('Getting station updates...', 'cyan') try: df = STATION.get_updates(SINCE, UNTIL) except Exception as exc: util.notify(exc, 'red') sys.exit() util.notify('Generating dataframe...', 'cyan') # Create dataframe columns = dict(bikes=[ find_tuple_according_moment(df, moment).bikes for moment in FILTERED_DATES ] if not blank else [''] * len(FILTERED_DATES), station=[station] * len(FILTERED_DATES), city=[city] * len(FILTERED_DATES)) index = FILTERED_DATES column_order = ['city', 'station', 'bikes'] return pd.DataFrame(columns, index)[column_order]
def retrieve_city_weather(city, since, until): # Make sure the city exists try: util.notify('Querying {city} in database...'.format(city=city), 'cyan') CITY = next(srv.get_cities(slug=city, serialized=False)) except CityNotFound as exc: util.notify(exc, 'red') sys.exit() df = CITY.get_weather_updates(since, until) df['city'] = [CITY.slug] * len(df.index) return df
def get_city_stations(city): click.secho('city: {}'.format(city), fg='yellow', bold=True) util.notify('Querying {city} in database...'.format(city=city), 'cyan') # Make sure the city exists try: CITY = next(srv.get_cities(name=city, serialized=False)) except CityNotFound as exc: util.notify(exc, 'red') sys.exit() # Make sure the city is predictable if not CITY.predictable: util.notify(CityUnpredicable, 'red') sys.exit() util.notify('Querying {city} stations in database...'.format(city=city), 'cyan') # Search city stations return srv.get_stations(city_slug=CITY.slug, serialized=False)
import pandas as pd from app import services as srv PARSER = argparse.ArgumentParser() PARSER.add_argument('city', type=str, help='City for which to run the analysis') PARAMS = PARSER.parse_args() SINCE = dt.datetime(year=2015, month=10, day=1) UNTIL = dt.datetime.now() # Generate all the dates between SINCE and UNTIL DAYS = [SINCE + dt.timedelta(days=d) for d in range((UNTIL - SINCE).days + 1)] city = next(srv.get_cities(name=PARAMS.city, serialized=False)) stations = list(srv.get_stations(city_slug=city.slug, serialized=False)) empty = pd.DataFrame(index=DAYS, columns=(station.name for station in stations)) full = pd.DataFrame(index=DAYS, columns=(station.name for station in stations)) for station in stations: df = station.get_updates(SINCE, UNTIL) df['moment'] = df.index df['day'] = df['moment'].apply(lambda x: x.date().isoformat()) # Extract the time between each observation df['duration'] = df['moment'].diff().shift(-1) for day, group in df.groupby('day'): empty[station.name][day] = group[group['bikes'] == 0]['duration'].sum() full[station.name][day] = group[group['spaces'] == 0]['duration'].sum()