Added function to import any Doordash CSV to create graphs
This commit is contained in:
138
main.py
138
main.py
@@ -1,4 +1,5 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
import csv
|
||||||
import sys
|
import sys
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
@@ -6,6 +7,7 @@ import numpy as np
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
def weakness_by_hours(hours):
|
def weakness_by_hours(hours):
|
||||||
keys = list(hours.keys())
|
keys = list(hours.keys())
|
||||||
keys.sort()
|
keys.sort()
|
||||||
@@ -13,9 +15,8 @@ def weakness_by_hours(hours):
|
|||||||
for key in keys:
|
for key in keys:
|
||||||
print(key, len(hours.get(key)))
|
print(key, len(hours.get(key)))
|
||||||
|
|
||||||
|
|
||||||
fig, ax = plt.subplots()
|
fig, ax = plt.subplots()
|
||||||
ax.tick_params(axis='x', labelrotation=45)
|
ax.tick_params(axis="x", labelrotation=45)
|
||||||
hour_list = keys
|
hour_list = keys
|
||||||
counts = [len(hours.get(key)) for key in hours]
|
counts = [len(hours.get(key)) for key in hours]
|
||||||
ax.bar(hour_list, counts)
|
ax.bar(hour_list, counts)
|
||||||
@@ -23,6 +24,7 @@ def weakness_by_hours(hours):
|
|||||||
ax.set_title("akshay's moments of weakness")
|
ax.set_title("akshay's moments of weakness")
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
class Weakness:
|
class Weakness:
|
||||||
def __init__(self, row):
|
def __init__(self, row):
|
||||||
self.name = row[0]
|
self.name = row[0]
|
||||||
@@ -32,6 +34,7 @@ class Weakness:
|
|||||||
self.time = datetime.today()
|
self.time = datetime.today()
|
||||||
self.total = row[2]
|
self.total = row[2]
|
||||||
|
|
||||||
|
|
||||||
def weaknessInAnHour(results):
|
def weaknessInAnHour(results):
|
||||||
weaknesses = [Weakness(row) for row in results]
|
weaknesses = [Weakness(row) for row in results]
|
||||||
restaurants = defaultdict(lambda: 0)
|
restaurants = defaultdict(lambda: 0)
|
||||||
@@ -40,13 +43,12 @@ def weaknessInAnHour(results):
|
|||||||
if weakness.time.hour == 0:
|
if weakness.time.hour == 0:
|
||||||
restaurants[weakness.name] += 1
|
restaurants[weakness.name] += 1
|
||||||
|
|
||||||
print(restaurants)
|
|
||||||
|
|
||||||
fig, ax = plt.subplots()
|
fig, ax = plt.subplots()
|
||||||
plt.xticks(rotation=45, ha='right')
|
plt.xticks(rotation=45, ha="right")
|
||||||
ax.bar(restaurants.keys(), restaurants.values())
|
ax.bar(restaurants.keys(), restaurants.values())
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
def weaknessPerMonth(results):
|
def weaknessPerMonth(results):
|
||||||
weaknesses = [Weakness(row) for row in results]
|
weaknesses = [Weakness(row) for row in results]
|
||||||
months = defaultdict(lambda: 0)
|
months = defaultdict(lambda: 0)
|
||||||
@@ -60,12 +62,11 @@ def weaknessPerMonth(results):
|
|||||||
ax.bar(sorted_keys, sorted_amounts)
|
ax.bar(sorted_keys, sorted_amounts)
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
def weaknessPerDayOverYear(results):
|
def weaknessPerDayOverYear(results):
|
||||||
weaknesses = [Weakness(row) for row in results]
|
weaknesses = [Weakness(row) for row in results]
|
||||||
months = defaultdict(lambda: 0)
|
months = defaultdict(lambda: 0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
week_array = [[0] * 7 for x in range(53)]
|
week_array = [[0] * 7 for x in range(53)]
|
||||||
|
|
||||||
for weakness in weaknesses:
|
for weakness in weaknesses:
|
||||||
@@ -77,53 +78,118 @@ def weaknessPerDayOverYear(results):
|
|||||||
fig, ax = plt.subplots()
|
fig, ax = plt.subplots()
|
||||||
im = ax.imshow(week_array, cmap="Reds")
|
im = ax.imshow(week_array, cmap="Reds")
|
||||||
|
|
||||||
ax.set_yticks(np.arange(7), labels=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"])
|
ax.set_yticks(
|
||||||
ax.set_xticks(np.arange(53), labels=[
|
np.arange(7),
|
||||||
"", "Jan", "", "", "",
|
labels=[
|
||||||
"Feb", "", "", "",
|
"Sunday",
|
||||||
"", "Mar", "", "", "",
|
"Monday",
|
||||||
"Apr", "", "", "",
|
"Tuesday",
|
||||||
"", "May", "", "", "",
|
"Wednesday",
|
||||||
"Jun", "", "", "",
|
"Thursday",
|
||||||
"Jul", "", "", "",
|
"Friday",
|
||||||
"Aug", "", "", "",
|
"Saturday",
|
||||||
"", "Sep", "", "", "",
|
],
|
||||||
"Oct", "", "", "",
|
)
|
||||||
"", "Nov", "", "", "",
|
ax.set_xticks(
|
||||||
"Dec", "", "", "",
|
np.arange(53),
|
||||||
])
|
labels=[
|
||||||
|
"",
|
||||||
|
"Jan",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Feb",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Mar",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Apr",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"May",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Jun",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Jul",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Aug",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Sep",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Oct",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Nov",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"Dec",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
],
|
||||||
|
)
|
||||||
# [ f"Week {x+1}" for x in range(53)])
|
# [ f"Week {x+1}" for x in range(53)])
|
||||||
|
|
||||||
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
|
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
|
||||||
rotation_mode="anchor")
|
|
||||||
for i in range(53):
|
for i in range(53):
|
||||||
for j in range(7):
|
for j in range(7):
|
||||||
pass
|
pass
|
||||||
# text = ax.text(i, j, week_array[j, i],
|
# text = ax.text(i, j, week_array[j, i],
|
||||||
# ha="center", va="center", color="0", fontsize=12)
|
# ha="center", va="center", color="0", fontsize=12)
|
||||||
|
|
||||||
|
|
||||||
ax.set_title("akshay weakness in 2022")
|
ax.set_title("akshay weakness in 2022")
|
||||||
fig.tight_layout()
|
fig.tight_layout()
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
def validateRow(row):
|
||||||
|
if len(row) != 9:
|
||||||
|
raise Exception("Should have 9 columns in Doordash data export")
|
||||||
|
|
||||||
|
float(row[3])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def importCSV(filename: str):
|
def importCSV(filename: str):
|
||||||
|
CREATE_TABLE_COMMAND = """CREATE TABLE IF NOT EXISTS "doordash"(
|
||||||
|
"ITEM" TEXT, "CATEGORY" TEXT, "STORE_NAME" TEXT, "UNIT_PRICE" TEXT,
|
||||||
|
"QUANTITY" TEXT, "SUBTOTAL" TEXT, "CREATED_AT" TEXT, "DELIVERY_TIME" TEXT,
|
||||||
|
"DELIVERY_ADDRESS" TEXT);"""
|
||||||
|
|
||||||
|
csvReader = csv.reader(open("yeet.csv"), delimiter=",", quotechar='"')
|
||||||
|
|
||||||
filepart = filename.split(".")[0]
|
filepart = filename.split(".")[0]
|
||||||
conn = sqlite3.connect(f"{filepart}.db")
|
conn = sqlite3.connect(f"{filepart}.db")
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
c.execute(".mode csv")
|
c.execute(CREATE_TABLE_COMMAND)
|
||||||
c.execute(f".import {filename} doordash")
|
|
||||||
|
# Skip first row.
|
||||||
|
next(csvReader)
|
||||||
|
|
||||||
|
for row in csvReader:
|
||||||
|
validateRow(row)
|
||||||
|
c.execute("insert into doordash values (?, ?, ?, ?, ?, ?, ?, ?, ?)", row)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
with sqlite3.connect("doordash.db") as connection:
|
|
||||||
c = connection.cursor()
|
|
||||||
results = c.execute("select STORE_NAME, DELIVERY_TIME, sum(cast(SUBTOTAL as decimal)) from doordash group by DELIVERY_TIME, STORE_NAME;")
|
|
||||||
|
|
||||||
weaknessPerDayOverYear(results)
|
|
||||||
"""
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
raise Exception("input csv missing")
|
raise Exception("input csv missing")
|
||||||
|
|||||||
Reference in New Issue
Block a user