Passed
Push — main ( 787c7b...9701fa )
by Sat CFDI
05:28
created

satcfdi.models.date_period.DatePeriod.__le__()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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