-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move all draw and input helper functions into a separate utils directory
- Loading branch information
Showing
3 changed files
with
91 additions
and
87 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import constants | ||
from PIL import Image | ||
from PIL import ImageDraw | ||
|
||
|
||
def draw_text(img: Image) -> None: | ||
""" | ||
Draw a tagline on the bottom of the generated image. | ||
""" | ||
text = 'This is your life on a single sheet of paper.' | ||
draw = ImageDraw.Draw(img) | ||
padding_left = 226 # The almost exact estimated half of pixel width of the current default tagline | ||
padding_bottom = 40 | ||
draw.text(((img.size[0] / 2) - padding_left, img.size[1] - padding_bottom), | ||
text, constants.dark_grey, font=constants.font) | ||
|
||
|
||
def draw_units_number(img: Image, units: int, unit_type: str) -> None: | ||
""" | ||
Draw the life expectancy in specified units in the right corner of the generated image. | ||
""" | ||
text = str(units) + ' ' + unit_type | ||
draw = ImageDraw.Draw(img) | ||
padding_left = 170 | ||
padding_bottom = 40 | ||
draw.text((img.size[0] - padding_left, img.size[1] - padding_bottom), | ||
text, constants.dark_grey, font=constants.font) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from dateutil.parser import parse | ||
from pycountry import countries | ||
from scraper import scrape_life_expectancy | ||
from typing import Tuple | ||
|
||
|
||
# Validators | ||
valid_f_sex = ['female', 'f', 'F', 'fem', 'she', 'woman'] | ||
valid_m_sex = ['male', 'm', 'M', 'man', 'he'] | ||
|
||
|
||
def input_dob() -> Tuple[int, int, int]: | ||
""" | ||
Input and validate date of birth | ||
""" | ||
dob = input('Enter your date of birth (YYYY-MM-DD):\n') | ||
dob = parse(dob) | ||
return dob.year, dob.month, dob.day | ||
|
||
|
||
def input_sex() -> bool: | ||
""" | ||
Input and validate biological sex | ||
""" | ||
while True: | ||
sex = input('Enter your biological sex (m/f):\n') | ||
if sex in valid_f_sex: | ||
return True | ||
elif sex in valid_m_sex: | ||
return False | ||
else: | ||
print('Please enter a valid biological sex.') | ||
|
||
|
||
def input_country() -> str: | ||
""" | ||
Input a valid country or a 2-letter country alias (e.g., "Germany" = "DE") | ||
""" | ||
while True: | ||
country = input('Enter your country (full name or 2-letter alias):\n') | ||
country_names = [country.name for country in countries] | ||
country_codes = [country.alpha_2 for country in countries] | ||
|
||
if country.capitalize() in country_names: | ||
return countries.get(name=country.capitalize()).alpha_3 | ||
elif country.upper() in country_codes: | ||
return countries.get(alpha_2=country.upper()).alpha_3 | ||
else: | ||
print('Please enter a valid country name.') | ||
|
||
|
||
def input_all(): | ||
""" | ||
Input all the needed arguments for the calendar. | ||
""" | ||
dob = input_dob() | ||
sex = input_sex() | ||
country = input_country() | ||
country_index = scrape_life_expectancy(country) | ||
|
||
return sex, country_index, dob |