Commit abeb0ab6 authored by Danielvdh03's avatar Danielvdh03
Browse files

added comments plus implemented date filtering for count_tweet_region

parent a902bb01
......@@ -43,7 +43,7 @@ def cleanup_type(text):
text = text[1:] # removes b
return text
# Hardcoded descriptions for when user runs help command
# Hardcoded list of available commands for when user runs help command
def generate_commands():
......@@ -52,6 +52,8 @@ def generate_commands():
"help", "quit")
return commands
# Hardcoded list of available descriptions when running help command
def generate_descriptions():
descriptions = ("Enter in a state to view the total number of votes in the 2020 election. Option \"All\" to view votes in descneding order by state",
......@@ -108,92 +110,13 @@ class Database:
return state_id
# Creates a Table based off user input
def create_table(self):
table_name = input("Enter name of new table : ")
if table_name in self.table_list:
print("Error : Name of table already exists.")
return
else:
type = []
max_length = []
num_floating = []
null = []
pk = []
print ("Make sure the fields you entered below are seperated by commas (ex. \"first_name, last_name, age\")")
input_string = input("Enter all fields that you want in table {0} seperated by"
" commas (each field can only be one word) : \n".format(table_name))
attributes = input_string.split(",")
attributes = trim(attributes) # Removes white space and unnecessary characters like , ' ( )
for attribute in attributes: # Gets specific info on each attribute
new_type = input("Enter the type of attribute {0} (Integer, Decimal, Date, Character, Boolean) : ".format(attribute))
new_type = new_type.lower() # No longer case sensitive
new_type, new_length, new_floating = self.handle_type_insert(new_type)
type.append(new_type)
max_length.append(new_length)
num_floating.append(new_floating)
user = input("Do you want to add a Primary Key to the table (Y/N) : ")
if user == "Y": # Adds primary key
print("Attributes created are : {0}".format(attributes))
input_pk = input("Enter attributes that you wish to use as Primary Key (separate by commas) : \n")
pk = input_pk.split(",")
query = self.generate_create_query(table_name, attributes, type, max_length, num_floating, pk)
# print(query) # For debugging purposes
results = self.execute_query(query)
print(results)
self.table_list.append(table_name) # Add to list of tables
# Generates create query based off user information provided
# Used to limit results of query
def limit_results(self):
num = input("Maximum results to be returned : ")
return "LIMIT {0}".format(num)
# Given a valid table name, prints out all atributes and associated types
def show_columns(self):
table_name = input("Please select table to view columns of : ")
field_names = []
types = []
if table_name in self.table_list:
field_names, types = self.get_column_and_type(table_name)
else:
print("Table does not exist")
return
print("Field Name, Type")
for field_name, type in zip(field_names, types):
type = cleanup_type(type)
print("{0},{1}".format(field_name, type))
# Prints out the list of all commands for help
def display_command_list(self):
......@@ -204,8 +127,7 @@ class Database:
print("{0} - {1}".format(command, description))
# Prints list of all tables
# Executes query and prints out if there is an error
def execute_query(self, query: str):
......@@ -218,6 +140,7 @@ class Database:
print("Something went wrong: {}".format(err))
#
def get_votes_state(self):
state = input("Enter state that you wish to filter by or All : ")
......@@ -335,7 +258,7 @@ class Database:
print("{2}. Username : {0}, Follower Count : {1}".format(result[0], result[1], counter))
counter += 1
return
# results county command
def results_county(self):
state = input("Enter name of state that you wish to search in : ")
......@@ -361,6 +284,7 @@ class Database:
print("{0}. Candidate Name : {1} Party Affiliation : {2} Vote Count : {3}".format(counter, result[0], result[1], result[2]))
counter +=1
# Some categories have a space in them. Converts spacing to database appropriate names
def sanitize_categories(self, input: str):
input = input.lower()
......@@ -382,8 +306,9 @@ class Database:
elif input == "family work":
return "family_work"
else:
return input
return input # leave as is
# group_county_by command
def group_county_by(self):
state = input("Select state to filter results by or (All) :")
......@@ -442,7 +367,8 @@ class Database:
print("{0}. County : {1}, State : {2}, {3} : {4}".format(counter, result[0], state_name, old_choice, result[1]))
counter += 1
#TODO : Add Date filtering
# get_tweet_candidate command, returns all tweets above a certain like threshold about a candidate in a specific county
def get_tweet_candidate(self):
candidate_name = input("Enter the candidate name (Joe Biden/ Donald Trump) : ")
......@@ -453,7 +379,7 @@ class Database:
state = state.strip()
region = input("Enter the county you wish to query by : ")
region = region.strip()
likes = input("Input the minimum amount of likes to query by")
likes = input("Input the minimum amount of likes to query by : ")
likes = likes.strip()
......@@ -472,6 +398,7 @@ class Database:
print("Username : {0}, Tweet : {1}, Likes : {2} ".format(result[0], result[1], result[2]))
#implements count_tweet_region
def count_tweet_region(self):
candidate_name = input("Enter the candidate name (Joe Biden/Donald Trump) : ")
......@@ -482,7 +409,7 @@ class Database:
state = state.strip()
region = input("Enter the county you wish to query by : ")
region = region.strip()
likes = input("Input the minimum amount of likes to query by")
likes = input("Input the minimum amount of likes to query by : ")
likes = likes.strip()
starting_date = input("Enter Start Date to filter by (YYYY-MM-DD): ")
......@@ -503,8 +430,10 @@ class Database:
print("No tweets in county {0} above {1} likes in this timeframe".format(region, likes))
return
print("Number of tweets in county {0} above {1} likes in timeframe is {2}".format(region, likes, results[0]))
for row in results:
print("Number of tweets in county {0} above {1} likes within timeframe is {2}".format(region, likes, row[0]))
# update_poll_count command, updates party vote tally in a specific county
def update_poll_count(self):
county = input("Enter the county that you wish to update the poll result for : ")
......@@ -544,6 +473,7 @@ class Database:
results = self.execute_query(query)
print(results)
# insert tweet command
def insert_tweet(self):
username = input("Enter the username of this tweet : ")
......@@ -562,7 +492,7 @@ class Database:
tweet_id = 1 + self.get_tweet_id() #tweet id is number of tweets in table + 1
query = "insert into tweet (id, user_id, created_at, tweet, likes) \n"
query+="values({0}, {1}, \"{2}\", \"{3}\", {4});".format(tweet_id, user_id , date, tweet, likes)
query += "values({0}, {1}, \"{2}\", \"{3}\", {4});".format(tweet_id, user_id , date, tweet, likes)
print(query)
results = self.execute_query(query)
......@@ -654,7 +584,7 @@ if __name__ == "__main__":
client.update_state_count()
elif command == "insert_tweet": # Untested
elif command == "insert_tweet": # Untested - Should work
client.insert_tweet()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment