Last active
August 29, 2015 13:56
-
-
Save pirhoo/8974596 to your computer and use it in GitHub Desktop.
dojo-kata-1
This file contains hidden or 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
| #!/usr/bin/env python | |
| # Encoding: utf-8 | |
| from random import choice, randint | |
| import requests | |
| DATASRC = "http://api.worldbank.org/countries/all/indicators/SP.DYN.LE00.IN?date={year}&format=json&per_page=10000" | |
| def get(year): | |
| key = "cache_%s" % year | |
| if hasattr(get, key): return getattr(get, key) | |
| r = requests.get( DATASRC.format(year=year) ) | |
| data = r.json() | |
| if len(data) > 1: | |
| setattr(get, key, data[1]) | |
| return data[1] | |
| else: | |
| return [] | |
| def getRandomCountriesByYear(year, lenght=2): | |
| countries = set() | |
| # Get countries for this year | |
| data = get(year) | |
| if len(data): | |
| while len(countries) < lenght: | |
| c = choice(data) | |
| if c["value"] is not None: | |
| countries.add( c["country"]["id"] ) | |
| return countries | |
| def getCountryByYear(year, iso): | |
| # Get countries for this year | |
| data = get(year) | |
| country = None | |
| for c in data: | |
| if c["country"]["id"] == iso: | |
| country = ( iso, c["country"]["value"], c["value"] ) | |
| return country | |
| def getRandomYear(): | |
| return randint(1980, 2012) | |
| if __name__ == "__main__": | |
| YEAR = getRandomYear() | |
| for iso in getRandomCountriesByYear(YEAR): | |
| print getCountryByYear(YEAR, iso) |
This file contains hidden or 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
| #!/usr/bin/env python | |
| # Encoding: utf-8 | |
| import unittest | |
| import sys | |
| import data | |
| def who_is_the_winner(iso_1, iso_2, year): | |
| data_1 = data.getCountryByYear(year, iso_1) | |
| data_2 = data.getCountryByYear(year, iso_2) | |
| if data_1[2] > data_2[2]: | |
| return iso_1 | |
| else: | |
| return iso_2 | |
| class WhoIsTheWinnerTestCase(unittest.TestCase): | |
| def test_who_is_the_winner(self): | |
| self.assertEqual(who_is_the_winner('FR','FI', 1980), 'FR') | |
| self.assertEqual(who_is_the_winner('GR','DE', 1980), 'GR') | |
| self.assertEqual(who_is_the_winner('IT','IL', 1980), 'IT') | |
| self.assertEqual(who_is_the_winner('GR','DE', 2010), 'GR') | |
| self.assertEqual(who_is_the_winner('GA','GM', 2010), 'GA') | |
| def test_random_countries(self): | |
| year = data.getRandomYear() | |
| countries = data.getRandomCountriesByYear(year) | |
| self.assertTrue(len(countries), 2) | |
| def test_pick_date(self): | |
| year = data.getRandomYear() | |
| self.assertIn(year, range(1980, 2010)) | |
| if __name__ == '__main__': | |
| if len(sys.argv) > 1 and sys.argv[1] == 'test': | |
| del sys.argv[1] | |
| unittest.main() | |
| else: | |
| print "Welcome to the life expentency game !" | |
| year = data.getRandomYear() | |
| countries = list(data.getRandomCountriesByYear(year)) | |
| country = data.getCountryByYear(year, countries[0]) | |
| other_country = data.getCountryByYear(year, countries[1]) | |
| print "Who between {country}(1) and {other_country}(2) had the higher life expectancy in {year}".format( | |
| country=country[1], other_country=other_country[1], year=year | |
| ) | |
| choice = input('Enter the number of your choice(1 or 2):') | |
| answer = countries[choice - 1] | |
| winner = who_is_the_winner(country[0], other_country[0], year) | |
| if answer == winner: | |
| print "You win, congratulations !" | |
| else: | |
| print "You lose, too bad !" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment