Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 9d1bd8f

Browse files
Merge pull request #752 from Chloe23077/feature-branch
Calculate Age Project: Modularize Code and Fix Input Handling
2 parents 51050b2 + 593c7d2 commit 9d1bd8f

File tree

22 files changed

+646
-299
lines changed

22 files changed

+646
-299
lines changed

projects/Calculate Age/src/__init__.py

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import time
2+
from utilize_date import judge_leap_year, month_days
3+
4+
5+
def age_calculator(name, age):
6+
"""
7+
Calculate user's age in years, month, and days
8+
based on current date and print.
9+
10+
Args:
11+
name (str): user's name.
12+
age (int): user's age in years.
13+
14+
Returns:
15+
None.
16+
"""
17+
localtime = time.localtime(time.time())
18+
19+
year = int(age)
20+
month = year * 12 + localtime.tm_mon
21+
day = 0
22+
23+
begin_year = int(localtime.tm_year) - year
24+
end_year = begin_year + year
25+
26+
for y in range(begin_year, end_year):
27+
if judge_leap_year(y):
28+
day += 366
29+
else:
30+
day += 365
31+
32+
leap_year = judge_leap_year(localtime.tm_year)
33+
for m in range(1, localtime.tm_mon):
34+
day += month_days(m, leap_year)
35+
36+
day += localtime.tm_mday
37+
38+
return f"{name}'s age is {year} years or {month} months or {day} days"

projects/Calculate Age/src/main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from calculate_age import age_calculator
2+
3+
4+
def main():
5+
"""
6+
the user to input name and age, validate input,
7+
and calculates and displays the user age in years, months and days.
8+
9+
Args:
10+
None.
11+
12+
Return:
13+
None.
14+
"""
15+
input_name = input("input your name: ")
16+
17+
while True:
18+
input_age = input("input your age: ")
19+
try:
20+
string_to_int_age = int(input_age)
21+
if string_to_int_age <= 0:
22+
print("Please input a positive number.")
23+
else:
24+
break
25+
except ValueError:
26+
print("Please input a valid age.")
27+
28+
result = age_calculator(input_name, string_to_int_age)
29+
30+
print(result)
31+
32+
33+
if __name__ == "__main__":
34+
main()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from calendar import isleap
2+
3+
4+
def judge_leap_year(year):
5+
"""
6+
judge the leap year.
7+
8+
Args:
9+
year (int): To check the year.
10+
return:
11+
bool: Ture if the year is a leap year, False otherwise.
12+
"""
13+
if isleap(year):
14+
return True
15+
else:
16+
return False
17+
18+
19+
def month_days(month, leap_year):
20+
"""
21+
Returns the number of days in each month
22+
23+
Args:
24+
month (int): The month 1-12.
25+
26+
Returns:
27+
int: The number of days in the month.
28+
"""
29+
if month in [1, 3, 5, 7, 8, 10, 12]:
30+
return 31
31+
elif month in [4, 6, 9, 11]:
32+
return 30
33+
elif month == 2 and leap_year:
34+
return 29
35+
elif month == 2 and (not leap_year):
36+
return 28

projects/Calculate Age/tests/__init__.py

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from unittest.mock import patch
2+
from calculate_age import age_calculator
3+
4+
5+
@patch("time.time", return_value=1621848668.0)
6+
def test_age_calculator(mock_time):
7+
"""
8+
Test the age_calculator function
9+
Mocks the current time and check if the age is calculated
10+
based on current time
11+
"""
12+
name = "Chloe"
13+
age = 30
14+
expect_output = "Chloe's age is 30 years or 365 months or 11102 days"
15+
assert age_calculator(name, age) == expect_output
16+
17+
18+
def test_age_calculator_negative_age():
19+
"""
20+
Tests the age_calculator function for negative age input
21+
Check for ValueError when user input negative age.
22+
"""
23+
name = "Emma"
24+
age = -5
25+
try:
26+
age_calculator(name, age)
27+
except ValueError as e:
28+
assert str(e) == "Please input a positive number."
29+
30+
31+
@patch("time.time", return_value=1621848668.0)
32+
def test_age_calculator_leap_year(mock_time):
33+
"""
34+
Test the age_calculator function considering leap years
35+
Check for expect_output_leap_year comparing the real output considering leap years
36+
"""
37+
name = "David"
38+
age = 30
39+
expect_output_leap_year = "David's age is 30 years or 365 months or 11102 days"
40+
assert age_calculator(name, age) == expect_output_leap_year
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from utilize_date import judge_leap_year, month_days
3+
4+
5+
def test_judge_leap_year():
6+
"""
7+
judge_leap_year function tests whether a given year is a leap year.
8+
9+
A leap year is divisible by 4 and, if divisible by 100, must also be divisible by 400.
10+
"""
11+
assert judge_leap_year(2000) == True
12+
assert judge_leap_year(2008) == True
13+
assert judge_leap_year(2023) == False
14+
assert judge_leap_year(1900) == False
15+
assert judge_leap_year(2400) == True
16+
assert judge_leap_year(2100) == False
17+
18+
19+
def test_month_days():
20+
"""
21+
The month_days function tests whether if returns the correct number of days for a given month
22+
23+
For Feb, both leap years and common years are considered.
24+
"""
25+
assert month_days(7, False) == 31
26+
assert month_days(4, True) == 30
27+
assert month_days(2, True) == 29
28+
assert month_days(2, False) == 28
29+
assert month_days(1, False) == 31
30+
assert month_days(11, True) == 30
31+
32+
33+
# "pytest -s test_utilize_date.py" to test this file

projects/Chat-GPT-Discord-Bot/Chat_GPT_Function.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,44 @@
66

77
gpt_api_key = os.getenv("GPT_API_KEY")
88

9+
910
def gpt(model: str, prompt: str, sys_prompt: str, temp: float):
10-
client = OpenAI(api_key= gpt_api_key)
11+
client = OpenAI(api_key=gpt_api_key)
1112
response = client.chat.completions.create(
12-
model = model,
13+
model=model,
1314
messages=[
14-
{
15-
"role": "system",
16-
"content": sys_prompt
17-
},
18-
{
19-
"role": "user",
20-
"content": prompt
21-
}
15+
{"role": "system", "content": sys_prompt},
16+
{"role": "user", "content": prompt},
2217
],
23-
temperature = temp,
18+
temperature=temp,
2419
# max_tokens=64,
25-
top_p=1
20+
top_p=1,
2621
)
2722
output = response.choices[0].message.content.strip()
2823
return output
2924

25+
3026
def dalle3(prompt: str, quality: str, size: str, style: str):
31-
client = OpenAI(api_key= gpt_api_key)
27+
client = OpenAI(api_key=gpt_api_key)
3228
response = client.images.generate(
33-
model = "dall-e-3",
34-
prompt = prompt,
35-
size = size,
36-
quality = quality,
37-
style = style,
29+
model="dall-e-3",
30+
prompt=prompt,
31+
size=size,
32+
quality=quality,
33+
style=style,
3834
n=1,
39-
)
35+
)
4036
image_url = response.data[0].url
4137
return image_url
4238

39+
4340
def dalle2(prompt: str, size: str):
44-
client = OpenAI(api_key= gpt_api_key)
41+
client = OpenAI(api_key=gpt_api_key)
4542
response = client.images.generate(
46-
model = "dall-e-2",
47-
prompt = prompt,
48-
size = size,
43+
model="dall-e-2",
44+
prompt=prompt,
45+
size=size,
4946
n=1,
50-
)
47+
)
5148
image_url = response.data[0].url
52-
return image_url
49+
return image_url

0 commit comments

Comments
 (0)