-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataminer.py
140 lines (105 loc) · 4.39 KB
/
dataminer.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
# pylint: disable=no-member
"""Ultimately prepares data.json with a few utility classes"""
import json
from functools import reduce
from pandas import read_csv
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
class ProductProperty:
"""Enum values."""
COMPOSITION = "composition"
COST = "cost"
DATE = "date"
DEMAND = "demand"
INGREDIENTS_wORTH = "ingredientsWorth"
IS_WORTH_IT = "isWorthIt"
MERCHANDISABLE = "merchandisable"
NAME = "name"
PROFIT = "profit"
PRICE = "price"
TOTAL_COST = "totalCost"
TOTAL_PROFIT = "totalProfit"
PRICE_ADJUSTMENT = "priceAdjustment"
products = []
max_ingredients = 4
class Products:
"""A utility class to prepare data.json from data.csv file."""
@staticmethod
def prepare_products():
"""Prepares products from a csv file."""
global products
global max_ingredients
converters = {
"cost": int,
"date": int,
"ingredient_1": str,
"ingredient_2": str,
"ingredient_3": str,
"ingredient_4": str,
"price": int,
"manufacturedAt": str,
"soldAt": str
}
products = read_csv("src/data/products.csv", delimiter='\t',
converters=converters).to_dict(orient="records")
for product in products:
product[ProductProperty.COMPOSITION] = list()
for i in range(1, max_ingredients + 1):
ingredient = "ingredient_" + str(i)
if product[ingredient] is not "":
product[ProductProperty.COMPOSITION].append(
product[ingredient])
product.pop(ingredient, None)
# Ingredients have been consolidated into an array so we can calculate
# price points
for index, product in enumerate(products):
product["index"] = index
product[ProductProperty.DEMAND] = 1
product[ProductProperty.PRICE_ADJUSTMENT] = 100
product[ProductProperty.TOTAL_COST] = Products.total_cost(
product[ProductProperty.NAME])
product[ProductProperty.PROFIT] = product[
ProductProperty.PRICE] - product[ProductProperty.TOTAL_COST]
product[ProductProperty.TOTAL_PROFIT] = product[
ProductProperty.PROFIT] * product[ProductProperty.DEMAND]
product[ProductProperty.INGREDIENTS_wORTH] = Products.ingredients_worth(
product[ProductProperty.NAME])
product[ProductProperty.IS_WORTH_IT] = Products.is_worth_it(
product[ProductProperty.NAME])
index += 1
with open("src/data/products.json", "w") as file:
json.dump(products, file, indent="\t")
@staticmethod
def get_composition(name):
"""Returns the composition list of a product."""
return Products.get_property(name, ProductProperty.COMPOSITION)
@staticmethod
def get_cost(name):
"""Returns the bare cost of manufacturing a product."""
return Products.get_property(name, ProductProperty.COST)
@staticmethod
def get_price(name):
"""Returns the price of a product."""
return Products.get_property(name, ProductProperty.PRICE)
@staticmethod
def get_property(name, prop):
"""Returns the value of a given property for a product."""
return Products.get_product(name)[prop]
@staticmethod
def get_product(name):
"""Returns a product object from a name."""
for product in products:
if product["name"] == name:
return product
@staticmethod
def total_cost(name):
"""Returns the total cost of manufacturing a product."""
return reduce(lambda lastCost, newCost: lastCost + newCost, map(Products.total_cost, Products.get_composition(name)), Products.get_cost(name))
@staticmethod
def ingredients_worth(name):
"""Returns the total price of the ingredients of a product."""
return reduce(lambda lastPrice, newPrice: lastPrice + newPrice, map(Products.get_price, Products.get_composition(name)), 0)
@staticmethod
def is_worth_it(name):
"""Returns if a product is worth the cost of its ingredients."""
return Products.get_property(name, ProductProperty.PROFIT) >= Products.ingredients_worth(name)
Products.prepare_products()