-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
73 lines (55 loc) · 2.66 KB
/
backend.py
File metadata and controls
73 lines (55 loc) · 2.66 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
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
import pandas as pd
import numpy as np
class Backend:
def __init__(self, noload):
self.columns = ["Title", "Author", "Genre", "Pages"]
if not noload:
self.dataframe = self.load_dataframe_from_disk()
else:
self.dataframe = pd.DataFrame(columns=self.columns)
def load_dataframe_from_disk(self):
try:
data = pd.read_csv("database.csv", index_col=0)
except:
print("FATAL ERROR, could not load database from disk to ram!")
print("Creating an empty database...")
data = pd.DataFrame(columns=self.columns)
return data
def write_dataframe_to_disk(self):
try:
self.dataframe.to_csv("database.csv", encoding='utf-8')
except:
print("FATAL ERROR, could not write database from ram to disk! Do not close the program or you will have data loss.")
def get_columns(self): # gets the list of columns types in the current DB
return self.dataframe.columns.tolist()
def add_book(self, atributes_of_book): # expects list with atributes of book to be added
self.dataframe.loc[len(self.dataframe.index)] = atributes_of_book
def remove_book(self, book_id): # removes book by index
self.dataframe = self.dataframe.drop(book_id)
def sort_by(self, type, mode): # returns a sorted dataframe and expects a column type and the way to sort
columns = self.get_columns()
if not type in columns:
return pd.DataFrame(columns=self.columns)
error_log("The database does not contain the criteria you tried sorting by.")
else:
if mode == "desc":
return self.dataframe.sort_values(by=type, ascending=False)
elif mode == "asc":
return self.dataframe.sort_values(by=type, ascending=True)
else:
return pd.DataFrame(columns=self.columns)
error_log('Invalid argument for mode, use terms "asc" ord "desc".')
def filter_for(self, type, argument): # returns a filtered dataframe and expects a column type and the argument to search for
columns = self.get_columns()
if not type in columns:
return pd.DataFrame(columns=self.columns)
error_log("The database does not contain the criteria you tried filtering by.")
else:
return self.dataframe.loc[self.dataframe[type] == argument]
def db_debug_display(self): # debug/testing method
print(self.dataframe)
def get_dataframe(self):
return self.dataframe
def error_log(error): # error log curently goes to print
print(error)