Last active
August 29, 2015 13:59
-
-
Save vied12/10975982 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# Encoding: utf-8 | |
# ----------------------------------------------------------------------------- | |
# Project : KATA #5 | |
# ----------------------------------------------------------------------------- | |
# Author : Edouard Richard <edou4rd@gmail.com> | |
# ----------------------------------------------------------------------------- | |
# Creation : 17-Apr-2014 | |
# Last mod : 17-Apr-2014 | |
# ----------------------------------------------------------------------------- | |
import requests | |
from collections import OrderedDict | |
import sys | |
country = sys.argv[1] | |
URL = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/inmcm3_0/a2/{var}/2060/2079/{country}.json" | |
DATA = { | |
"Potatoes" : {"pr":1400, "tas" :8}, | |
"White Wine" : {"pr":1300, "tas" :11.5}, | |
"Red Wine" : {"pr":1200, "tas" :13}, | |
"Olives" : {"pr":800, "tas" :14.5}, | |
"Oranges" : {"pr":900, "tas" :16}, | |
"Mango" : {"pr":410, "tas" :20}, | |
} | |
pr = requests.get(URL.format(country=country, var='pr')).json()[0]['annualData'][0] | |
tas = requests.get(URL.format(country=country, var='tas')).json()[0]['annualData'][0] | |
results = {} | |
for item, info in DATA.items(): | |
delta_pr = abs(info['pr'] - pr) | |
delta_tas = abs(info['tas'] - tas) | |
results[item] = {"delta_pr": delta_pr, 'delta_tas':delta_tas} | |
moyennes = {'tas':0, 'pr': 0} | |
for item, info in results.items(): | |
moyennes['tas'] += info["delta_tas"] | |
moyennes['pr'] += info["delta_pr"] | |
moyennes['tas'] = moyennes['tas'] / len(results.keys()) | |
moyennes['pr'] = moyennes['pr'] / len(results.keys()) | |
for item, info in results.items(): | |
results[item]["score_tas"] = info["delta_tas"] / moyennes['tas'] | |
results[item]["score_pr"] = info["delta_pr"] / moyennes['pr'] | |
results[item]["score"] = results[item]["score_tas"] + results[item]["score_pr"] | |
r = OrderedDict(sorted(results.items(), key=lambda t: t[1]['score'])) | |
for i, (item, info) in enumerate(r.items()): | |
print "#%d" % (i+1), item, info['score'] | |
# EOF | |
# python kata5.py deu | |
# #1 Red Wine 0.218520281791 | |
# #2 White Wine 0.683275003277 | |
# #3 Olives 1.93571175662 | |
# #4 Oranges 2.07202077903 | |
# #5 Potatoes 2.11864782465 | |
# python kata5.py fra | |
# #1 Oranges 0.447508577521 | |
# #2 Olives 0.633856575672 | |
# #3 Red Wine 1.50405455813 | |
# #4 White Wine 2.31223836059 | |
# #5 Mango 3.35126873604 | |
# #6 Potatoes 3.75107319204 | |
# python kata5.py che | |
# #1 Red Wine 0.583767891527 | |
# #2 Olives 1.25701423138 | |
# #3 White Wine 1.41896342225 | |
# #4 Oranges 1.42181870065 | |
# #5 Potatoes 2.92082561965 | |
# #6 Mango 4.39761013454 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment