generated from Code-Institute-Org/python-essentials-template-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
122 lines (97 loc) · 3.46 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
# python code goes here
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 figure 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:")
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.
"""
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):
print(f"Updating {worksheet} worksheet...\n")
worksheet_to_update = SHEET.worksheet(worksheet)
worksheet_to_update.append_row(data)
print(f"{worksheet} update succesfully\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]
print("Calculating surplus...")
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():
"""
Collect last 5 days of sales from worksheet
"""
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 kind of sandwiches 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():
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_colummns = get_last_5_entries_sales()
stock_data = calculate_stock_data(sales_colummns)
update_worksheet(stock_data, "stock")
print("Welcome to Love Sandwiches Data Automation\n")
main()