String manipulation in python using APIs
This was done as part of my course ‘Introduction to Programming’.It was a stepping stone to what I knew about programming done in high school.
Here, I will cover what I learnt and all the necessary resources required on the way.
For this assignment, we will access the data of currency exchange rates from the
open source website https://exchangeratesapi.io/. You can access the data using the
APIs (Application Programming Interfaces) given on the website.
Following are the APIs used for this assignment
Note: Date Format : yyyy-mm-dd
If an invalid query is entered,following response pops up:
{"error":"time data 'message' does not match format '%Y-%m-%d'"}
url = urllib.request.urlopen( "https://api.exchangeratesapi.io/latest" )
data = url.read()
This function is responsible to get latest current exchange rates.
def getLatestRates():
"""Returns: a JSON string that is a response to a latest rates query.
The Json string will have the attributes: rates, base and date (yyyy-mm-dd).
"""
url=urlopen("https://api.exchangeratesapi.io/latest")
data=url.read()
return data
Once we have implemented getLatestRates() correctly, we have the currency
exchange data in the form of a string.
Now we will process it and apply String module functions (you don’t need to import any modules for this) to perform the following queries.
def changeBase(amount,currency,desiredCurrency,date):
"""Outputs: a float value f.
"""
url=urlopen("https://api.exchangeratesapi.io/"+date)
data=url.read()
result=eval(data)
a=result["rates"][currency]
b=result["rates"][desiredCurrency]
value=(b/a)*amount
return value
def printAsending(json):
"""Output: the sorted order of the Rates
You don't have to return anything.
Parameter:
json: a json string to parse
"""
data=eval(json)#converts string into dictionary
x=data["rates"]#a dictionary of only rates
y=sorted(x.items(),key=lambda x:x[1])#sorts according to the values and returns a list of tuples
x=dict(y)#converts back to dictionary
for k in x:
print("1 "+data["base"]+" = "+str(x[k])+" "+k)
def extremeFridays(startDate,endDate,currency):
"""Output: on which friday was currency the strongest and on which was it the weakest.
You don't have to return anything.
Parameters:
stardDate and endDate: strings of the form yyyy-mm-dd
currency: a string representing the currency those extremes you have to determine
"""
url=urlopen("https://api.exchangeratesapi.io/history?start_at="+startDate+"&end_at="+endDate)
data=eval(url.read())#string to dictionary
for key in data["rates"].keys():
y=parse(key).weekday()#converts into date format and weekday returns no of the weekday
if(y==4):#checks if its friday
x.update({key:data["rates"][key][currency]})#updates date and currency value on that date in a dictionary
z=sorted(x.items(),key=lambda x:x[1])#sorting according to values
x=dict(z)#converts tuples into dictionary
print(currency+" was strongest on "+x[0])
print(currency+" was weakest on "+x[-1])
def findMissingDates(startDate,endDate):
"""Output: the dates that are not present when you do a json query from startDate to endDate
You don't have to return anything.
Parameters: stardDate and endDate: strings of the form yyyy-mm-dd
"""
url=urlurl=urlopen("https://api.exchangeratesapi.io/history?start_at="+startDate+"&end_at="+endDate)
data=eval(url.read())#string to dictionary
x=[]
for key in data["rates"].keys():
x.append(datetime.strptime(key,'%Y-%m-%d').date())#convets string to date objects
x.sort() #sorts the date objects
for i in range(len(x)-1):#print missing dates
a=x[i]
b=x[i+1]
c=a+timedelta(1)
while c!=b:#prints dates till c is equal next date
print(c)
c+=timedelta(1)
Sample code consisiting of all queries
self.assertAlmostEqual(changeBase(782, "SGD", "PLN", "2016-04-18"), 2196.7, delta = 0.1)
self.assertAlmostEqual(changeBase(314, "INR", "CAD", "2014-05-21"), 5.8, delta = 0.05)
self.assertAlmostEqual(changeBase(200, "INR", "BRL","2018-02-22"), 10.05, delta = 0.1)
self.assertAlmostEqual(changeBase(2708, "EEK", "ZAR","2009-12-01"), 1915.3, delta = 0.1)