generated from Code-Institute-Org/p3-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
175 lines (120 loc) · 4.31 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import gspread
from google.oauth2.service_account import Credentials
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
"""
while True:
print("Please enter sales data from the last market.")
print("Data should be six numbers, separated by commas.")
print("Example: 10,20,30,40,50,60\n")
data_str = input("Enter your data here:\n")
sales_data = data_str.split(",")
if validate_data(sales_data):
print("Data is valid!")
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.
"""
print(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_sales_worksheet(data):
"""
Update sales worksheet, add new row with the list data provided.
"""
print("Updating sales worksheet...\n")
sales_worksheet = SHEET.worksheet("sales")
sales_worksheet.append_row(data)
print("Sales worksheet updated successfully.\n")
def update_surplus_worksheet(data):
"""
Update surplus worksheet, add new row with the list data provided.
"""
print("Updating surplus worksheet...\n")
surplus_worksheet = SHEET.worksheet("surplus")
surplus_worksheet.append_row(data)
print("Surplus worksheet updated successfully.\n")
def update_worksheet(data, worksheet):
"""
Recieves 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 surplus for each item type.
The surplus is defined as the sales figure substracted 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 collumns 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 avarage 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 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")
print("Welcome to Love Sandwiches Data Automation")
main()