Issues (105)

personroles/resources/helpers.py (11 issues)

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
Wrong hanging indentation before block (add 4 spaces).
Loading history...
49
                "ew",
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
50
                None,
0 ignored issues
show
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
Missing function or method docstring
Loading history...
95
    lst = list()
96
    for letter in range(97, 123):
97
        lst.append(chr(letter))
98
    for letter in range(65, 91):
99
        lst.append(chr(letter))
100
    for number in range(1, 10):
101
        lst.append(number)
102
103
    random_values = random.sample(lst, 5)
104
    print(random_values)
105
    random_values = map(lambda x: str(x), random_values)
0 ignored issues
show
This lambda might be unnecessary.
Loading history...
106
    return "".join(random_values)
107
108
109
@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...
110
class _Session_base:
111
    """Session minimum informations."""
112
    state: str
113
    term: str
114
    date: str
115
    protocol_nr: str
116
117
118
@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...
Too many instance attributes (9/7)
Loading history...
119
class _Session_default:
120
    """Session additional informations."""
121
    page_from: str = field(default="unknown")
122
    page_to: str = field(default="unknown")
123
    expletive: str = field(default="unknown")
124
    kind: str = field(default="unknown")
125
    result: str = field(default="unknown")
126
    classification: str = field(default="unknown")
127
    tags: List[str] = field(
128
        default_factory=lambda: []
129
    )  # noqa
130
    region: str = field(default="unknown")
131
    speakers: List[str] = field(
132
        default_factory=lambda: []
133
    )  # noqa
134
135
136
@dataclass
137
class Session(_Session_default, _Session_base, AttrDisplay):
138
    """A session's details."""
139
140
141
@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...
142
class _Input_base:
143
    """A member of parliament's contribution."""
144
    key: str
145