We stared with the file bithday-ugly.py
and the refactored version is in bithday-refactored.py
with the two extra modules we created after it.
Last active
February 28, 2025 15:54
-
-
Save erikw/a24d46893148702783043ac8a698ee44 to your computer and use it in GitHub Desktop.
[CS50x Python Aryaloka] Workshop: Refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def message_header(): | |
return "\nππππππ Here is some birthday stats for you. ππππππ" | |
def message_for_year(year): | |
if year <= 1950: | |
return "π΄ Wow, you are old now!" | |
else: | |
return "πΆ You are still young, enjoy!" | |
def message_for_month(month): | |
msg = f"Oh, you are born in month number {month}: " | |
match month: | |
case 1: | |
msg += "π January, known as the Long Nights." | |
case 2: | |
msg += "π February, known as the Snow Melt." | |
case 3: | |
msg += "π March, known as the New Blossom." | |
case 4: | |
msg += "π April, known as the Old Blossom." | |
case 5: | |
msg += "π May, known as the Kord's Tempest." | |
case 6: | |
msg += "π June, known as the Fresh Hay." | |
case 7: | |
msg += "π July, known as the Pelor's Blessing." | |
case 8: | |
msg += "π August, known as the Golden Harvest." | |
case 9: | |
msg += "π September, known as the Pelor's Bounty." | |
case 10: | |
msg += "π October, known as the Leaf Fall." | |
case 11: | |
msg += "π November, known as the Sehanine." | |
case 12: | |
msg += "π December, known as the Dark Frost." | |
return msg | |
def message_for_day(day): | |
msg = "Your bith-day is " | |
if day % 2 == 0: | |
msg += "even" | |
else: | |
msg += "odd" | |
msg += "." | |
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from date_validation2 import validate_date | |
from birthday_messages import message_header, message_for_year, message_for_month, message_for_day | |
QUESTION ="Hello dear user, please tell me what year, month and day were you born [yyyy-mm-dd] in. Then I will tell you something about it. Input: " | |
def get_input(): | |
while True: | |
try: | |
date = input(QUESTION) | |
year, month, day = date.split("-") | |
year = int(year) | |
month = int(month) | |
day = int(day) | |
break | |
except ValueError as e: | |
print("Try again. Input in format yyyy-mm-dd", file=sys.stderr) | |
return [year, month, day] | |
def main(): | |
year, month, day = get_input() | |
validate_date(year, month, day) | |
print(message_header()) | |
print(message_for_year(year)) | |
print(message_for_month(month)) | |
print(message_for_day(day)) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
while True: | |
try: | |
date = input("Hello dear user, please tell me what year, month and day were you born [yyyy-mm-dd] in. Then I will tell you something about it. Input: ") | |
year, month, day = date.split("-") | |
year = int(year) | |
month = int(month) | |
day = int(day) | |
break | |
except ValueError as e: | |
print("Try again. Input in format yyyy-mm-dd", file=sys.stderr) | |
if not (0 <= year <= 9999): | |
raise ValueError("Invalid year range") | |
if not (1 <= month <= 12): | |
raise ValueError("Invalid month range") | |
if not (1 <= day <= 31): | |
raise ValueError("Invalid day range") | |
if year <= 1950: | |
print("π΄ Wow, you are old now!") | |
else: | |
print("πΆ You are still young, enjoy!") | |
print() | |
print("ππππππ Here is some birthday stats for you. ππππππ") | |
print(f"Oh, you are born in month number {month}: ", end="") | |
match month: | |
case 1: | |
print("π January, known as the Long Nights.") | |
case 2: | |
print("π February, known as the Snow Melt.") | |
case 3: | |
print("π March, known as the New Blossom.") | |
case 4: | |
print("π April, known as the Old Blossom.") | |
case 5: | |
print("π May, known as the Kord's Tempest.") | |
case 6: | |
print("π June, known as the Fresh Hay.") | |
case 7: | |
print("π July, known as the Pelor's Blessing.") | |
case 8: | |
print("π August, known as the Golden Harvest.") | |
case 9: | |
print("π September, known as the Pelor's Bounty.") | |
case 10: | |
print("π October, known as the Leaf Fall.") | |
case 11: | |
print("π November, known as the Sehanine.") | |
case 12: | |
print("π December, known as the Dark Frost.") | |
if day % 2 == 0: | |
print("Your bith-day is even.") | |
else: | |
print("Your bith-day is odd.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate_year(year): | |
if not (0 <= year <= 9999): | |
raise ValueError("Invalid year range") | |
def validate_month(month): | |
if not (1 <= month <= 12): | |
raise ValueError("Invalid month range") | |
def validate_day(day): | |
if not (1 <= day <= 31): | |
raise ValueError("Invalid day range") | |
def validate_date(year, month, day): | |
validate_year(year) | |
validate_month(month) | |
validate_day(day) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment