Passed
Push — main ( bc64e0...ce7d48 )
by Eran
01:35
created

music.albums_()   B

Complexity

Conditions 6

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 17
rs 8.6666
c 0
b 0
f 0
cc 6
nop 0
1
import json
2
import pathlib
3
from collections import defaultdict
4
from dataclasses import dataclass
5
from datetime import datetime, timedelta
6
7
from lxml import etree
8
9
10
def file_path(file: str) -> pathlib.Path:
11
    current_script_path = pathlib.Path(__file__).resolve()
12
    parent_dir = current_script_path.parent
13
    return parent_dir / file
14
15
16
def xml_to_dict(element):
17
    if len(element) == 0:
18
        return element.text
19
    return {child.tag: xml_to_dict(child) for child in element}
20
21
22
@dataclass
23
class Entry:
24
    artist: str
25
    title: str
26
    type: str
27
    time: timedelta
28
    genre: str
29
    label: str
30
    performers: set[str]
31
    year: int
32
    added: datetime
33
34
35
def albums_():
36
    with open("albums.json") as f:
37
        albums = json.load(f)
38
39
        artist_titles = defaultdict(list)
40
        title_performers = defaultdict(set)
41
        performer_instruments = defaultdict(set)
42
        for album in albums:
43
            artist_titles[album.get('Artist')].append(album.get('Title'))
44
            performers = [tuple(p.strip().split(':')) for p in album.get('Performer').split('/')]
45
            for items in performers:
46
                performer = items[0].strip()
47
                title_performers[album.get('Title')].add(performer)
48
49
                if len(items) > 1:
50
                    for instrument in items[1].strip().split(','):
51
                        performer_instruments[performer].add(instrument)
52
53
54
if __name__ == "__main__":
55
    tree = etree.parse(file_path('db.xml'))
56
    root = tree.getroot()
57
    data_dict = xml_to_dict(root)
58
    print(data_dict)
59