generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
120 lines (98 loc) · 3.99 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
import gspread
from google.oauth2.service_account import Credentials
from pprint import pprint
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 valid data from the user
via the terminal, which must be a string of 6 numbers separated
by commas. The loop will request data until valid."""
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: ")
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 strings to integers.
Raises ValueError if strings can't be converted to integers,
or if there aren't 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):
"""Update sales and surplus worksheet, add new rows with the list 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!")
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 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)
dict = {}
for head, val in zip(headings, data):
dict.update({head: val})
return dict
def main():
"""Run all program functions"""
data = get_sales_data()
sales_data = [int(num) for num in data]
new_surplus_data = calculate_surplus_data(sales_data)
update_worksheet(sales_data, 'sales')
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')
return stock_data
print("Welcome to Love Sandwiches Data Automation")
stock_data = main()
stock_values = get_stock_values(stock_data)
print("Make the following numbers of sandwiches for next market:\n")
print(stock_values)