GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Failed Conditions
Pull Request — master (#17)
by Yngve
04:14
created

paytext.paytext.PaymentText.generalize()   C

Complexity

Conditions 10

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 57
rs 5.9999
c 0
b 0
f 0
cc 10
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like paytext.paytext.PaymentText.generalize() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# pyre-strict
2
3
"""
4
This module helps you work with payment texts such as
5
"*4321 29.06 USD 50.00 ITUNES.COM/BILL Rate: 1.0000".
6
"""
7
from re import compile as compile_regex
8
from typing import List, Any
9
10
import iso4217parse
11
12
13
class PaymentText:
14
    """
15
    Use this class to represent your payment text.
16
    """
17
    text: str = ''
18
19
    def __init__(self, text: str) -> None:
20
        self.text = text
21
22
    def __repr__(self) -> str:
23
        return self.text
24
25
    def generalize(self) -> None:
26
        """
27
        Remove all parts of the strings that are specific to just this payment,
28
        in order to have a string that is the same across different payments of
29
        the same type (e.g. a monthly payment to Apple for iCloud storage).
30
31
        :return:
32
        """
33
        parts: List[str] = self.text.split()
34
35
        pattern = compile_regex(r'\*\d{4}')
36
37
        try:
38
            if pattern.match(parts[0]):
39
                del parts[0]
40
        except IndexError:
41
            return
42
43
        pattern = compile_regex(r'\d{2}\.\d{2}')
44
45
        if pattern.match(parts[0]):
46
            del parts[0]
47
48
        currency: Any = iso4217parse.by_alpha3(parts[0])
49
50
        if isinstance(currency, iso4217parse.Currency):
51
            del parts[0]
52
            del parts[0]
53
54
        pattern = compile_regex(r'\d{1}\.\d{4}')
55
56
        if pattern.match(parts[-1]):
57
            del parts[-1]
58
            del parts[-1]
59
60
        pattern = compile_regex(r'\d{2}\.\d{2}\.\d{2}')
61
62
        if pattern.match(parts[-1]):
63
            del parts[-1]
64
65
            pattern = compile_regex(r'.+:')
66
67
            if pattern.match(parts[-1]):
68
                del parts[-1]
69
70
        pattern = compile_regex(r'\d+,\d{2}')
71
72
        if pattern.match(parts[-1]):
73
            del parts[-1]
74
75
            currency: Any = iso4217parse.by_alpha3(parts[-1])
76
            if isinstance(currency, iso4217parse.Currency):
77
                del parts[-1]
78
79
        text = ' '.join(parts)
80
81
        self.text = text
82