Esempio n. 1
0
def group_tweets_by_state(tweets):
    """Return a database that aggregates tweets by their nearest state center.

    The keys of the returned database are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> get_len(two_tweets_by_state)
    2
    >>> california_tweets = get_value_from_key(two_tweets_by_state, 'CA')
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = make_states_database_with_empty_lists()
    "*** YOUR CODE HERE ***"
    for i in range(len(tweets)):
        state_of_tweet = closest_state_to_tweet(tweets[i])
        state_list = get_value_from_key(tweets_by_state, state_of_tweet)
        state_list.append(tweets[i])
        tweets_by_state = add_value(tweets_by_state, state_of_tweet, state_list)
    final_tweets_by_state_list = make_database()
    for i in range(get_len(tweets_by_state)):
        if get_values(tweets_by_state)[i] != []:
            final_tweets_by_state_list = add_value(final_tweets_by_state_list, get_keys(tweets_by_state)[i], get_values(tweets_by_state)[i])

    return final_tweets_by_state_list
Esempio n. 2
0
def group_tweets_by_state(tweets):
    """Return a database that aggregates tweets by their nearest state center.

    The keys of the returned database are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> get_len(two_tweets_by_state)
    2
    >>> california_tweets = get_value_from_key(two_tweets_by_state, 'CA')
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    tweets_by_state = make_states_database_with_empty_lists()
    "*** YOUR CODE HERE ***"
    for i in range(len(tweets)):
        state_of_tweet = closest_state_to_tweet(tweets[i])
        state_list = get_value_from_key(tweets_by_state, state_of_tweet)
        state_list.append(tweets[i])
        tweets_by_state = add_value(tweets_by_state, state_of_tweet,
                                    state_list)
    final_tweets_by_state_list = make_database()
    for i in range(get_len(tweets_by_state)):
        if get_values(tweets_by_state)[i] != []:
            final_tweets_by_state_list = add_value(
                final_tweets_by_state_list,
                get_keys(tweets_by_state)[i],
                get_values(tweets_by_state)[i])

    return final_tweets_by_state_list
Esempio n. 3
0
def closest_state_to_tweet(tweet):
    #takes in a tweet and returns the two-letter state postal code for the
    #closest state center to that tweet
    database = us_states
    new_dynamic_database = make_database()
    list_of_keys = get_keys(database)
    for i in range(get_len(database)):
        dist = geo_distance(tweet_location(tweet), find_state_center(get_value_from_key(database, list_of_keys[i])))
        new_dynamic_database = add_value(new_dynamic_database, list_of_keys[i], dist)
    distances_from_states = get_values(new_dynamic_database)
    closest_state = ""
    for i in range(get_len(new_dynamic_database)):
        if get_value_from_key(new_dynamic_database, get_keys(new_dynamic_database)[i]) == min(distances_from_states):
            closest_state = get_keys(new_dynamic_database)[i]
    return closest_state
Esempio n. 4
0
def closest_state_to_tweet(tweet):
    #takes in a tweet and returns the two-letter state postal code for the
    #closest state center to that tweet
    database = us_states
    new_dynamic_database = make_database()
    list_of_keys = get_keys(database)
    for i in range(get_len(database)):
        dist = geo_distance(
            tweet_location(tweet),
            find_state_center(get_value_from_key(database, list_of_keys[i])))
        new_dynamic_database = add_value(new_dynamic_database, list_of_keys[i],
                                         dist)
    distances_from_states = get_values(new_dynamic_database)
    closest_state = ""
    for i in range(get_len(new_dynamic_database)):
        if get_value_from_key(new_dynamic_database,
                              get_keys(new_dynamic_database)[i]) == min(
                                  distances_from_states):
            closest_state = get_keys(new_dynamic_database)[i]
    return closest_state
Esempio n. 5
0
def fetch_and_save_reports(reports, queries):
	results = []
	start = None
	total_records = 0

	report_date = get_report_date()
	for report in reports:
		try:
			start = get_report_date(date_format="%H:%M:%S", days=0)

			query = queries.get(report)
			query = query.format(report_date=report_date)
			
			data = get_values(query, db_config)
			content = prepare_csv_data(report, data)
			save_csv_report(report, content, report_date)
			# print content

			total_records = len(data)
			results.append({
				"report": report_names.get(report),
				"is_sucessful": True,
				"start": start,
				"end": get_report_date(date_format="%H:%M:%S", days=0),
				"total_records": total_records,
				"result": "CSV Created"
			});
		except Exception, e:
			import traceback
			print e
			print traceback.print_exc()
			results.append({
				"report": report_names.get(report),
				"is_sucessful": False,
				"start": start or "",
				"end": get_report_date(date_format="%H:%M:%S", days=0),
				"total_records": total_records,
				"result": "Error while creating CSV"
			});
Esempio n. 6
0
def update_model():

    try:

        country_code = request.get_json()['country_code']

        data, timestamps = get_values(country_code)

        path = MODELS_FOLDER + country_code

        job_train = q_train.enqueue(train, args=(
            path,
            data,
        ))
        job_predict = q_predict.enqueue(predict,
                                        depends_on=job_train,
                                        args=(path, data[-7:], timestamps,
                                              country_code))

    except Exception as e:

        return {'success': False}

    return {'success': True}