The try-except block in load_expenses from main.py does not seem to do it's job properly.
The try will never fail itself because it can read the csv perfectly fine even without the correct headers, and the assertion is only done after the try-except block. Thus, the header column will never be added in the except.
A fix would be to add one more validate_column_names step into the try block (which would allow it to actually fail).
try:
df = pd.read_csv(DATA_FILE, header=0, encoding='UTF8')
# check for valid column names and raise AssertionError if not
assert validate_column_names(df)
# or alternatively
if not validate_column_names(df):
raise NameError("whatever error message...")
except:
with open(DATA_FILE, 'w', newline='') as csv_file:
csv.writer(csv_file).writerow(['Title','Category','Date','Amount'])
df = pd.read_csv(DATA_FILE, header=0)
# Check if every column needed is there
if not validate_column_names(df):
sys.exit()
EDIT:
I've just tried that fix, and it loads the file successfully now. However, it also erases all the entries from the file! I guess that is due to the csv.writer(...) code. Hence, I would recommend, to replace this piece of code with
# replace
with open(DATA_FILE, 'w', newline='') as csv_file:
csv.writer(csv_file).writerow(['Title','Category','Date','Amount'])
df = pd.read_csv(DATA_FILE, header=0)
# with this instead:
df = pd.read_csv(DATA_FILE, names = ["Title",...])
This will preserve the file and still add the correct column names (although only to the df and not the file itself)
The try-except block in
load_expenses
frommain.py
does not seem to do it's job properly. The try will never fail itself because it can read the csv perfectly fine even without the correct headers, and the assertion is only done after the try-except block. Thus, the header column will never be added in the except.A fix would be to add one more
validate_column_names
step into the try block (which would allow it to actually fail).EDIT:
I've just tried that fix, and it loads the file successfully now. However, it also erases all the entries from the file! I guess that is due to the
csv.writer(...)
code. Hence, I would recommend, to replace this piece of code withThis will preserve the file and still add the correct column names (although only to the df and not the file itself)