satcfdi.models.date_period   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 92.45%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 77
ccs 49
cts 53
cp 0.9245
rs 9.92
c 0
b 0
f 0
wmc 31

10 Methods

Rating   Name   Duplication   Size   Complexity  
A DatePeriod.__le__() 0 5 2
A DatePeriod.__str__() 0 7 3
A DatePeriod.__ge__() 0 5 2
A DatePeriod.__lt__() 0 8 5
A DatePeriod.__gt__() 0 8 5
A DatePeriod.__init__() 0 8 5
A DatePeriod.__eq__() 0 8 5
A DatePeriod.__ne__() 0 5 2
A DatePeriod.__hash__() 0 2 1
A DatePeriod.strftime() 0 5 1
1 1
from datetime import date
2
3
4 1
class DatePeriod:
5
    # A class from defining a data range to then do date comparisons
6
7 1
    def __init__(self, year: int | None, month: int = None, day: int = None):
8 1
        self.year = year
9 1
        if year is None and month is not None:
10
            raise ValueError("Month can't have value if Year doesn't")
11 1
        self.month = month
12 1
        if month is None and day is not None:
13
            raise ValueError("Day can't have value if Month doesn't")
14 1
        self.day = day
15
16 1
    def __eq__(self, other):
17 1
        if isinstance(other, date | DatePeriod):
18 1
            for s, o in ((self.year, other.year), (self.month, other.month), (self.day, other.day)):
19 1
                if s is not None and s != o:
20 1
                    return False
21 1
            return True
22
23 1
        return NotImplemented
24
25 1
    def __ne__(self, other):
26 1
        if isinstance(other, date | DatePeriod):
27 1
            return not self.__eq__(other)
28
29 1
        return NotImplemented
30
31 1
    def __gt__(self, other):
32 1
        if isinstance(other, date | DatePeriod):
33 1
            for s, o in ((self.year, other.year), (self.month, other.month), (self.day, other.day)):
34 1
                if s is not None and s != o:
35 1
                    return s > o
36 1
            return False
37
38 1
        return NotImplemented
39
40 1
    def __ge__(self, other):
41 1
        if isinstance(other, date | DatePeriod):
42 1
            return self > other or self == other
43
44 1
        return NotImplemented
45
46 1
    def __lt__(self, other):
47 1
        if isinstance(other, date | DatePeriod):
48 1
            for s, o in ((self.year, other.year), (self.month, other.month), (self.day, other.day)):
49 1
                if s is not None and s != o:
50 1
                    return s < o
51 1
            return False
52
53 1
        return NotImplemented
54
55 1
    def __le__(self, other):
56 1
        if isinstance(other, date | DatePeriod):
57 1
            return self < other or self == other
58
59 1
        return NotImplemented
60
61 1
    def __str__(self):
62 1
        res = ""
63 1
        for n, f in ((self.year, "{:04d}"), (self.month, "-{:02d}"), (self.day, "-{:02d}")):
64 1
            if n:
65 1
                res += f.format(n)
66
67 1
        return res
68
69 1
    def __hash__(self):
70
        return (self.year or 0) * 1000 + (self.month or 0) * 100 + (self.day or 0)
71
72 1
    def strftime(self, fmt):
73
        return (fmt
74
                .replace("%Y", str(self.year))
75
                .replace("%m", str(self.month))
76
                .replace("%d", str(self.day))
77
                )
78