Test Failed
Push — master ( 6b0277...975f37 )
by Oliver
02:07
created

personroles.resources.helpers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 39
dl 0
loc 100
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Party.__post_init__() 0 3 2
A AttrDisplay.__str__() 0 14 1
A AttrDisplay.gather_attrs() 0 13 4
A TooManyFirstNames.__init__() 0 4 1
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
"""
4
Some helper functions
5
"""
6
from dataclasses import dataclass, field
7
8
from .constants import GERMAN_PARTIES  # type: ignore  # noqa
9
10
11
class NotInRange(Exception):
12
13
    """
14
    Until term 13 the available PDF files are scanned in and need OCR to be
15
    scraped. Therefore only terms 14 to currently term 17 are accepted.
16
    """
17
18
    pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
19
20
21
class NotGermanParty(Exception):
22
23
    """
24
    Since this project focusses on Germany the current range of parties that
25
    will be accepted is reduced to German parties.
26
    """
27
28
    pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
29
30
31
class TooManyFirstNames(Exception):
32
33
    """
34
    Currently only one first name and two middle names are supported.
35
    Example: Tom H. Paul last_name
36
    """
37
38
    def __init__(self, message):
39
        """use like: raise TooManyFirstNames ("message")"""
40
        Exception.__init__(self)
41
        print(message)
42
43
44
class AttrDisplay:
45
46
    """
47
    Mark Lutz, Programming Python
48
    Provides an inheritable display overload method that shows instances
49
    with their class names and a name=value pair for each attribute stored
50
    on the instance itself (but not attrs inherited from its classes). Can
51
    be mixed into any class, and will work on any instance.
52
    """
53
54
    def gather_attrs(self) -> list:
55
        """
56
        Check if attributes have content and add them to a list called attrs.
57
        """
58
        attrs = []
59
        for key in sorted(self.__dict__):
60
            if self.__dict__[key] and self.__dict__[key] not in [
61
                "unknown",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
62
                "ew",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
63
                None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
64
            ]:
65
                attrs.append(f"{key}={getattr(self, key)}")
66
        return attrs
67
68
    def __str__(self) -> str:
69
        """
70
        Instances will printed like this:
71
            class name
72
            attr1=value1
73
            attr2=value2
74
            ...
75
        """
76
        comp_repr = (
77
            f"{self.__class__.__name__}:\n"
78
            + "\n".join(str(attr) for attr in self.gather_attrs())
79
            + "\n"
80
        )
81
        return comp_repr
82
83
84
@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...
85
class _Party_base:
86
    party_name: str  # type: ignore  # noqa
87
88
89
@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...
90
class _Party_default:
91
    party_entry: str = field(default="unknown")
92
    party_exit: str = field(default="unknown")
93
94
95
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
96
class Party(_Party_default, _Party_base, AttrDisplay):
97
    def __post_init__(self):
98
        if self.party_name not in GERMAN_PARTIES:
99
            raise NotGermanParty
100