-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase_module.py
More file actions
39 lines (32 loc) · 958 Bytes
/
database_module.py
File metadata and controls
39 lines (32 loc) · 958 Bytes
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
# main program: inventory_tracker.py
# this module handles the database;
# checking for db file, creates one if none found, reads and writes to file
# 06.13.2022
import json
import csv
import ast
database = 'inventory_db.json'
def db_check():
"""Checks for json file. Creates one and initializes inv_list, if none."""
# database = 'inventory_db.json'
try:
with open(database) as db:
data = json.load(db)
except FileNotFoundError:
with open(database, 'w') as db:
inv_list = []
json.dump(inv_list, db)
print("Database created!\n")
else:
print("Database ready...\n")
inv_list = data
def write(data):
"""Writes the inventory list to json file; inventory_db.json"""
with open(database, 'w') as db:
json.dump(data, db, indent=2)
print("\nWriting to database...")
def read():
"""Loads inventory list from json file (inventory_db.json) into python object"""
with open(database, 'r') as db:
data = json.load(db)
return data