Skip to content

Commit

Permalink
add the number of weeks in the bottom right corner, change top paddin…
Browse files Browse the repository at this point in the history
…g for ease of attaching the printed sheet to the wall
  • Loading branch information
kronicka committed Jan 7, 2019
1 parent 38f5b7e commit 310daa2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
22 changes: 17 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# All the scraped life expectancy data belongs to The World Bank Group (http://www.worldbank.org/)
# The code is written by me, kronicka (https://github.com/kronicka) and is licensed under GNU 3.0
# The code is written by me, kronicka (https://github.com/kronicka), and is licensed under GNU 3.0

import constants
from datetime import date
Expand Down Expand Up @@ -83,8 +83,19 @@ def draw_text(img: Image) -> None:
font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 24)
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, (192, 192, 192), font=font)
# img.save('weeks.png')
draw.text(((img.size[0] / 2) - padding_left, img.size[1] - padding_bottom), text, constants.dark_grey, font=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)
font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 24)
padding_left = 170
padding_bottom = 40
draw.text((img.size[0] - padding_left, img.size[1] - padding_bottom), text, constants.dark_grey, font=font)


def calculate_weeks(sex: bool, country_index: int, dob: Tuple[int, int, int]) -> int:
Expand Down Expand Up @@ -115,7 +126,7 @@ def calculate_weeks(sex: bool, country_index: int, dob: Tuple[int, int, int]) ->
return int(weeks_left)


def generate_calendar(units: int, unit_type: str = None):
def generate_calendar(units: int, unit_type: str = 'weeks'):
"""
Generate a calendar based on the number of weeks
"""
Expand Down Expand Up @@ -144,10 +155,11 @@ def generate_calendar(units: int, unit_type: str = None):
cols *= leftover
cols = int(cols)
for col in range(0, cols):
box = (square.size[0] * col + padding, square_size[1] * row + padding)
box = (square.size[0] * col + padding, square_size[1] * row + padding * 2)
background.paste(square, box)

draw_text(background)
draw_units_number(background, units, unit_type)
# background.save('weeks.png')
background.show()

Expand Down
6 changes: 5 additions & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Constants (2016 WB Data)
# Constants
# Life Expectancy (2016 WB Data)
general_life_expectancy_days = 26097.5
general_life_expectancy_weeks = 3728.214 # ~71.5 years
female_life_expectancy_weeks = 3872.18071 # ~74.261 years
male_life_expectancy_weeks = 3647.54929 # ~69.953 years

# Text Draw Utilities
dark_grey = (192, 192, 192)
6 changes: 3 additions & 3 deletions scraper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Scraper of the The World Bank's most recent life expectancy stats
# All the scraped life expectancy data belongs to The World Bank Group (http://www.worldbank.org/)
# The code is written by me, kronicka (https://github.com/kronicka) and is licensed under GNU 3.0
# The code is written by me, kronicka (https://github.com/kronicka), and is licensed under GNU 3.0

from bs4 import BeautifulSoup
from pycountry import countries
Expand Down Expand Up @@ -35,13 +35,13 @@ def scrape_life_expectancy(country: str) -> int:
if life_expectancy_years == '..':
life_expectancy_years = 0
except requests.exceptions.ConnectionError:
print('Couldn\'t reach the World Bank server, pal, fix your connection for the most recent data.')
print('Couldn\'t reach the World Bank server, pal. Fix your connection for the most recent data.')
print('Using locally stored 2016 data for countries instead.')

with open('countries.txt', 'r') as countries_file:
countries_dict = json.loads(countries_file.read())
life_expectancy_years = countries_dict[country]
print(f'Fetched {country}: {life_expectancy_years}')
print(f'Fetched {country}: {life_expectancy_years}')
except requests.exceptions.RequestException as e:
print(e)

Expand Down

0 comments on commit 310daa2

Please sign in to comment.