-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeInventory.py
executable file
·135 lines (103 loc) · 3.35 KB
/
HomeInventory.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
#!/usr/bin/env python3
"""
HomeInventory.py
Author: Dr. Andreas Janzen, janzen@gmx.net
Date: 2020-08-03
"""
import sqlite3
import sys
from contextlib import contextmanager
DB = "HomeInventory.db"
def first_launch():
""" Creates a new database on first launch """
try:
conn = sqlite3.connect(DB)
except:
sys.exit(f"Error: could not create database {DB}")
@contextmanager
def access_db():
""" Generator to yield a DB cursor and close the connection afterwards """
try:
conn = sqlite3.connect(DB)
cursor = conn.cursor()
yield cursor
finally:
conn.commit()
conn.close()
def scrub(text):
""" Removes all non-alphanumeric characters from the input string """
return "".join([chr for chr in text if chr.isalnum()])
def main_menu():
menu = dict()
menu["1"] = "Create new room"
menu["2"] = "Create new inventory"
menu["3"] = "Show inventory list"
menu["4"] = "Show total value"
menu["5"] = "Quit"
while True:
print("\n")
for item, description in sorted(menu.items()):
print(f"{item}) {description}")
print()
choice = scrub(input("Please chose an action > "))
if choice == "1":
create_room()
elif choice == "2":
create_inventory(select_room())
elif choice == "3":
show_inventory(select_room())
elif choice == "4":
show_value()
elif choice == "5":
sys.exit()
else:
print("\n=== Invalid input. Please try again! ===\n")
def room_list():
rooms = list()
with access_db() as cursor:
cursor.execute("SELECT name from sqlite_master WHERE type='table'")
for room in cursor:
rooms.append(room[0])
return rooms
def select_room():
while True:
print("\n")
for room in room_list():
print(room)
selection = input("\nPlease select a room: ").lower()
if selection not in room_list():
print(f"Room does not exist: {selection}")
else:
return scrub(selection)
def create_room():
name = input("\nWhat name would you like to give the room? > ")
name = scrub(name)
with access_db() as cursor:
cursor.execute("CREATE TABLE '" + name.lower() + "' (Item TEXT, Value REAL)")
print(f"\nA room with name '{name}' has been added to the database.\n")
def create_inventory(room):
while True:
item = scrub(input("Please specify item name > "))
value = int(input("Please specify item value > "))
with access_db() as cursor:
cursor.execute("INSERT INTO '" + room + "' VALUES(?,?)", (item, value))
cont = input("\nWould you like to add another item or (q)uit? > ")
if cont.lower() == "q":
break
def show_inventory(room):
total_value = 0
with access_db() as cursor:
cursor.execute("SELECT * FROM '" + room + "'")
print("\n")
for item in cursor:
print(f"{item[0]}, value: {item[1]}")
total_value += item[1]
print(f"\nTotal value of all items in room {room}: {total_value}")
return total_value
def show_value():
total_value = 0
for room in room_list():
total_value += show_inventory(room)
print(f"\nTotal value of all rooms: {total_value}")
first_launch()
main_menu()