-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask2.py
150 lines (103 loc) · 3.69 KB
/
task2.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
from database_interface import *
from collections import Counter
import cyrtranslit
import task3
def a():
albums = fetch_all_albums_from_database()
genres = {}
for album in albums:
genres_list = album['genre'].split("#")
for genre in genres_list:
genres[genre] = genres.get(genre, 0) + 1
return sorted(genres.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
def b():
albums = fetch_all_albums_from_database()
styles = {}
for album in albums:
styles_list = album['style'].split("#")
for style in styles_list:
styles[style] = styles.get(style, 0) + 1
return sorted(styles.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
def c(top: int):
albums = fetch_all_albums_from_database()
sorted_albums = sorted(albums, key= lambda x: x['versions'], reverse=True)
sorted_albums = [[album['title'], album['versions']] for album in sorted_albums]
ret1 = list()
i = 0
last = -1
while i < len(sorted_albums) and len(ret1)< top:
if len(ret1) == 0 or sorted_albums[i][1] != last:
last = sorted_albums[i][1]
ret1.append([last, set([sorted_albums[i][0]])])
i+=1
continue
ret1[-1][1].add(sorted_albums[i][0])
last = sorted_albums[i][1]
i+=1
c = Counter([album['title'] for album in albums])
ret2 = c.most_common(top)
c = Counter(["{0}#{1}".format(album['title'], album['artist']) for album in albums])
ret3 = c.most_common(top)
ret3 = [[string.split('#')[0], num] for string, num in ret3]
return [ret1, ret2, ret3]
def d():
artists = fetch_all_artists_from_database()
ret = dict()
for metric in ['credits', 'credits_cnt', 'credits_songs',
'vocals', 'vocals_cnt', 'vocal_songs',
'arranged', 'arranged_cnt', 'arranged_songs',
'lyrics', 'lyrics_cnt', 'lyrics_songs',
'music', 'music_cnt', 'music_songs']:
res = sorted(artists, key=lambda x: x[metric], reverse=True)[0:100]
res = [[x[metric], x['name']] for x in res]
ret[metric] = res
return ret
def to_latin(string: str):
if task3.is_cyrillic(string):
return cyrtranslit.to_latin(string)
return string
def e(top: int, generate_report=False):
songs = fetch_all_songs_from_database()
song_names = list()
for song in songs:
name = to_latin(song['name'])
song_names.append(name)
c = Counter(song_names)
counted_songs = c.most_common(top)
if generate_report:
res = list()
for counted_song in counted_songs:
cnt = counted_song[1]
name = counted_song[0]
lst = list()
for song in songs:
if to_latin(song['name']) == name:
sng = {
'format': song['format'],
'country': song['country'],
'year': song['year'],
'genre': song['genre'],
'style': song['style'],
'url': song['url']
}
lst.append(sng)
res.append((cnt, name, lst))
save_dictionary_to_json_file('tast2_e.json', res)
return counted_songs
def f():
artists = fetch_all_artists_from_database()
ret = list()
for artist in artists:
if artist['sites'] != None:
ret.append([artist['name'], ", ".join(artist['sites'])])
return ret
if __name__ == '__main__':
#print(a())
#print(b())
#print('C')
#for item in c(20):
#print(item)
print(d())
# TODO check
#print(e(100, True))
#print(f())