Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Wed Dec 5 22:21:36 2018
- @author: jeffreyleong
- """
- # -*- coding: utf-8 -*-
- """
- Created on Wed Nov 7 12:34:11 2018
- @author: alvin
- """
- import sqlite3
- import getpass
- import time
- import pandas as pd
- import scipy.stats
- import dns.resolver
- import seaborn as sns
- import matplotlib.pyplot as plt
- xl = pd.ExcelFile("SalesDataFull.xlsx")
- OrdersOnlyData = xl.parse("Orders")
- CopyOrdersData = OrdersOnlyData.copy()
- lines = "="*60
- connection = sqlite3.connect('OS_Employee.db',timeout=5)
- cur= connection.cursor()
- connection2= sqlite3.connect('Orders.db',timeout=5)
- c2= connection2.cursor()
- def login():
- print("Login")
- print()
- print("Enter 1 to Login")
- print("Enter 2 to Create an Account")
- print("Enter 3 to Exit")
- def get_login():
- accepted = False
- while not accepted:
- choice = int(input("Enter 1, 2, or 3: "))
- if 1 <= choice <= 5:
- accepted = True
- else:
- print()
- print("Please enter a valid value:")
- return choice
- def main_menu():
- time.sleep(1)
- print("")
- print("Main Menu")
- print()
- print("1. Top 10 Most Profitable products by Year + Quarters")
- print("2. Top 10 Least Profitable by Year + Quarters")
- print("3. Log out")
- print("4. Top 10 Most profitable states and region")
- print("5. Top 10 Least Profitable States and region")
- print("6. See Plot for Associate between Discounts and Quantities Sold in South")
- print("7. See Plot for Associate between Discounts and Quantities Sold in Central")
- print("8. See Plot for Associate between Discounts and Quantities Sold in East")
- print("9. See Plot for Associate between Discounts and Quantities Sold in West")
- def get_main_menu():
- accepted = False
- while not accepted:
- choice = int(input("Please select an option: "))
- if 0 <= choice <= 10:
- accepted = True
- else:
- print()
- print("Please enter a valid value: (1)")
- return choice
- '''def plot(region, num):
- word = ""
- if num == 6:
- word = "Central"
- elif num == 2:
- word = "South"
- elif num == 3:
- word = "West"
- elif num == 4:
- word = "East"
- RegionalSales = CopyOrdersData.loc[CopyOrdersData['Region'] == word, :]
- ActiveDataFrame = RegionalSales
- x = "Discount"
- y = "Quantity"
- getCorr = scipy.stats.spearmanr(ActiveDataFrame[x], ActiveDataFrame[y])
- correlation = str(getCorr[0])
- pValue = str(getCorr[1])
- print("The correlation between " + x + " and " + y + " in the " + word + " Region is: " + correlation)
- print("With p value of: " + pValue)
- sns.lmplot(x=x, y=y, data=ActiveDataFrame)
- plt.xlabel(x)
- plt.ylabel(y)
- plt.title("Correlation of " + x + " and " + y + " in region: " + word)
- plt.show()
- '''
- def validate_data(year, num):
- sort = True
- word = ""
- if num == 1:
- sort = False
- word = "Most"
- else:
- sort = True
- word = "Least"
- prod_prof = OrdersOnlyData[["Product Name", "Order Date", "Profit"]]
- prod_select = prod_prof.loc[prod_prof["Order Date"].dt.year == year]
- months = (pd.DatetimeIndex(prod_select["Order Date"]).month.unique()).sort_values()
- print("Year: " + str(year) + ".")
- for month in months:
- months_list = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
- prod_select_month = prod_select.loc[prod_select["Order Date"].dt.month == month]
- best_select = prod_select_month.groupby('Product Name').sum().sort_values(by = "Profit", ascending = sort)
- print("\n" + word + " profitable products in " + months_list[month-1] + "\n")
- print(best_select.head(10))
- def validate_quarter(year, num):
- sort = True
- word = ""
- if num == 1:
- sort = False
- word = "Most"
- else:
- sort = True
- word = "Least"
- prod_prof = OrdersOnlyData[["Product Name", "Order Date", "Profit"]]
- prod_select = prod_prof.loc[prod_prof["Order Date"].dt.year == year]
- quarters = (pd.DatetimeIndex(prod_select["Order Date"]).quarter.unique()).sort_values()
- print("Year: " + str(year) + ".")
- for quarter in quarters:
- quarters_list = ["1st Quarter", "2nd Quarter", "3rd Quarter", "4th Quarter"]
- prod_select_quarter = prod_select.loc[prod_select["Order Date"].dt.quarter == quarter]
- best_select = prod_select_quarter.groupby('Product Name').sum().sort_values(by = "Profit", ascending = sort)
- print("\n" + word + " profitable products in " + quarters_list[quarter-1] + "\n")
- print(best_select.head(10))
- #after year choose whether they want a specific month or every month, or by quarter = 3/12 Jan,Feb,Mar
- '''def product_by_quarter(year, num):
- lines = "="*25
- sort = True
- word = ""
- if num == 1:
- sort = False
- word = "Most"
- else:
- sort = True
- word = "Least"
- prod_prof = OrdersOnlyData[["Product Name", "Order Date", "Profit"]]
- prod_select = prod_prof.loc[prod_prof["Order Date"].dt.year == year]
- quarters = (pd.DatetimeIndex(prod_select["Order Date"]).quarter.unique()).sort_values()
- print("\n" + lines + lines + "\n")
- print("Year: " + str(year) + ".")
- for quarter in quarters:
- quarters_list = ["Quarter I", "Quarter II", "Quarter III", "Quarter IV"]
- prod_select_quarter = prod_select.loc[prod_select["Order Date"].dt.quarter == quarter]
- best_select = prod_select_quarter.groupby('Product Name').sum().sort_values(by = "Profit", ascending = sort)
- print("\n" + word + " profitable products in " + quarters_list[quarter-1] + "\n")
- print("\n" + lines + lines + "\n")
- print(best_select.head(10))
- '''
- def main():
- finished = False
- while not finished:
- login()
- choice = get_login()
- if choice == 1:
- with sqlite3.connect('OS_Employee.db'):
- email = input('Please enter your email address: ')
- password = getpass.getpass('Please enter your password: ')
- try:
- cur.execute("SELECT COUNT (*) FROM Employee WHERE (Email = '" + email +"' AND Password = '" + password + "')")
- connection.commit()
- result= cur.fetchone()
- if result[0]==1:
- print('')
- print('Processing...')
- time.sleep(1.5)
- print("...")
- print("Login successful.")
- time.sleep(1)
- print("Welcome to Office Solutions!")
- connection.close()
- finished = False
- while not finished:
- main_menu()
- choice = get_main_menu()
- if choice == 1:
- num = 1
- while True:
- print("\nYears available: 2014, 2015, 2016, 2017")
- choice = input("Please choose a year or press (0) to go back to previous menu: ")
- if choice == "2014":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2014, num)
- validate_quarter(2014, num)
- elif choice == "2015":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2015, num)
- validate_quarter(2015, num)
- elif choice == "2016":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2016, num)
- validate_quarter(2016, num)
- elif choice == "2017":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2017, num)
- validate_quarter(2017, num)
- elif choice == "0":
- break
- else:
- print("Year not found")
- elif choice == 2:
- num = 2
- while True:
- print("\nYears available: 2014, 2015, 2016, 2017")
- choice = input("Please choose a year or press (0) to go back to previous menu: ")
- print("------")
- if choice == "2014":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2014, num)
- validate_quarter(2014, num)
- elif choice == "2015":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2015, num)
- validate_quarter(2015, num)
- elif choice == "2016":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2016, num)
- validate_quarter(2016, num)
- elif choice == "2017":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- validate_data(2017, num)
- validate_quarter(2017, num)
- elif choice == "0":
- break
- else:
- print("Year not found")
- elif choice ==4:
- state_prof_cols=OrdersOnlyData[["State", "Profit"]]
- state_purchases=state_prof_cols.groupby(by="State").sum().sort_values(by="Profit", ascending=False)
- print("\nTop 10 states with the highest profit.")
- print(state_purchases.head(10))
- print(lines)
- reg_prof_cols=OrdersOnlyData[["Region", "Profit"]]
- region_profits=reg_prof_cols.groupby(by="Region").sum().sort_values(by="Profit", ascending = False)
- print("\nRegion with the highest profit.")
- print(region_profits.head(4))
- print(lines)
- elif choice ==5:
- state_prof_cols=OrdersOnlyData[["State", "Profit"]]
- state_purchases=state_prof_cols.groupby(by="State").sum().sort_values(by="Profit", ascending = True)
- print("\nTop 10 states with most Revenue Loss")
- print(state_purchases.head(10))
- reg_prof_cols=OrdersOnlyData[["Region", "Profit"]]
- region_profits=reg_prof_cols.groupby(by="Region").sum().sort_values(by="Profit", ascending = True)
- print("\nRegion with the lowest profit.")
- print(region_profits.tail(4))
- print(lines)
- elif choice == 6:
- Reg = "South"
- RegionalSales = CopyOrdersData.loc[CopyOrdersData['Region'] == Reg, :]
- ActiveDataFrame = RegionalSales
- x = "Discount"
- y = "Quantity"
- getCorr = scipy.stats.spearmanr(ActiveDataFrame[x], ActiveDataFrame[y])
- correlation = str(getCorr[0])
- pValue = str(getCorr[1])
- print("The correlation between " + x + " and " + y + " in the selected Region is: " + correlation)
- print("With p value of: " + pValue)
- sns.lmplot(x=x, y=y, data=ActiveDataFrame)
- plt.xlabel(x)
- plt.ylabel(y)
- plt.title("Correlation of " + x + " and " + y + " in region: " + Reg)
- plt.show()
- elif choice == 7:
- Reg = "Central"
- RegionalSales = CopyOrdersData.loc[CopyOrdersData['Region'] == Reg, :]
- ActiveDataFrame = RegionalSales
- x = "Discount"
- y = "Quantity"
- getCorr = scipy.stats.spearmanr(ActiveDataFrame[x], ActiveDataFrame[y])
- correlation = str(getCorr[0])
- pValue = str(getCorr[1])
- print("The correlation between " + x + " and " + y + " in the selected Region is: " + correlation)
- print("With p value of: " + pValue)
- sns.lmplot(x=x, y=y, data=ActiveDataFrame)
- plt.xlabel(x)
- plt.ylabel(y)
- plt.title("Correlation of " + x + " and " + y + " in region: " + Reg)
- plt.show()
- elif choice == 8:
- Reg = "East"
- RegionalSales = CopyOrdersData.loc[CopyOrdersData['Region'] == Reg, :]
- ActiveDataFrame = RegionalSales
- x = "Discount"
- y = "Quantity"
- getCorr = scipy.stats.spearmanr(ActiveDataFrame[x], ActiveDataFrame[y])
- correlation = str(getCorr[0])
- pValue = str(getCorr[1])
- print("The correlation between " + x + " and " + y + " in the selected Region is: " + correlation)
- print("With p value of: " + pValue)
- sns.lmplot(x=x, y=y, data=ActiveDataFrame)
- plt.xlabel(x)
- plt.ylabel(y)
- plt.title("Correlation of " + x + " and " + y + " in region: " + Reg)
- plt.show()
- elif choice == 9:
- Reg = "West"
- RegionalSales = CopyOrdersData.loc[CopyOrdersData['Region'] == Reg, :]
- ActiveDataFrame = RegionalSales
- x = "Discount"
- y = "Quantity"
- getCorr = scipy.stats.spearmanr(ActiveDataFrame[x], ActiveDataFrame[y])
- correlation = str(getCorr[0])
- pValue = str(getCorr[1])
- print("The correlation between " + x + " and " + y + " in the selected Region is: " + correlation)
- print("With p value of: " + pValue)
- sns.lmplot(x=x, y=y, data=ActiveDataFrame)
- plt.xlabel(x)
- plt.ylabel(y)
- plt.title("Correlation of " + x + " and " + y + " in region: " + Reg)
- plt.show()
- '''num == 6
- while True:
- print("\nChoose Region: 1. Central, 2. South, 3. West, 4. East: ")
- choice = input("Please choose a region or press (0) to go back to previous menu: ")
- print("------")
- if choice == "1":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- plot(1, num)
- elif choice == "2":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- plot(2, num)
- elif choice == "3":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- plot(3, num)
- elif choice == "4":
- print("...")
- time.sleep(1)
- print("Processing...")
- time.sleep(1)
- plot(4, num)
- '''
- elif choice == 0:
- print('')
- time.sleep(1)
- print("Thank you!")
- finished = True
- else:
- finished = True
- else:
- print()
- print('Processing...')
- time.sleep(1.5)
- print()
- print("Login failed")
- except:
- print("Connection failed")
- #user creates an account here
- elif choice == 2:
- with sqlite3.connect('OS_Employee.db') as db:
- employee_ID =input("Please enter your Employee ID: ")
- while employee_ID.isnumeric() == False:
- employee_ID = input("Sorry, try again\nPlease enter your Employee ID:")
- first_name = input("Please enter your first name: ")
- while first_name.isnumeric() == True:
- first_name = input("Sorry, try again\nPlease enter your first name: ")
- last_name = input("Please enter your last name: ")
- while last_name.isnumeric() == True:
- last_name = input("Sorry, try again\nPlease enter your last name: ")
- while True:
- email = input('Please enter your working email: ')
- dom = email.find("@")
- if (dom == -1):
- print("Symbol @ is not found")
- exit()
- if (dom == 0):
- print("First substring is empty")
- exit()
- if (email[-1] == '@'):
- print("Second substring is empty")
- exit()
- try:
- dns.resolver.query(email[dom+1:], 'MX')
- except dns.resolver.MXDOMAIN:
- email= input("invalid email, please enter another email")
- exit()
- cur.execute("SELECT COUNT (*) FROM Employee WHERE (Email ='" +email+ "')")
- emailresult= cur.fetchone()
- if emailresult[0]!=1:
- print("Email is available")
- break
- else:
- print("Email is taken. Please enter another email")
- email=''
- continue
- password = input("Create a password for your log-in: ")
- while len(password) > 24 or len(password)<3:
- password= input("Sorry, try again\nPlease enter a password for your log-in: ")
- user_account =(employee_ID, first_name.title(), last_name.title(), email, password)
- try:
- cur.execute('INSERT INTO Employee (EmployeeID,FirstName,LastName,Email,Password) VALUES (?,?,?,?,?)', user_account)
- db.commit()
- print("Processing...")
- time.sleep(1.5)
- print("Registration successful! Please log in.")
- except:
- print("Connection failed.")
- elif choice == 0:
- print('')
- time.sleep(1)
- print("Thank you!")
- finished = True
- else:
- finished = True
- if __name__ == "__main__":
- DATABASE = 'OS_Employee.db'
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement