-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorignal_File.py
More file actions
61 lines (51 loc) · 1.34 KB
/
orignal_File.py
File metadata and controls
61 lines (51 loc) · 1.34 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
import json
import logging
from datetime import datetime
# Global variable
stock_data = {}
def addItem(item="default", qty=0, logs=[]):
if not item:
return
stock_data[item] = stock_data.get(item, 0) + qty
logs.append("%s: Added %d of %s" % (str(datetime.now()), qty, item))
def removeItem(item, qty):
try:
stock_data[item] -= qty
if stock_data[item] <= 0:
del stock_data[item]
except:
pass
def getQty(item):
return stock_data[item]
def loadData(file="inventory.json"):
f = open(file, "r")
global stock_data
stock_data = json.loads(f.read())
f.close()
def saveData(file="inventory.json"):
f = open(file, "w")
f.write(json.dumps(stock_data))
f.close()
def printData():
print("Items Report")
for i in stock_data:
print(i, "->", stock_data[i])
def checkLowItems(threshold=5):
result = []
for i in stock_data:
if stock_data[i] < threshold:
result.append(i)
return result
def main():
addItem("apple", 10)
addItem("banana", -2)
addItem(123, "ten") # invalid types, no check
removeItem("apple", 3)
removeItem("orange", 1)
print("Apple stock:", getQty("apple"))
print("Low items:", checkLowItems())
saveData()
loadData()
printData()
eval("print('eval used')") # dangerous
main()