From 73979df188ccedeaf2df874c77762f61df4b919c Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Thu, 11 Jan 2024 21:23:49 +0100 Subject: [PATCH 1/8] fix: do not consider months in `calculate_age` --- web_programming/get_top_billionaires.py | 1 - 1 file changed, 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 6f986acb9181..fb9bee2ee7f5 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -48,7 +48,6 @@ def calculate_age(unix_date: float) -> str: return str( TODAY.year - birthdate.year - - ((TODAY.month, TODAY.day) < (birthdate.month, birthdate.day)) ) From 45eb74058736b1c06875a70be03511d55f59888a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 11 Jan 2024 20:37:58 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/get_top_billionaires.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index fb9bee2ee7f5..1abcf6f801bc 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -45,10 +45,7 @@ def calculate_age(unix_date: float) -> str: ).date() else: birthdate = datetime.fromtimestamp(unix_date, tz=UTC).date() - return str( - TODAY.year - - birthdate.year - ) + return str(TODAY.year - birthdate.year) def get_forbes_real_time_billionaires() -> list[dict[str, str]]: From 299df36d0d6c56f8dc38b48e61bf6f4cec5d9808 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 12 Jan 2024 16:49:19 +0100 Subject: [PATCH 3/8] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 66 +++++++++++++------------ 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 1abcf6f801bc..fdc45abcfc07 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -3,7 +3,7 @@ This works for some of us but fails for others. """ -from datetime import UTC, datetime, timedelta +from datetime import date, datetime import requests from rich import box @@ -11,8 +11,7 @@ from rich import table as rich_table LIMIT = 10 -TODAY = datetime.now() - +TODAY = datetime.utc_now() API_URL = ( "https://www.forbes.com/forbesapi/person/rtb/0/position/true.json" "?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth" @@ -20,36 +19,40 @@ ) -def calculate_age(unix_date: float) -> str: - """Calculates age from given unix time format. +def years_old(birth_timestamp: int, today: date | None = None) -> int: + """ + Calculate the age in years based on the given birth date. Only the year, month, + and day are used in the calculation. The time of day is ignored. + + Args: + birth_timestamp: The date of birth. + today: (useful for writing tests) or if None then datetime.date.today(). Returns: - Age as string - - >>> from datetime import datetime, UTC - >>> years_since_create = datetime.now(tz=UTC).year - 2022 - >>> int(calculate_age(-657244800000)) - years_since_create - 73 - >>> int(calculate_age(46915200000)) - years_since_create - 51 + int: The age in years. + + Examples: + >>> today = date(2024, 1, 12) + >>> years_old(birth_timestamp=datetime(1959, 11, 20).timestamp(), today=today) + 64 + >>> years_old(birth_timestamp=datetime(1970, 2, 13).timestamp(), today=today) + 53 + >>> all( + ... years_old(datetime(today.year - i, 1, 12).timestamp(), today=today) == i + ... for i in range(1, 111) + ... ) + True """ - # Convert date from milliseconds to seconds - unix_date /= 1000 - - if unix_date < 0: - # Handle timestamp before epoch - epoch = datetime.fromtimestamp(0, tz=UTC) - seconds_since_epoch = (datetime.now(tz=UTC) - epoch).seconds - birthdate = ( - epoch - timedelta(seconds=abs(unix_date) - seconds_since_epoch) - ).date() - else: - birthdate = datetime.fromtimestamp(unix_date, tz=UTC).date() - return str(TODAY.year - birthdate.year) + today = today or date.today() + birth_date = date.fromtimestamp(birth_timestamp) + return (today.year - birth_date.year) - ( + (today.month, today.day) < (birth_date.month, birth_date.day) + ) def get_forbes_real_time_billionaires() -> list[dict[str, str]]: - """Get top 10 realtime billionaires using forbes API. + """ + Get the top 10 real-time billionaires using Forbes API. Returns: List of top 10 realtime billionaires data. @@ -62,21 +65,22 @@ def get_forbes_real_time_billionaires() -> list[dict[str, str]]: "Country": person["countryOfCitizenship"], "Gender": person["gender"], "Worth ($)": f"{person['finalWorth'] / 1000:.1f} Billion", - "Age": calculate_age(person["birthDate"]), + "Age": years_old(person["birthDate"]), } for person in response_json["personList"]["personsLists"] ] def display_billionaires(forbes_billionaires: list[dict[str, str]]) -> None: - """Display Forbes real time billionaires in a rich table. + """ + Display Forbes real-time billionaires in a rich table. Args: - forbes_billionaires (list): Forbes top 10 real time billionaires + forbes_billionaires (list): Forbes top 10 real-time billionaires """ table = rich_table.Table( - title=f"Forbes Top {LIMIT} Real Time Billionaires at {TODAY:%Y-%m-%d %H:%M}", + title=f"Forbes Top {LIMIT} Real-Time Billionaires at {TODAY:%Y-%m-%d %H:%M}", style="green", highlight=True, box=box.SQUARE, From 504dcae2a0db3073495a1bc37469ae2fc2420b30 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 12 Jan 2024 16:54:15 +0100 Subject: [PATCH 4/8] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index fdc45abcfc07..ce180b16a76e 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -3,7 +3,7 @@ This works for some of us but fails for others. """ -from datetime import date, datetime +from datetime import date, datetime, UTC import requests from rich import box @@ -43,8 +43,8 @@ def years_old(birth_timestamp: int, today: date | None = None) -> int: ... ) True """ - today = today or date.today() - birth_date = date.fromtimestamp(birth_timestamp) + today = today or TODAY.date() + birth_date = datetime.fromtimestamp(birth_timestamp, tz=UTC).date() return (today.year - birth_date.year) - ( (today.month, today.day) < (birth_date.month, birth_date.day) ) From a26d749ccac0da8badfef0ea7746ddcbc54f5c84 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 12 Jan 2024 16:59:23 +0100 Subject: [PATCH 5/8] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index ce180b16a76e..56d64f909dd2 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -3,7 +3,7 @@ This works for some of us but fails for others. """ -from datetime import date, datetime, UTC +from datetime import UTC, date, datetime import requests from rich import box @@ -50,7 +50,7 @@ def years_old(birth_timestamp: int, today: date | None = None) -> int: ) -def get_forbes_real_time_billionaires() -> list[dict[str, str]]: +def get_forbes_real_time_billionaires() -> list[dict[str, int | str]]: """ Get the top 10 real-time billionaires using Forbes API. @@ -71,7 +71,7 @@ def get_forbes_real_time_billionaires() -> list[dict[str, str]]: ] -def display_billionaires(forbes_billionaires: list[dict[str, str]]) -> None: +def display_billionaires(forbes_billionaires: list[dict[str, int | str]]) -> None: """ Display Forbes real-time billionaires in a rich table. From 0c0b057be4cc625d7919e171b4ef1723e8b6579c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 12 Jan 2024 17:04:08 +0100 Subject: [PATCH 6/8] TODAY = datetime.utcnow() --- web_programming/get_top_billionaires.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index 56d64f909dd2..a50a4bdd8b72 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -11,7 +11,7 @@ from rich import table as rich_table LIMIT = 10 -TODAY = datetime.utc_now() +TODAY = datetime.utcnow() API_URL = ( "https://www.forbes.com/forbesapi/person/rtb/0/position/true.json" "?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth" From e185f55cb90170064bd3d9890a02b57ffeb2d8e8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 12 Jan 2024 17:06:02 +0100 Subject: [PATCH 7/8] Update get_top_billionaires.py --- web_programming/get_top_billionaires.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/get_top_billionaires.py b/web_programming/get_top_billionaires.py index a50a4bdd8b72..703b635eef82 100644 --- a/web_programming/get_top_billionaires.py +++ b/web_programming/get_top_billionaires.py @@ -11,7 +11,7 @@ from rich import table as rich_table LIMIT = 10 -TODAY = datetime.utcnow() +TODAY = datetime.now(tz=UTC) API_URL = ( "https://www.forbes.com/forbesapi/person/rtb/0/position/true.json" "?fields=personName,gender,source,countryOfCitizenship,birthDate,finalWorth" From e4b873cd4b55552aed24da34a15855db3d8fba5b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 12 Jan 2024 17:09:51 +0100 Subject: [PATCH 8/8] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 60c1d6d119d0..1631feb2ba06 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: actions/setup-python@v4 + - uses: actions/setup-python@v5 with: python-version: 3.12 allow-prereleases: true