-
Notifications
You must be signed in to change notification settings - Fork 0
/
Book Organizer.py
50 lines (38 loc) · 1.26 KB
/
Book Organizer.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
from operator import attrgetter
import csv
class Book():
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year
def organize(rule, collection):
if(rule == []):
return []
elif(rule[0][0] == "Null"):
raise Exception('OrderingException')
for r in rule:
if(r[1] == 'desc'):
desc = True
else:
desc = False
collection = sorted(collection, key=attrgetter(r[0]), reverse=desc)
return collection
def load_data(csv_file):
with open(csv_file, 'r') as f:
reader = csv.reader(f)
return list(reader)
#---------------------------------------------------------------------------
config_rule = load_data('config.csv')
collection = load_data('collection.csv')
book_collection = []
for b in collection:
book_collection.append(Book(b[0],b[1],b[2]))
organized_objects = organize(config_rule, book_collection)
organized_collection = []
for b in organized_objects:
organized_collection.append([b.title, b.author, b.year])
with open('Organized Collection.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(organized_collection)
f.close()
input("Livros Organizados\nPressione qualquer tecla para continuar...")