Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.59 KB | None | 0 0
  1. import sqlite3
  2. import re
  3. import pandas as pd
  4. import seaborn as sns
  5. import matplotlib.pyplot as plt
  6. from email_validator import validate_email, EmailNotValidError
  7.  
  8. pd.options.display.float_format = '${:,.2f}'.format
  9.  
  10. SalesDataFull = pd.ExcelFile("SalesDataFull.xlsx")
  11.  
  12.  
  13. def EMCheck(self):
  14. Email = input("Email")
  15. if Email:
  16. try:
  17. v = validate_email(Email)
  18. self.dbConn()
  19. except EmailNotValidError as er:
  20. self.showMessage("Error", str(er))
  21.  
  22. def register():
  23. with conn:
  24. cur = conn.cursor()
  25. try:
  26. EmployeeID = int(input("Enter New Employee ID: "))
  27. while EmployeeID == "":
  28. EmployeeID = input("What's your Employee ID:")
  29.  
  30. FirstName = input("What's your first name:")
  31. while FirstName == "":
  32. FirstName = input("What's your first name:")
  33.  
  34. LastName = input("What's your last name: ")
  35. while LastName == "":
  36. LastName = input("What's your last name:")
  37.  
  38. Email = input("Email: ")
  39. EMCheck(Email)
  40.  
  41. Password = input("Enter a password: ")
  42. while Password == "":
  43. Password = input("Enter a password: ")
  44. Password = Password.lower()
  45.  
  46. cur.execute(
  47. 'insert into employee values(?,?,?,?,?)',
  48. (EmployeeID, FirstName, LastName, Email, Password))
  49. cur.execute(
  50. 'select * from employee where EmployeeId=?',
  51. (EmployeeID,))
  52.  
  53. results = cur.fetchall()
  54. print(results)
  55. except sqlite3.Error as e:
  56. print(e)
  57. print("User already exist")
  58.  
  59.  
  60. def login():
  61. with conn:
  62. cur = conn.cursor()
  63. try:
  64. loginTest = False # main condition to loop if email and password not met
  65. while not loginTest: # wrong email loopy
  66. userEmail = input("Email please: ")
  67. userEmail = userEmail.lower().replace(" ", "")
  68. userPassword = input("Password: ").strip()
  69. cur.execute(
  70. "SELECT COUNT (*) FROM Employee WHERE(Email= '" + userEmail.lower() + "' AND Password= '" + userPassword + "')")
  71. results = cur.fetchone() # return very first thing it finds that matches
  72. print(results[0]) # print first thing
  73. if results[0] == 1:
  74. print("Login successful")
  75. loginTest = True
  76. else:
  77. print("Login Unsuccessful")
  78. existingUser = input("Existing user?[yes/no]")
  79. if existingUser == "no":
  80. register()
  81. except:
  82. print("connection failed")
  83.  
  84. def most():
  85. OrdersOnlyData = SalesDataFull.parse("Orders")
  86.  
  87. df_year = OrdersOnlyData["Order Date"].dt.year
  88. OrdersOnlyData["Year"] = df_year
  89.  
  90. # month_profit_headers = OrdersOnlyData[['Year', 'Month', 'Quarter', 'Product Name', 'Profit']]
  91. # month_profit_sum = month_profit_headers.groupby(by=['Product Name', 'Month', 'Quarter' ]).sum().sort_values(
  92. # by='Profit', ascending=False).reset_index()
  93.  
  94. year = int(input("Choose year (2014-2017): "))
  95. while not (2014 <= year <= 2017):
  96. print("Invalid input. Enter between 2014 to 2017")
  97. year = int(input("Enter year (2014-2017): "))
  98.  
  99. choice = input("\nTo view product per month enter 1" + "To view product by quarter 2:")
  100.  
  101. if choice == "1":
  102. df_month = OrdersOnlyData["Order Date"].dt.month
  103. OrdersOnlyData["Month"] = df_month
  104. month_profit_headers = OrdersOnlyData[["Product Name", "Profit"]]
  105. month_profit_sum = month_profit_headers.groupby(["Product Name"]).sum().sort_values(
  106. by="Profit", ascending=False)
  107. month_profit_sum = month_profit_sum.reset_index()
  108. month = int(input("Enter month (1-12): "))
  109. while not (1 <= month <= 12):
  110. print("Invalid entry, please try again")
  111. month = int(input("Enter month (1-12): "))
  112. # year = month_profit_sum.loc[month_profit_sum["Year"] == year]
  113. print("The 10 most profitable products ")
  114. data1 = month_profit_sum.head(10).round(2)
  115. print(data1)
  116. barchart2 = sns.barplot(x="Month", y="Profit", data=month_profit_sum)
  117. barchart2.set_title("Sales by Profit")
  118. plt.show()
  119.  
  120. # sns.set_style("whitegrid")
  121. # ax1 = sns.barplot(x="Product Name", y="Profit", data=data1)
  122. # ax1.set_xticklabels(labels=month_profit_headers["Product Name"], rotation=90, size=10)
  123. # ax1.set_title("Sales by Month")
  124. # plt.show()
  125. # plt.figure(figsize=(6,6))
  126. # plt.pie(month_profit_sum.Month, labels=month_profit_sum.Profit, shadow=True, autopct='%1.1f%%')
  127. # plt.title("Profit per month")
  128. # plt.axis('equal')
  129. # plt.show()
  130.  
  131. main_menu = input("Exit, enter 1. Main menu, enter 2: ")
  132. if main_menu == "1":
  133. print("Bye!")
  134. exit()
  135. if main_menu == "2":
  136. main()
  137.  
  138. if choice == "2":
  139. df_quarter = OrdersOnlyData["Order Date"].dt.quarter
  140. OrdersOnlyData["Quarter"] = df_quarter
  141. month_profit_headers = OrdersOnlyData[['Year', 'Quarter', 'Product Name', 'Profit']]
  142. month_profit_sum = month_profit_headers.groupby(by=['Product Name', 'Quarter']).sum().sort_values(
  143. by='Profit', ascending=False).reset_index()
  144. quarter = int(input("Enter quarter (1, 2, 3, 4): "))
  145.  
  146. while not (1 <= quarter <= 4):
  147. print("Invalid entry, please try again")
  148. quarter = int(input("Enter quarter: (1, 2, 3, 4"))
  149. year = month_profit_sum.loc[month_profit_sum['Year'] == year]
  150. print(year.loc[month_profit_sum['Quarter'] == quarter].head(10).round(2))
  151.  
  152. if quarter == 1:
  153. print("The 10 most profitable products ")
  154. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].head(10).round(2))
  155.  
  156. elif quarter == 2:
  157. print("The 10 most profitable products ")
  158. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].head(10).round(2))
  159.  
  160. elif quarter == 3:
  161. print("The 10 most profitable products ")
  162. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].head(10).round(2))
  163.  
  164. elif quarter == 4:
  165. print("The 10 most profitable products ")
  166. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].head(10).round(2))
  167.  
  168. main_menu = input("Exit, enter 1. Main menu, enter 2: ")
  169. if main_menu == "1":
  170. print("Bye!")
  171. exit()
  172. if main_menu == "2":
  173. main()
  174. else:
  175. print("Invalid input only use integers 1 and 2")
  176.  
  177.  
  178. def least():
  179. OrdersOnlyData = SalesDataFull.parse("Orders")
  180.  
  181. df_month = OrdersOnlyData["Order Date"].dt.month
  182. OrdersOnlyData["Month"] = df_month
  183.  
  184. df_year = OrdersOnlyData["Order Date"].dt.year
  185. OrdersOnlyData["Year"] = df_year
  186.  
  187. df_quarter = OrdersOnlyData["Order Date"].dt.quarter
  188. OrdersOnlyData["Quarter"] = df_quarter
  189.  
  190. month_profit_headers = OrdersOnlyData[['Year', 'Month', 'Quarter', 'Product Name', 'Profit']]
  191. month_profit_sum = month_profit_headers.groupby(['Product Name', 'Year', 'Month', 'Quarter']).sum().sort_values(
  192. by='Profit', ascending=False).reset_index()
  193.  
  194. year = int(input("Enter year (2014-2017): "))
  195.  
  196. while not (2014 <= year <= 2017):
  197. print("Invalid year. Please enter between 2014 to 2017")
  198. year = int(input("Enter year(2014-2017): "))
  199.  
  200. choice = input("\nTo view product per month enter 1. To view product by quarter 2: ")
  201.  
  202. if choice == "1":
  203. month = int(input("Enter month (1-12): "))
  204. while not (1 <= month <= 12):
  205. print("Invalid Month")
  206. month = int(input("Enter month (1-12): "))
  207. select_year = month_profit_sum.loc[month_profit_sum['Year'] == year]
  208. print("The 10 least profitable products ")
  209. print(select_year.loc[month_profit_sum['Month'] == month].tail(10).round(2))
  210. main_menu = input("Exit, enter 1. Main menu, enter 2: ")
  211. if main_menu == "1":
  212. print("Bye!")
  213. exit()
  214. if main_menu == "2":
  215. main()
  216.  
  217. if choice == "2":
  218. quarter = int(input("Enter quarter (1, 2, 3, 4): "))
  219.  
  220. while not (1 <= quarter <= 4):
  221. print("Invalid entry, please try again")
  222. quarter = int(input("Enter quarter: (1, 2, 3, 4"))
  223. year = month_profit_sum.loc[month_profit_sum['Year'] == year]
  224. print(year.loc[month_profit_sum['Quarter'] == quarter].tail(10).round(2))
  225.  
  226. if quarter == 1:
  227. print("The 10 most profitable products ")
  228. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].tail(10).round(2))
  229.  
  230. elif quarter == "2":
  231. print("The 10 least profitable products ")
  232. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].tail(10).round(2))
  233.  
  234. elif quarter == "3":
  235. print("The 10 least profitable products ")
  236. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].tail(10).round(2))
  237.  
  238. elif quarter == "4":
  239. print("The 10 least profitable products ")
  240. print(df_quarter.loc[month_profit_sum['Quarter'] == df_quarter].tail(10).round(2))
  241.  
  242. main_menu = input("Exit, enter 1. Main menu, enter 2: ")
  243. if main_menu == "1":
  244. print("Bye!")
  245. exit()
  246. if main_menu == "2":
  247. main()
  248. else:
  249. print("Invalid input only use integers 1 and 2")
  250.  
  251. def main():
  252. print("\nWelcome!\nTo view 10 most profitable products, enter 1. To view 10 least profitable products, enter 2: ")
  253.  
  254. while True:
  255. try:
  256. userdecision = input("Enter a choice 1 or 2: ")
  257. if userdecision == "1":
  258. most()
  259.  
  260. elif userdecision == "2":
  261. least()
  262.  
  263. else:
  264. print("Invalid entry, enter 1 or 2")
  265. main()
  266. except ValueError:
  267. print("Please enter 1 or 2")
  268. exit
  269.  
  270. def state():
  271. OrdersOnlyData = SalesDataFull.parse("Orders")
  272. # line below is a variable, separates states by creating a line between each state's information
  273. lines = "=" * 25
  274.  
  275. df_year = OrdersOnlyData["Order Date"].dt.year
  276. OrdersOnlyData["Year"] = df_year
  277.  
  278. states = OrdersOnlyData.State.unique()
  279. # block below will present positive profit sorted by state, will display from 1-10 products, depending on how many
  280. # products are available
  281. state_decision = input("To view best products, enter 1. To view worse products, enter 2.\n")
  282. if state_decision == "1":
  283. for pos_state in states:
  284. state_select = OrdersOnlyData.loc[OrdersOnlyData["State"] == pos_state]
  285. state_prof = state_select[["Year", "State", "Product Name", "Profit"]]
  286. best_state = state_prof.groupby("Product Name").sum().sort_values(by="Profit",
  287. ascending=False).reset_index()
  288. print("\nBest Product(s) in {} ".format(pos_state))
  289. print(lines)
  290. print(best_state.head(5).round(2))
  291. # block below will present negative profit sorted by state, will display 1-10 products, depending on how many
  292. # products are available
  293. elif state_decision == "2":
  294. for neg_state in states:
  295. state_select = OrdersOnlyData.loc[OrdersOnlyData["State"] == neg_state]
  296. state_prof = state_select[["Year", "State", "Product Name", "Profit"]]
  297. worse_state = state_prof.groupby("Product Name").sum().sort_values(by="Profit").reset_index()
  298. print("\nBest Product(s) in {} ".format(neg_state))
  299. print(lines)
  300. print(worse_state.tail(5).round(2))
  301.  
  302. # block below gives user the option to either exit program or return to main menu
  303. else:
  304. print("Invalid input, enter 1 or 2: ")
  305. menu = input("\nMain menu, enter 1. Exit, enter 2.\n ")
  306. if menu == "1":
  307. main()
  308. if menu == "2":
  309. print("\nBye!")
  310. exit()
  311.  
  312.  
  313. # block will display profitable/negative products sorted by city
  314. def city_prod():
  315. OrdersOnlyData = SalesDataFull.parse("Orders")
  316. # line below is a variable, separates states by creating a line between each state's information
  317. lines = "=" * 25
  318. df_year = OrdersOnlyData["Order Date"].dt.year
  319. OrdersOnlyData["Year"] = df_year
  320.  
  321. cities = OrdersOnlyData.City.unique()
  322.  
  323. # block below will present positive profit sorted by city, will display from 1-10 products, depending on how many
  324. # products are available
  325. city_decision = input("To view best products, enter 1. To view worse products, enter 2.\n")
  326. if city_decision == "1":
  327. for city in cities:
  328. cit_select = OrdersOnlyData.loc[OrdersOnlyData["City"] == city]
  329. cit_prof = cit_select[["Year", "City", "Product Name", "Profit"]]
  330. best_city = cit_prof.groupby("Product Name").sum().sort_values(by="Profit", ascending=False).reset_index()
  331. print("\nBest Product(s) in {} ".format(city))
  332. print(lines)
  333. print(best_city.head(5).round(2))
  334.  
  335. if city_decision == "2":
  336. for city in cities:
  337. cit_select = OrdersOnlyData.loc[OrdersOnlyData["City"] == city]
  338. cit_neg = cit_select[["Year", "City", "Product Name", "Profit"]]
  339. nega_city = cit_neg.groupby("Product Name").sum().sort_values(by="Profit").reset_index()
  340. print("\nBest Product(s) in {} ".format(city))
  341. print(lines)
  342. print(nega_city.tail(5).round(2))
  343.  
  344. # block below gives user the option to either exit program or return to main menu
  345. else:
  346. print("Invalid input, enter 1 or 2: ")
  347. menu = input("\nMain menu, enter 1. Exit, enter 2.\n ")
  348. if menu == "1":
  349. main()
  350. if menu == "2":
  351. print("\nBye!")
  352. exit()
  353.  
  354.  
  355. # main menu, prompts user to choose between sorting products by state or city
  356. def main2():
  357. print("\nWelcome!\nSort product by state, enter 1. Sort product by city, enter 2.")
  358.  
  359. while True:
  360. try:
  361. option = input("Enter option (1 or 2): ")
  362.  
  363. if option == "1":
  364. state()
  365. elif option == "2":
  366. city_prod()
  367. else:
  368. print("Invalid input, enter 1 or 2: ")
  369.  
  370. # excepts value error if its an invalid input (e.g. a letter, or number that is not 1 or 2
  371. except ValueError:
  372. print("Please enter 1 or 2")
  373. exit
  374.  
  375.  
  376. conn = sqlite3.connect('OS_employee.db')
  377. with conn:
  378. cur = conn.cursor()
  379. print("successfully connected")
  380.  
  381. existingUser = input("Existing user?[yes/no]")
  382. existingUser = existingUser.lower()
  383. while existingUser != "yes" or existingUser != "no":
  384. if existingUser == "no":
  385. register()
  386. proceed = input("proceed to log in?")
  387. if proceed == "yes":
  388. login()
  389. print("successfully log in")
  390. main()
  391. main2()
  392. break
  393. elif existingUser == "yes":
  394. login()
  395. print("successfully log in")
  396. main()
  397. main2()
  398. break
  399. else:
  400. input("Invalid input. Please answer [yes/no]")
  401. existingUser = input("Existing user?[yes/no]")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement