Test Failed
Push — master ( 121a6b...418b67 )
by Oliver
03:25 queued 10s
created

_Politician_default.renamed_wards()   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
rs 9.95
c 0
b 0
f 0
cc 3
nop 1
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
"""
4
A set of dataclasses concerning roles of persons and their particulars.
5
"""
6
import os
7
import sys
8
from dataclasses import dataclass, field
9
from typing import List, Optional
10
11
PACKAGE_PARENT = ".."
12
SCRIPT_DIR = os.path.dirname(
13
    os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))
14
)  # isort:skip # noqa # pylint: disable=wrong-import-position
15
sys.path.append(
16
    os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))
17
)  # isort: skip # noqa # pylint: disable=wrong-import-position
18
19
from person import Person  # type: ignore  # noqa
20
from src.resources import helpers  # type: ignore  # noqa
21
from src.resources.constants import GERMAN_PARTIES  # type: ignore  # noqa
22
from src.resources.constants import PEER_PREPOSITIONS  # type: ignore # noqa
23
from src.resources.constants import PEERTITLES  # type: ignore # noqa
24
from src.resources.helpers import (  # type: ignore # noqa
25
    AttrDisplay,
26
    NotInRange,
27
    Party,
28
    TooManyFirstNames,
29
)
30
31
32
@dataclass
33
class _Politician_default:
34
    electoral_ward: str = field(default="ew")
35
    ward_no: Optional[int] = field(default=None)
36
    voter_count: Optional[int] = field(default=None)
37
    minister: Optional[str] = field(default=None)
38
    offices: List[str] = field(default_factory=lambda: [])
39
    parties: List[str] = field(default_factory=lambda: [])
40
41
    def renamed_wards(self):
42
        wards = {
43
            "Kreis Aachen I": "Aachen III",
44
            "Hochsauerlandkreis II – Soest III": "Hochsauerlandkreis II",
45
            "Kreis Aachen II": "Aachen IV"
46
            if self.last_name in ["Wirtz", "Weidenhaupt"]
47
            else "Kreis Aachen I",
48
        }
49
        if self.electoral_ward in wards.keys():
50
            self.electoral_ward = wards[self.electoral_ward]
51
52
    def scrape_wiki_for_ward(self) -> None:
53
        import requests
54
        from bs4 import BeautifulSoup  # type: ignore
55
56
        URL_base = "https://de.wikipedia.org/wiki/Landtagswahlkreis_{}"
57
        URL = URL_base.format(self.electoral_ward)
58
        req = requests.get(URL)
59
        bsObj = BeautifulSoup(req.text, "lxml")
60
        table = bsObj.find(class_="infobox float-right toptextcells")
61
        self.scrape_wiki_table_for_ward(table)
62
63
    def scrape_wiki_table_for_ward(self, table) -> None:
64
        for td in table.find_all("td"):
65
            if "Wahlkreisnummer" in td.text:
66
                ward_no = td.find_next().text.strip()
67
                ward_no = ward_no.split(" ")[0]
68
                self.ward_no = int(ward_no)
69
            elif "Wahlberechtigte" in td.text:
70
                voter_count = td.find_next().text.strip()
71
                voter_count = self.fix_voter_count(voter_count)
72
                self.voter_count = int(voter_count)
73
74
    def fix_voter_count(self, voter_count):
75
        if voter_count[-1] == "]":
76
            voter_count = voter_count[:-3]
77
        if " " in voter_count:
78
            voter_count = "".join(voter_count.split(" "))
79
        else:
80
            voter_count = "".join(voter_count.split("."))
81
        return voter_count
82
83
84
@dataclass
85
class Politician(
86
    _Politician_default,
87
    helpers._Party_default,
88
    Person,
89
    helpers._Party_base,
90
    AttrDisplay,
91
):
92
    def __post_init__(self):
93
        Party.__post_init__(self)
94
        Person.__post_init__(self)
95
        Person.get_sex(self)
96
        Person.get_age(self)
97
        self.change_ward()
98
        if self.party_name in GERMAN_PARTIES:
99
            self.parties.append(
100
                Party(self.party_name, self.party_entry, self.party_exit)
101
            )
102
        if self.minister and self.minister not in self.offices:
103
            self.offices.append(self.minister)
104
105
    def add_Party(
106
        self, party_name, party_entry="unknown", party_exit="unknown"
107
    ):  # noqa
108
        if party_name in GERMAN_PARTIES:
109
            if self.party_is_in_parties(party_name, party_entry, party_exit):
110
                pass
111
            else:
112
                self.parties.append(Party(party_name, party_entry, party_exit))
113
                self.party_name = party_name
114
                self.party_entry = party_entry
115
                self.party_exit = party_exit
116
117
    def align_party_entries(
118
        self, party, party_name, party_entry, party_exit
119
    ) -> Party:  # noqa
120
        if party_entry != "unknown" and party.party_entry == "unknown":
121
            party.party_entry = party_entry
122
        if party_exit != "unknown" and party.party_exit == "unknown":
123
            party.party_exit = party_exit
124
        return party
125
126
    def party_is_in_parties(self, party_name, party_entry, party_exit):
127
        parties_tmp = self.parties[:]
128
        for party in parties_tmp:
129
            if party_name == party.party_name:
130
                party_updated = self.align_party_entries(
131
                    party, party_name, party_entry, party_exit
132
                )
133
                self.parties.remove(party)
134
                self.parties.append(party_updated)
135
                self.party_entry = party_updated.party_entry
136
                self.party_exit = party_updated.party_exit
137
                return True
138
        return False
139
140
    def change_ward(self, ward=None):
141
        if ward:
142
            self.electoral_ward = ward
143
        if self.electoral_ward not in ["ew", "Landesliste"]:
144
            self.renamed_wards()
145
            self.scrape_wiki_for_ward()
146
        else:
147
            self.electoral_ward = "ew"
148
149
150
if __name__ == "__main__":
151
152
    politician = Politician(
153
        "SPD",
154
        "Bärbel",
155
        "Gutherz",
156
        academic_title="Dr.",
157
        date_of_birth="1980",
158
        electoral_ward="Köln I",
159
    )
160
    print(politician)
161