-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct.py
More file actions
180 lines (157 loc) · 7.21 KB
/
product.py
File metadata and controls
180 lines (157 loc) · 7.21 KB
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
176
177
178
179
180
import getpass, os, hashlib, random
from globals import *
def product_create_dict( product_id,
product_seller_id,
product_category,
product_name,
product_description,
product_quantity,
product_unit_price
):
"""Creates a dictionary from individual items.
This is a helper function useful after reading
items from product.db
"""
#create an emptry dictionary
new_product_dict = {}
#populate the fields
new_product_dict["product_id"] = product_id
new_product_dict["product_seller_id"] = product_seller_id
new_product_dict["product_category"] = product_category
new_product_dict["product_name"] = product_name
new_product_dict["product_description"] = product_description
new_product_dict["product_quantity"] = product_quantity
new_product_dict["product_unit_price"] = product_unit_price
#return the newly created and populated dictionary
return new_product_dict
def product_load_db():
"""Reads the contents of product.db
The contents are placed in the products
global variable defined in globals.py
"""
#we are using the global variable
global products
#open the file for reading
product_db_handle = open("data/product.db","r")
#read all the lines
lines = product_db_handle.readlines()
#remove existing items in the products list
products.clear()
#initialize the count to zero
count = 0
for line in lines:
count += 1
fields = line.strip().split(",")
#we call product_create_dict() to construct
#a dictionary for us using the fields read
#from the file. This dictionary is appended
#to the global 'products' variable
products.append(product_create_dict( fields[0],
fields[1],
fields[2],
fields[3],
fields[4],
fields[5],
fields[6]
))
#close the handle
product_db_handle.close()
def product_init():
"""Initializes the product module.
It creates the product.db if it is absent
then calls product_load_db().
"""
if not os.path.exists("data/product.db"):
product_db_handle = open("data/product.db","w")
product_db_handle.close()
product_load_db()
def product_save_dict(product_dict):
"""Saves a dictionary of a product to product.db.
"""
product_db_handle = open("data/product.db","a+")
#construct the output line
output_line = str(product_dict["product_id"]+","+
product_dict["product_seller_id"]+","+
product_dict["product_category"]+","+
product_dict["product_name"]+","+
product_dict["product_description"]+","+
product_dict["product_quantity"]+","+
product_dict["product_unit_price"]+"\n")
#write to file then close the handle
product_db_handle.write(output_line)
product_db_handle.close()
#return a dictionary with the product_category as key
def product_get_categories():
"""Returns a dictionary of product_category as key.
"""
#we are using the global variable
global products
#return product_category as key
return {product_dict["product_category"]:product_dict["product_id"] for product_dict in products}
#product search
def product_search(search_term):
"""Searchs for a product.
"""
#load the product.db
product_load_db()
#we are using the global variable
global products
#create search_results
product_search_dict = {}
#search for the product using the product_name key, product_category key, and product_description key in a loop
for product_dict in products:
if search_term.lower() in product_dict["product_category"].lower() or search_term.lower() in product_dict["product_name"].lower() or search_term.lower() in product_dict["product_description"].lower():
#add all the matching products to the search_results dictionary
product_search_dict[product_dict["product_id"]] = product_dict
#return the search_results dictionary
return product_search_dict
def product_view_search():
"""Searchs for a product.
"""
#we are using the global variable
global products
#enter search term
product_search_term = input("Keyword: ")
#use product_search() to search for products and store the value
product_search_dict = product_search(product_search_term)
#count the number of search results and print
print(str(len(product_search_dict))+" match(es) found")
#print the search results
for key, product_search_results in product_search_dict.items():
#print product id
print(" [ " + str(key)+" ] - "+ str(product_search_results["product_name"]) + " , "+str(product_search_results["product_category"]) + " , "+ str(product_search_results["product_description"]) + " , "+str(product_search_results["product_unit_price"])+" per unit, "+str(product_search_results["product_quantity"])+" unit(s)")
input("Press [ENTER] to continue..")
return product_search_dict
def product_flush_to_file():
product_db_handle = open("data/product.db","w")
for product_dict in products:
output_line = str(product_dict["product_id"]+","+
product_dict["product_seller_id"]+","+
product_dict["product_category"]+","+
product_dict["product_name"]+","+
product_dict["product_description"]+","+
product_dict["product_quantity"]+","+
product_dict["product_unit_price"]+"\n")
product_db_handle.write(output_line)
product_db_handle.close()
#display random 5 products
def product_view_random():
#we are using the global variable
#load product databes
product_load_db()
global products
#check if products is less than 5
#check if products is empty
if len(products) == 0:
print("No products available")
elif len(products) < 5:
#print the products
for product_dict in products:
print("[ "+ str(product_dict["product_id"])+" ] - "+ str(product_dict["product_name"]) + " , "+str(product_dict["product_category"]) + " , "+ str(product_dict["product_description"]) + " , "+str(product_dict["product_unit_price"])+" per unit, "+str(product_dict["product_quantity"])+" unit(s)")
else:
#randomly select 5 products
random_products = random.sample(products,5)
#print the products
for random_product in random_products:
print("[ "+ str(random_product["product_id"])+" ] - "+ str(random_product["product_name"]) + " , "+str(random_product["product_category"]) + " , "+ str(random_product["product_description"]) + " , "+str(random_product["product_unit_price"])+" per unit, "+str(random_product["product_quantity"])+" unit(s)")
input("Press [ENTER] to continue..")