-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
201 lines (163 loc) · 4.76 KB
/
database.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import numpy as np
from collections import defaultdict
import pickle
# initializing default dictionary with default value = None
# and songID dictionary with default value also = None
fingerprints = defaultdict(None)
songID = defaultdict(None)
def addFingerprint(prints: list): #fp (1,2,3) : [(1,2), (1,3)] --song
# fingerprints.py returns a list of fingerprints and a list of times
# so we probably have to change our parameters - Josh
# check out fingerprints in fingerprints.py
# we can steal Ryan's code for this function too -
# for (fm, fn, dt), tm in key:
# database[(fm, fn, dt)].append(("song-j", tm))
"""
Adds fingerprint key/value pair to the fingerprints dictionary
Parameters
----------
key: tuple, shape = [(float, float, float), int, float]
list of fingerprints with each element in the form [(f_i, f_j, ∆t_ij), ti, id]
"""
for key, id, time in prints:
if key not in fingerprints:
fingerprints[key] = [(id, time)]
else:
fingerprints[key].append((id, time))
def removeFingerprint(key: tuple, value: tuple):
"""
Removes input fingerprint from dictionary
Parameters
----------
key: tuple, shape = [int, int, int]
fingerprint in the form (f_i, f_j, ∆t_ij)
value: tuple, shape = [int, int]
Tuple containing songID and time pair.
"""
if key not in fingerprints:
return "No such finger print exists :("
else:
values = fingerprints[key]
if len(values) == 1:
del(fingerprints[key])
else:
values.remove(value)
fingerprints[key] = values
def saveFingerprints():
"""
Saves the fingerprints dictionary as fingerprints.pkl
"""
with open("fingerprints.pkl", mode="wb") as opened_file:
pickle.dump(fingerprints, opened_file)
def loadFingerprints():
"""
Loads fingerprints.pkl and returns it as a dictionary
Returns
--------
defaultdict
defaultdict containing the saved fingerprints
"""
with open("fingerprints.pkl", mode="rb") as opened_file:
fingerprints1 = pickle.load(opened_file)
return fingerprints1
def getFingerprint(key: tuple):
"""
Gets all times in all songs where a certain fingerprint occurs
Parameters
-----------
key : tuple, shape = (3, )
The fingerprint whos times and songs we want to retrieve
Returns
------------
List[Tuple[int, int]]
List of tuples containing songIDs and times
"""
try:
return fingerprints[key]
except:
return []
def addSongID(id: int, songInfo: tuple):
"""
Add new song ID and informatino to the songID dictionary
Parameters
----------
id: int
Value of the song ID to be added
songInfo: tuple[string, string]
Tuple containing the song name and artist
"""
songID[id] = [songInfo]
def removeSongID(id: int):
"""
Removes input id from songID dictionary
Parameters
-----------
id: int
Value of the song ID to be removed
"""
if id in songID:
del songID[id]
def saveSongIDs():
"""
Saves the songID dictionary as songID.pkl
"""
with open("songID.pkl", mode="wb") as opened_file:
pickle.dump(songID, opened_file)
def loadSongIDs():
"""
Loads songID.pkl and returns it as defaultdict
Returns
--------
defaultdict
Contains the saved song IDs and song info.
"""
with open("songID.pkl", mode="rb") as opened_file:
songID1 = pickle.load(opened_file)
return songID1
def getSong (key: int):
"""
Gets name of song from songID dictionary given its songID.
return
-------
tuple[string, string]
Contains the song name and artist
"""
try:
return songID[key]
except:
return []
def generateID(songN: str):
"""
Generates a new ID and SongName pairing and adds it into the SongID dict
Parameters
-----------
songN: String
Name of the song
return
-------
NewId: int
ID of the song added to songID dict
"""
try:
newID = max(songID) + 1
except:
newID = 1
return newID
'''
addFingerprint([[(1, 2, 3), 4, 1], [(1, 2, 3), 5, 1], [(1, 2, 3), 6, 2]])
addFingerprint([[(4, 5, 6), 4, 3]])
print(fingerprints)
removeFingerprint((4, 5, 6), (4, 1))
'''
'''
addSongID(1, ("Halo Theme", "Ryan Soklaski"))
addSongID(2, ('Waluigiverse', 'Ryan Soklaski'))
addSongID(3, ('Never Gonna Give You Up', 'Ryan Soklaski'))
addSongID(4, ("Song of Storms", 'Ryan Soklaski'))
addSongID(2, ("Cotton Eye Joe", 'Ryan Soklaski'))
removeSongID(3)
removeSongID(2)
removeSongID(3)
print(songID)
saveSongIDs()
'''