Skip to content

Commit b06e8ec

Browse files
committed
Merge branch 'master' of github.com:talkpython/100daysofcode-with-python-course
2 parents 5ceca8b + 2929a91 commit b06e8ec

File tree

8 files changed

+179
-2
lines changed

8 files changed

+179
-2
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
# 100daysofcode-with-python-course
2-
Course materials and handouts for #100DaysOfCode in Python course
1+
# #100DaysOfCode with Python course
2+
3+
[![Visit the course page](readme_resources/100days-course.png)](https://training.talkpython.fm/courses/explore_100days_in_python/100-days-of-code-in-python)
4+
5+
6+
7+
File renamed without changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Days 88-90: Create a Home Inventory App
2+
3+
This is one of those "scratch your own itch" projects. Something we all need to do is properly track the contents of our homes for insurance purposes right? Here's an app to do it!
4+
5+
The app uses sqlite3 for the database and even has a generator thrown in for good measure!
6+
7+
8+
## Day N: Watch videos and start planning!
9+
10+
Watch the first 3x videos detailing the *Main Menu*, *SQLite3 DB Access* and *Scrubbing Malicious Data* for your first day.
11+
12+
Start visualising how you'll form and write your app then get coding if you have time!
13+
14+
15+
## Day N+1: App Run Through
16+
17+
Today we do a quick run through of the entire app. You'll see how all of the functions work together to form the final product.
18+
19+
Watch the *App Demonstration* video and start coding up your own solution if you haven't started already!
20+
21+
22+
## Day N+2: Your Turn!
23+
24+
Watch the video *App Pitfalls and Bugs* and see if you're able to resolve the bugs!
25+
26+
These are all issues you'll need to take into account in your own app so it's worth giving it a try.
27+
28+
If you have even more time, see how you can expand this app to have its own GUI or web interface.
29+
30+
31+
### Time to share what you've accomplished!
32+
33+
Be sure to share your last couple of days work on Twitter or Facebook. Use the hashtag **#100DaysOfCode**.
34+
35+
Here are [some examples](https://twitter.com/search?q=%23100DaysOfCode) to inspire you. Consider including [@talkpython](https://twitter.com/talkpython) and [@pybites](https://twitter.com/pybites) in your tweets.
36+
37+
*See a mistake in these instructions? Please [submit a new issue](https://github.com/talkpython/100daysofcode-with-python-course/issues) or fix it and [submit a PR](https://github.com/talkpython/100daysofcode-with-python-course/pulls).*
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!python3
2+
3+
import sqlite3
4+
import sys
5+
from contextlib import contextmanager
6+
7+
DB = "Inventory.db"
8+
9+
#Script that creates the DB on first launch, otherwise is just a check.
10+
def first_launch():
11+
try:
12+
conn = sqlite3.connect(DB)
13+
except:
14+
sys.exit('Error code X')
15+
16+
17+
#generator to yield a db cursor and close the connection nicely.
18+
@contextmanager
19+
def access_db():
20+
try:
21+
conn = sqlite3.connect(DB)
22+
cursor = conn.cursor()
23+
yield cursor
24+
finally:
25+
conn.commit()
26+
conn.close()
27+
28+
29+
#A somewhat dirty menu. Improvement Point (make it a dict perhaps)
30+
def main_menu():
31+
menu = {}
32+
menu['1'] = "Add Room."
33+
menu['2'] = "Add Inventory."
34+
menu['3'] = "View Inventory List."
35+
menu['4'] = "Total Value."
36+
menu['5'] = "Exit."
37+
while True:
38+
print("\n")
39+
for item, desc in sorted(menu.items()):
40+
print(item, desc)
41+
42+
choice = input("Selection: ")
43+
if choice == '1':
44+
add_room()
45+
elif choice == '2':
46+
add_inventory(check_input())
47+
elif choice == '3':
48+
view_inventory(check_input())
49+
elif choice == '4':
50+
calc_total()
51+
elif choice == '5':
52+
sys.exit()
53+
else:
54+
print("Invalid option, try again.")
55+
56+
57+
#Adds a room. Scrubs the input first to only allow default chars.
58+
def add_room():
59+
name = input("\nWhat name would you like to give the room? ")
60+
name = scrub(name)
61+
with access_db() as cursor:
62+
cursor.execute("CREATE TABLE '" + name.lower() + "' """"
63+
(Item TEXT, Value REAL)
64+
""")
65+
print("\nA room with name %s has been added to the db.\n" % name)
66+
67+
68+
#Parses the DB for table names and returns a list of the names.
69+
def list_rooms():
70+
room_list = []
71+
with access_db() as cursor:
72+
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
73+
for room in cursor:
74+
room_list.append(room[0])
75+
return room_list
76+
77+
78+
#The scrubbing function. Removes all chars that aren't letters and numbers.
79+
def scrub(table_name):
80+
return ''.join( chr for chr in table_name if chr.isalnum() )
81+
82+
83+
#Checks the users' input to see if it matches a room/table name.
84+
def check_input():
85+
while True:
86+
print('\n')
87+
for room in list_rooms():
88+
print(room)
89+
selection = input('Select a room: ').lower()
90+
if selection not in list_rooms():
91+
print("\n%s does not exist." % selection)
92+
else:
93+
return scrub(selection)
94+
95+
96+
#Allows users to add an item and value to the DB of a specific room.
97+
def add_inventory(selection):
98+
while True:
99+
name = input("\nName of item: ")
100+
cost = input("Monetary value: ")
101+
with access_db() as cursor:
102+
cursor.execute("INSERT INTO '" + selection + "' VALUES(?, ?)", [name, cost])
103+
104+
cont = input('\nHit Q to quit or any other key to continue: ')
105+
if cont.lower() == 'q':
106+
break
107+
108+
109+
#Returns a list of all items in a room and their total value.
110+
def view_inventory(selection):
111+
total = 0
112+
with access_db() as cursor:
113+
cursor.execute("SELECT * FROM '" + selection + "'")
114+
print("\n")
115+
for data in cursor:
116+
print("%s: $%d" % (data[0], data[1]))
117+
total += data[1]
118+
print("Total Value: $%d" % total)
119+
120+
121+
#Function to calculate the $ total of the entire database.
122+
def calc_total():
123+
total = 0
124+
room_list = list_rooms()
125+
with access_db() as cursor:
126+
for room in room_list:
127+
cursor.execute("SELECT value FROM '" + room + "'")
128+
for value in cursor:
129+
total += value[0]
130+
print("\nTotal Value of all rooms: $%d" % total)
131+
132+
133+
if __name__ == "__main__":
134+
first_launch()
135+
main_menu()

readme_resources/100days-course.png

162 KB
Loading

0 commit comments

Comments
 (0)