Skip to content

Commit

Permalink
Merge pull request #752 from Chloe23077/feature-branch
Browse files Browse the repository at this point in the history
Calculate Age Project: Modularize Code and Fix Input Handling
  • Loading branch information
Mrinank-Bhowmick authored Jun 7, 2024
2 parents 51050b2 + 593c7d2 commit 9d1bd8f
Show file tree
Hide file tree
Showing 22 changed files with 646 additions and 299 deletions.
Empty file.
38 changes: 38 additions & 0 deletions projects/Calculate Age/src/calculate_age.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import time
from utilize_date import judge_leap_year, month_days


def age_calculator(name, age):
"""
Calculate user's age in years, month, and days
based on current date and print.
Args:
name (str): user's name.
age (int): user's age in years.
Returns:
None.
"""
localtime = time.localtime(time.time())

year = int(age)
month = year * 12 + localtime.tm_mon
day = 0

begin_year = int(localtime.tm_year) - year
end_year = begin_year + year

for y in range(begin_year, end_year):
if judge_leap_year(y):
day += 366
else:
day += 365

leap_year = judge_leap_year(localtime.tm_year)
for m in range(1, localtime.tm_mon):
day += month_days(m, leap_year)

day += localtime.tm_mday

return f"{name}'s age is {year} years or {month} months or {day} days"
34 changes: 34 additions & 0 deletions projects/Calculate Age/src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from calculate_age import age_calculator


def main():
"""
the user to input name and age, validate input,
and calculates and displays the user age in years, months and days.
Args:
None.
Return:
None.
"""
input_name = input("input your name: ")

while True:
input_age = input("input your age: ")
try:
string_to_int_age = int(input_age)
if string_to_int_age <= 0:
print("Please input a positive number.")
else:
break
except ValueError:
print("Please input a valid age.")

result = age_calculator(input_name, string_to_int_age)

print(result)


if __name__ == "__main__":
main()
36 changes: 36 additions & 0 deletions projects/Calculate Age/src/utilize_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from calendar import isleap


def judge_leap_year(year):
"""
judge the leap year.
Args:
year (int): To check the year.
return:
bool: Ture if the year is a leap year, False otherwise.
"""
if isleap(year):
return True
else:
return False


def month_days(month, leap_year):
"""
Returns the number of days in each month
Args:
month (int): The month 1-12.
Returns:
int: The number of days in the month.
"""
if month in [1, 3, 5, 7, 8, 10, 12]:
return 31
elif month in [4, 6, 9, 11]:
return 30
elif month == 2 and leap_year:
return 29
elif month == 2 and (not leap_year):
return 28
Empty file.
40 changes: 40 additions & 0 deletions projects/Calculate Age/tests/test_calculate_age.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest.mock import patch
from calculate_age import age_calculator


@patch("time.time", return_value=1621848668.0)
def test_age_calculator(mock_time):
"""
Test the age_calculator function
Mocks the current time and check if the age is calculated
based on current time
"""
name = "Chloe"
age = 30
expect_output = "Chloe's age is 30 years or 365 months or 11102 days"
assert age_calculator(name, age) == expect_output


def test_age_calculator_negative_age():
"""
Tests the age_calculator function for negative age input
Check for ValueError when user input negative age.
"""
name = "Emma"
age = -5
try:
age_calculator(name, age)
except ValueError as e:
assert str(e) == "Please input a positive number."


@patch("time.time", return_value=1621848668.0)
def test_age_calculator_leap_year(mock_time):
"""
Test the age_calculator function considering leap years
Check for expect_output_leap_year comparing the real output considering leap years
"""
name = "David"
age = 30
expect_output_leap_year = "David's age is 30 years or 365 months or 11102 days"
assert age_calculator(name, age) == expect_output_leap_year
33 changes: 33 additions & 0 deletions projects/Calculate Age/tests/test_utilize_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from utilize_date import judge_leap_year, month_days


def test_judge_leap_year():
"""
judge_leap_year function tests whether a given year is a leap year.
A leap year is divisible by 4 and, if divisible by 100, must also be divisible by 400.
"""
assert judge_leap_year(2000) == True
assert judge_leap_year(2008) == True
assert judge_leap_year(2023) == False
assert judge_leap_year(1900) == False
assert judge_leap_year(2400) == True
assert judge_leap_year(2100) == False


def test_month_days():
"""
The month_days function tests whether if returns the correct number of days for a given month
For Feb, both leap years and common years are considered.
"""
assert month_days(7, False) == 31
assert month_days(4, True) == 30
assert month_days(2, True) == 29
assert month_days(2, False) == 28
assert month_days(1, False) == 31
assert month_days(11, True) == 30


# "pytest -s test_utilize_date.py" to test this file
47 changes: 22 additions & 25 deletions projects/Chat-GPT-Discord-Bot/Chat_GPT_Function.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,44 @@

gpt_api_key = os.getenv("GPT_API_KEY")


def gpt(model: str, prompt: str, sys_prompt: str, temp: float):
client = OpenAI(api_key= gpt_api_key)
client = OpenAI(api_key=gpt_api_key)
response = client.chat.completions.create(
model = model,
model=model,
messages=[
{
"role": "system",
"content": sys_prompt
},
{
"role": "user",
"content": prompt
}
{"role": "system", "content": sys_prompt},
{"role": "user", "content": prompt},
],
temperature = temp,
temperature=temp,
# max_tokens=64,
top_p=1
top_p=1,
)
output = response.choices[0].message.content.strip()
return output


def dalle3(prompt: str, quality: str, size: str, style: str):
client = OpenAI(api_key= gpt_api_key)
client = OpenAI(api_key=gpt_api_key)
response = client.images.generate(
model = "dall-e-3",
prompt = prompt,
size = size,
quality = quality,
style = style,
model="dall-e-3",
prompt=prompt,
size=size,
quality=quality,
style=style,
n=1,
)
)
image_url = response.data[0].url
return image_url


def dalle2(prompt: str, size: str):
client = OpenAI(api_key= gpt_api_key)
client = OpenAI(api_key=gpt_api_key)
response = client.images.generate(
model = "dall-e-2",
prompt = prompt,
size = size,
model="dall-e-2",
prompt=prompt,
size=size,
n=1,
)
)
image_url = response.data[0].url
return image_url
return image_url
Loading

0 comments on commit 9d1bd8f

Please sign in to comment.