Test Failed
Push — master ( 989503...89b123 )
by Oliver
01:57
created

generate_unique_key()   A

Complexity

Conditions 5

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 12
rs 9.3333
c 0
b 0
f 0
cc 5
nop 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
"""Helper functions: exceptions, print style, Party, ..."""
4
5
import random
6
from dataclasses import dataclass, field
7
from typing import List
8
9
from .constants import GERMAN_PARTIES  # type: ignore  # noqa
10
11
12
class NotInRange(Exception):
13
    """For state NRW only terms 14 to currently term 17 are accepted."""
14
15
16
class NotGermanParty(Exception):
17
    """Only German parties, this will most likely not change."""
18
19
20
class TooManyFirstNames(Exception):
21
    """
22
    Currently only one first name and two middle names are supported.
23
    Example: Tom H. Paul last_name
24
    """
25
26
    def __init__(self, message):
27
        """Usage: raise TooManyFirstNames ("message")."""
28
        Exception.__init__(self)
29
        print(message)
30
31
32
class AttrDisplay:
33
    """
34
    Mark Lutz, Programming Python
35
    Provides an inheritable display overload method that shows instances
36
    with their class names and a name=value pair for each attribute stored
37
    on the instance itself (but not attrs inherited from its classes). Can
38
    be mixed into any class, and will work on any instance.
39
    """
40
41
    def gather_attrs(self) -> list:
42
        """
43
        Check if attributes have content and add them to a list called attrs.
44
        """
45
        attrs = []
46
        for key in sorted(self.__dict__):
47
            if self.__dict__[key] and self.__dict__[key] not in [
48
                "unknown",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
49
                "ew",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
50
                None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
51
            ]:
52
                attrs.append(f"{key}={getattr(self, key)}")
53
        return attrs
54
55
    def __str__(self) -> str:
56
        """
57
        Instances will printed like this:
58
            class name
59
            attr1=value1
60
            attr2=value2
61
            ...
62
        """
63
        comp_repr = (
64
            f"{self.__class__.__name__}:\n"
65
            + "\n".join(str(attr) for attr in self.gather_attrs())
66
            + "\n"
67
        )
68
        return comp_repr
69
70
71
@dataclass
0 ignored issues
show
Coding Style Naming introduced by
Class name "_Party_base" doesn't conform to PascalCase naming style ('[^\\W\\da-z][^\\W_]+$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
72
class _Party_base:
73
    """Name of party is required, surprisingly."""
74
    party_name: str  # type: ignore  # noqa
75
76
77
@dataclass
0 ignored issues
show
Coding Style Naming introduced by
Class name "_Party_default" doesn't conform to PascalCase naming style ('[^\\W\\da-z][^\\W_]+$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
78
class _Party_default:
79
    """Party entry and exit are possible additions, if known."""
80
    party_entry: str = field(default="unknown")
81
    party_exit: str = field(default="unknown")
82
83
84
@dataclass
85
class Party(_Party_default, _Party_base, AttrDisplay):
86
    """Collect party name and entry/exit data."""
87
    def __post_init__(self):
88
        """Checking for German parties."""
89
        if self.party_name not in GERMAN_PARTIES:
90
            raise NotGermanParty
91
92
93
# https://codereview.stackexchange.com/questions/200355/generating-a-unique-key
94
def generate_unique_key():
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
95
    array = []
96
    for letter in range(97, 123):
97
        array.append(chr(letter))
98
    for letter in range(65, 91):
99
        array.append(chr(letter))
100
    for number in range(0, 10):
101
        array.append(number)
102
103
    random_values = random.sample(array, 5)
104
    random_values = map(lambda x: str(x), random_values)
0 ignored issues
show
Unused Code introduced by
This lambda might be unnecessary.
Loading history...
105
    return "".join(random_values)
106
107
108
@dataclass
0 ignored issues
show
Coding Style Naming introduced by
Class name "_Session_base" doesn't conform to PascalCase naming style ('[^\\W\\da-z][^\\W_]+$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
109
class _Session_base:
110
    """Session minimum informations."""
111
    state: str
112
    term: str
113
    date: str
114
    protocol_nr: str
115
116
117
@dataclass
0 ignored issues
show
Coding Style Naming introduced by
Class name "_Session_default" doesn't conform to PascalCase naming style ('[^\\W\\da-z][^\\W_]+$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
best-practice introduced by
Too many instance attributes (9/7)
Loading history...
118
class _Session_default:
119
    """Session additional informations."""
120
    page_from: str = field(default="unknown")
121
    page_to: str = field(default="unknown")
122
    expletive: str = field(default="unknown")
123
    kind: str = field(default="unknown")
124
    result: str = field(default="unknown")
125
    classification: str = field(default="unknown")
126
    tags: List[str] = field(
127
        default_factory=lambda: []
128
    )  # noqa
129
    region: str = field(default="unknown")
130
    speakers: List[str] = field(
131
        default_factory=lambda: []
132
    )  # noqa
133
134
135
@dataclass
136
class Session(_Session_default, _Session_base, AttrDisplay):
137
    """A session's details."""
138
139
140
@dataclass
0 ignored issues
show
Coding Style Naming introduced by
Class name "_Input_base" doesn't conform to PascalCase naming style ('[^\\W\\da-z][^\\W_]+$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
141
class _Input_base:
142
    """A member of parliament's contribution."""
143
    key: str
144