Skip to content

Commit 1348358

Browse files
committed
add seasons
1 parent b71c67a commit 1348358

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

week-8/seasons/seasons.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import re
2+
import sys
3+
from datetime import date
4+
from inflect import engine
5+
6+
7+
class HumanLiveTime:
8+
9+
def __init__(self, date):
10+
self.date = date
11+
12+
def calculate(self, date: date):
13+
self.total_minute = self.get_total_minute(date)
14+
return self.get_total_minute_p(self.total_minute)
15+
16+
def get_total_minute(self, date: date):
17+
return round((date.today() - date).total_seconds() / 60)
18+
19+
def get_total_minute_p(self, date):
20+
e = engine()
21+
return (e.number_to_words(date, andword="").capitalize() + " minutes")
22+
23+
24+
def main():
25+
human = HumanLiveTime()
26+
birth_date = input("Date of Birth: ").strip()
27+
28+
if not (birth_date := is_valid_birth_date(birth_date)):
29+
sys.exit("Invalid date")
30+
31+
print(human.calculate(birth_date))
32+
33+
34+
def is_valid_birth_date(birth_date: str) -> bool:
35+
"""
36+
Validates a birth date.
37+
38+
Args:
39+
birth_date: The birth date in the format YYYY-MM-DD.
40+
41+
Returns:
42+
True if the birth date is valid, False otherwise.
43+
44+
Raises:
45+
ValueError if the birth date is not in the correct format.
46+
"""
47+
48+
try:
49+
return date.fromisoformat(birth_date)
50+
except ValueError:
51+
return False
52+
53+
# with regex
54+
# regex = r"^\d{4}-\d{2}-\d{2}$"
55+
56+
# with out regex
57+
# dates = x.split("-")
58+
# if len(dates) != 3:
59+
# sys.exit("Invalid date")
60+
61+
# for each in dates:
62+
# if not each.isdigit():
63+
# sys.exit("Invalid date")
64+
65+
# try:
66+
# return date(*[int(each) for each in dates])
67+
# except Exception as e:
68+
# sys.exit("Invalid date")
69+
70+
71+
if __name__ == "__main__":
72+
main()

week-8/seasons/test_seasons.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
import datetime
3+
from seasons import HumanLiveTime, is_valid_birth_date
4+
5+
6+
7+
8+
def test_invalid_date():
9+
assert is_valid_birth_date("1954 21") == False
10+
assert is_valid_birth_date("Sep 10,, 1990") == False
11+
assert is_valid_birth_date(" 190.10.10") == False
12+
assert is_valid_birth_date(" 1a0-0-10") == False
13+
assert is_valid_birth_date("1asd0-1-1") == False
14+
15+
16+
17+
def test_valid_date():
18+
assert is_valid_birth_date("2000-12-12") != False
19+
assert is_valid_birth_date("1900-01-08") != False
20+
assert is_valid_birth_date("2000-01-30") != False
21+
22+
23+
def test_ok():
24+
date_valid = is_valid_birth_date("1999-01-01")
25+
date=datetime.date(2000,1,1)
26+
human = HumanLiveTime(date)
27+
28+
assert human.calculate(date_valid) == "Five hundred twenty-five thousand, six hundred minutes"
29+

0 commit comments

Comments
 (0)