import json
import urllib.request
url = "http://api.fixer.io/2014-01-03"
response_bytes = urllib.request.urlopen(url).read()
print(response_bytes)
data = json.loads(response_bytes)
print(data["rates"]["CHF"])
import time
def fetch_exchange_rate(year, month, day, _cache={}):
date_str = "{:4d}-{:02d}-{:02d}".format(year, month, day)
if date_str in _cache:
print("cache hit !")
return _cache[date_str]
url = "http://api.fixer.io/{}".format(date_str)
response_bytes = urllib.request.urlopen(url).read()
data = json.loads(response_bytes)
result = data["rates"]["CHF"]
_cache[date_str] = result
return result
print(fetch_exchange_rate(2015, 1, 1))
print(fetch_exchange_rate(2017, 12, 1))
print(fetch_exchange_rate(2017, 12, 1))
print(fetch_exchange_rate(2017, 12, 1))
from functools import wraps
def cache(function):
cache = {}
def wrapped(*args, **kwargs):
key = args + tuple(kwargs.items())
if key not in cache:
print("key", key, "not in cache")
cache[key] = function(*args, **kwargs)
return cache[key]
return wrapped
def fetch_exchange_rate(year, month, day, _cache={}):
date_str = "{:4d}-{:02d}-{:02d}".format(year, month, day)
url = "http://api.fixer.io/{}".format(date_str)
response_bytes = urllib.request.urlopen(url).read()
data = json.loads(response_bytes)
return data["rates"]["CHF"]
fetch_exchange_rate = cache(fetch_exchange_rate)
print(fetch_exchange_rate(2017, 12, 1))
print(fetch_exchange_rate(2017, 12, 1))
print(fetch_exchange_rate(2017, 12, 1))
@cache
def fetch_exchange_rate(year, month, day, _cache={}):
date_str = "{:4d}-{:02d}-{:02d}".format(year, month, day)
url = "http://api.fixer.io/{}".format(date_str)
response_bytes = urllib.request.urlopen(url).read()
data = json.loads(response_bytes)
return data["rates"]["CHF"]
print(fetch_exchange_rate(2015, 1, 1))
print(fetch_exchange_rate(2017, 12, 1))
print(fetch_exchange_rate(2017, 12, 1))
print(fetch_exchange_rate(2017, 12, 1))
def to_list(generator):
def wrapped(*args, **kwargs):
return list(generator(*args, **kwargs))
return wrapped
from datetime import datetime
@to_list
def first_month_rates():
for year in (2015, 2016, 2017):
for month in range(1, 13):
yield datetime(year, month, 1), fetch_exchange_rate(year, month, 1)
from matplotlib import pylab
dates_and_rates = first_month_rates()
dates = [date for (date, rate) in dates_and_rates]
rates = [rate for (date, rate) in dates_and_rates]
pylab.figure(figsize=(20,4))
pylab.plot(dates, rates)
pylab.show()
from statistics import mean
import time
def is_leap_year(year):
if year % 400 == 0:
return True
if year % 100 == 0:
return False
return year % 4 == 0
def last_day(year, month):
if month == 2:
return 29 if is_leap_year(year) else 28
if month in (1, 3, 5, 7, 8, 10, 12):
return 31
return 30
@cache
def monthly_average_exchange_rate(year, month):
rates = []
for day in range(1, last_day(year, month) + 1):
rates.append(fetch_exchange_rate(year, month, day))
time.sleep(.2)
return datetime(year, month, 1), mean(rates)
monthly_average_exchange_rate(2016, 1)
dates_and_rates = first_month_rates()
dates = [date for (date, rate) in dates_and_rates]
rates = [rate for (date, rate) in dates_and_rates]
pylab.figure(figsize=(20,4))
pylab.plot(dates, rates)
while True:
try:
dates_and_rates = [monthly_average_exchange_rate(2017, month) for month in range(1, 13)]
except urllib.request.HTTPError:
print("try again")
time.sleep(3)
continue
break
dates = [date for (date, rate) in dates_and_rates]
rates = [rate for (date, rate) in dates_and_rates]
pylab.plot(dates, rates, "r:")
pylab.show()