generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
150 lines (122 loc) · 4.52 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import gspread
from google.oauth2.service_account import Credentials
import colorama as colorama
SCOPE = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
CREDS = Credentials.from_service_account_file('creds.json')
SCOPED_CREDS = CREDS.with_scopes(SCOPE)
GSPREAD_CLIENT = gspread.authorize(SCOPED_CREDS)
SHEET = GSPREAD_CLIENT.open('love_sandwiches')
def get_sales_data():
"""
Get sales figures input from the user.
Run a while loop to collect a valid string of data from the user
via the terminal, which must be a string of 6 numbers separated
by commas. The loop will repeatedly request data, until it is valid.
"""
while True:
print("{}Please enter sales data from the last market.".format(
colorama.Fore.LIGHTBLUE_EX))
print("{}Data should be six numbers, separated by commas.".format(
colorama.Fore.YELLOW))
print("Example: 10,20,30,40,50,60\n")
print("{}Enter in order of sandwich type... ".format(
colorama.Fore.BLUE))
print("{}avoc, tom&moz, hum, mushrm, lentil hum, salad".format(
colorama.Fore.LIGHTYELLOW_EX))
data_str = input("{}Enter your data here:\n".format(
colorama.Fore.LIGHTWHITE_EX))
sales_data = data_str.split(",")
if validate_data(sales_data):
print("{}Data is valid!".format(colorama.Fore.LIGHTWHITE_EX))
break
return sales_data
def validate_data(values):
"""
Inside the try, converts all string values into integers.
Raises ValueError if strings cannot be converted into int,
or if there aren't exactly 6 values.
"""
try:
[int(value) for value in values]
if len(values) != 6:
raise ValueError(
f"Exactly 6 values required, you provided {len(values)}"
)
except ValueError as e:
print(f"Invalid data: {e}, please try again.\n")
return False
return True
def update_worksheet(data, worksheet):
"""
Receives a list of integers to be inserted into a worksheet
Update the relevant worksheet with the data provided
"""
print(f"Updating {worksheet} worksheet...\n")
worksheet_to_update = SHEET.worksheet(worksheet)
worksheet_to_update.append_row(data)
print(f"{worksheet} worksheet updated successfully\n")
def calculate_surplus_data(sales_row):
"""
Compare sales with stock and calculate the surplus for each item type.
The surplus is defined as the sales figure subtracted from the stock:
- Positive surplus indicates waste
- Negative surplus indicates extra made when stock was sold out.
"""
print("Calculating surplus data...\n")
stock = SHEET.worksheet("stock").get_all_values()
stock_row = stock[-1]
surplus_data = []
for stock, sales in zip(stock_row, sales_row):
surplus = int(stock) - sales
surplus_data.append(surplus)
return surplus_data
def get_last_5_entries_sales():
"""
Collects columns of data from sales worksheet, collecting
the last 5 entries for each sandwich and returns the data
as a list of lists.
"""
sales = SHEET.worksheet("sales")
columns = []
for ind in range(1, 7):
column = sales.col_values(ind)
columns.append(column[-5:])
return columns
def calculate_stock_data(data):
"""
Calculate the average stock for each item type, adding 10%
"""
print("Calculating stock data...\n")
new_stock_data = []
for column in data:
int_column = [int(num) for num in column]
average = sum(int_column) / len(int_column)
stock_num = average * 1.1
new_stock_data.append(round(stock_num))
return new_stock_data
def get_stock_values(data):
headings = SHEET.worksheet("stock").row_values(1)
stock_dict = dict(zip(headings, data))
print("{}MAKE THE FOLLOWING FOR THE NEXT MARKET: \n".format(
colorama.Fore.CYAN))
print(stock_dict)
return stock_dict
def main():
"""
Run all program functions
"""
data = get_sales_data()
sales_data = [int(num) for num in data]
update_worksheet(sales_data, "sales")
new_surplus_data = calculate_surplus_data(sales_data)
update_worksheet(new_surplus_data, "surplus")
sales_columns = get_last_5_entries_sales()
stock_data = calculate_stock_data(sales_columns)
update_worksheet(stock_data, "stock")
get_stock_values(data)
print("Welcome to Vegan Sandwiches data automation")
main()