-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowse.py
149 lines (111 loc) · 4.98 KB
/
browse.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Your Name: Nikki Tebaldi
Class: CS 521 - Summer 2
Date: 08/18/2019
Term Project
Browse: A Module that contains functions that facilitate browsing a library
catalog.
Functions:
sort_collection_by_title(resource_list): Sort the collection by alphabetical
order by title.
display_collection_by_title_record(resource_list): Displays the collection
in record format.
display_collection_by_title_table(resource_list): Displays the collection in
table format.
"""
def sort_collection_by_title(resource_list):
"""
Sort the collection in alphabetical order by title using a instertion sort
algorithm.
Parameters:
resource_list (list): A list of resources
Returns:
resource_list(list): A list of resources sorted by title
"""
# Loop through resource_list
for i in range(len(resource_list)):
# Store current element in resource list
current_element = resource_list[i]
# Initialize another index value for comparison
k = i - 1
# Compare the titles of the current element and the element found at
# index k
while k >= 0 and resource_list[k].title > current_element.title:
# Insert the element found at index k into the element found at
# k + 1
resource_list[k + 1] = resource_list[k]
k -= 1
# Insert current element at k + 1 after completing index comparisons
resource_list[k + 1] = current_element
return resource_list
def display_collection_by_title_record(resource_list):
"""
Displays the collection in record format organized in alphabetical order
by title.
Parameters:
resource_list (list): List of Resource objects either Book or Movie
"""
# Obtain sorted resource_list
resource_list = sort_collection_by_title(resource_list)
# initialize a variable to keep track of number of resources displayed
resource_count = 0
# Loop through the list and display each record
print() # Separate output from main menu
for resource in resource_list:
print(resource, "\n")
resource_count += 1
# Display two records and then pause display; ask user to hit enter
# to continue
if resource_count % 2 == 0:
if resource_count == len(resource_list):
continue
input("\nPress 'Enter' to continue record display.\n")
def display_collection_by_title_table(resource_list):
"""
Displays the collection in table format organized in alphabetical order
by title.
Not all resource data is displayed. The table display is meant to show all
resources in the collection in an easy to read manner.
Parameters:
resource_list (list): List of Resource objects either Book or Movie
"""
# Obtain sorted resource_list
resource_list = sort_collection_by_title(resource_list)
# Display type
print("\nBOOKS:")
print("======")
# Display column names
print("{:7s} {:30s} {:20s} {:11s} {:9s} {:5s} {:8s} {:14s}"\
" {:9s} {:18s} {:20s}"
.format("UID", "Title", "Creator", "Genre", "Language", "Year",
"Country", "Publisher", "City", "Category",
"Keywords"))
# Display book resources
for resource in resource_list:
if resource.resource_type == "book":
print("{:<7d} {:30s} {:20s} {:11s} {:9s} {:<5d} {:8s} {:14s} "\
"{:9s} {:18s} {:20s}"
.format(resource.get_uid(), resource.title[:29],
resource.creator.get_full_name(), resource.genre[:10],
resource.language[:8], resource.year,
resource.country, resource.publisher[:13],
resource.city, resource.category,
resource.get_keyword_string()))
# Display type
print("\nMOVIES:")
print("=======")
# Display column names
print("{:7s} {:30s} {:20s} {:11s} {:9s} {:5s} {:8s} {:7s} {:35s} {:20s}"
.format("UID", "Title", "Creator", "Genre", "Language", "Year",
"Country", "Rating", "Writers", "Keywords"))
# Display movie resources
for resource in resource_list:
if resource.resource_type == "movie":
print("{:<7d} {:30s} {:20s} {:11s} {:9s} {:<5d} {:8s} {:7s} "\
"{:35s} {:20s}"
.format(resource.get_uid(), resource.title,
resource.creator.get_full_name(),
resource.genre, resource.language[:8], resource.year,
resource.country, resource.rating,
resource.get_names_string(resource.writers)[:35],
resource.get_keyword_string()))