Passed
Push — master ( 062d8e...ae8a23 )
by Steffen
02:13
created

Player.remove_control_characters()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
#!/usr/local/bin/python
2
# coding: utf-8
3 1
import unicodedata
4
5 1
from pycountry import countries
6
7 1
from tw_serverinfo.utility.countries import custom_countries
8
9
10 1
class Player(object):
11
    """Player Model Template, containing properties for all possible attributes of players"""
12 1
    _name = ''
13 1
    _clan = ''
14 1
    _country_index = '-1'
15 1
    _score = 0
16 1
    _ingame = False
17
18 1
    def __init__(self, name: str, clan: str = '', country: int = -1, score: int = 0, ingame: bool = False) -> None:
19
        """Initializing function
20
21
        :type name: str
22
        :type clan: str
23
        :type country: int
24
        :type score: int
25
        :type ingame: bool
26
        """
27 1
        self.name = name
28 1
        self.clan = clan
29 1
        self.country = str(country)
30 1
        self.score = score
31 1
        self.ingame = ingame
32
33
    def __repr__(self) -> str:
34
        """Reprint function, displays player details instead of instance information
35
36
        :return:
37
        """
38
        return 'Player(name={name:s}, clan={clan:s}, country={country:s}, country_index={country_index:d}, ' \
39
               'score={score:d}, ingame={ingame!r})'.format(name=self.name, clan=self.clan, country=self.country,
40
                                                            country_index=self.country_index, score=self.score,
41
                                                            ingame=self.ingame)
42
43 1
    def __eq__(self, other) -> bool:
44
        """Check for equality of objects
45
46
        :type other: Player
47
        :return:
48
        """
49 1
        return self.name == other.name and self.clan == other.clan
50
51 1
    @property
52 1
    def name(self) -> str:
53 1
        return self._name
54
55 1
    @name.setter
56 1
    def name(self, name: str) -> None:
57 1
        self._name = self.remove_control_characters(name)
58
59 1
    @property
60 1
    def clan(self) -> str:
61 1
        return self._clan
62
63 1
    @clan.setter
64 1
    def clan(self, clan: str) -> None:
65 1
        self._clan = self.remove_control_characters(clan)
66
67 1
    @property
68 1
    def country(self) -> str:
69 1
        if self._country_index in custom_countries:
70 1
            return custom_countries[self._country_index]['name']
71
        else:
72 1
            return countries.get(numeric=str(self._country_index)).name
73
74 1
    @country.setter
75 1
    def country(self, country: str) -> None:
76 1
        if country in custom_countries:
77 1
            self._country_index = country
78
        else:
79 1
            zfilled_country = country.zfill(3)
80
            # only allow to set indexes which are known
81 1
            try:
82 1
                self._country_index = countries.get(numeric=zfilled_country).numeric
83 1
            except KeyError:
84 1
                pass
85
86 1
    @property
87 1
    def country_code(self) -> str:
88 1
        if self._country_index in custom_countries:
89 1
            return custom_countries[self._country_index]['code']
90
        else:
91 1
            return countries.get(numeric=str(self._country_index)).alpha_2
92
93 1
    @property
94 1
    def country_index(self) -> int:
95 1
        return int(self._country_index)
96
97 1
    @property
98 1
    def score(self) -> int:
99 1
        return self._score
100
101 1
    @score.setter
102 1
    def score(self, score: int) -> None:
103 1
        self._score = score
104
105 1
    @property
106 1
    def ingame(self) -> bool:
107 1
        return self._ingame
108
109 1
    @ingame.setter
110 1
    def ingame(self, ingame: bool) -> None:
111 1
        self._ingame = ingame
112
113 1
    @staticmethod
114 1
    def remove_control_characters(string) -> str:
115
        """Remove control characters which should not be in the string but appear sometimes in player strings
116
117
        :type string: str
118
        :return:
119
        """
120
        return "".join(ch for ch in string if unicodedata.category(ch)[0] != "C")
121