import urllib.request class Currency(object): """Currency class""" def __init__(self, amount = 1, curr1 = 'USD'): """ exchages currency rates amount - amount of the selected currency given code - code for the type of currency given """ self.amount = amount self.curr1 = curr1 def __str__(self): """returns the amount and type of currency""" return "Currency(amount:{},curr1:{})".format(self.amount,self.curr1) def convert_to(self,curr2): """Coverts the currency into the other currency by taking the string of the first curr1 amount, the string of the curr1, and the string of the second currency curr2 and post them into the select google API calculator as shown below.""" self.curr2 = curr2 string = 'http://www.google.com/ig/calculator?hl=en&q='+str(self.amount)+str(self.curr1)+'=?'+str(self.curr2) web_obj = urllib.request.urlopen(string) results_str = str(web_obj.read()) web_obj.close() return(results_str) def __add__(self, amount = 1,curr2 ='USD'): """Uses Same method to convert currencies but first finds out if function is curr + int, curr + float, or curr + curr""" """Once you know its a curr + curr makes a list and splices the appropriate numbers from the list""" if type(amount) == int: x = self.amount + amount print(str(self.amount)+' '+self.curr1+' '+'plus'+' '+str(amount)+' '+self.curr1+' '+'is equal to'+' '+str(x)+' '+self.curr1) elif type(amount) == float: x = self.amount +amount print(str(self.amount)+' '+self.curr1+' '+'plus'+' '+str(amount)+' '+self.curr1+' '+'is equal to'+' '+str(x)+' '+self.curr1) else: x_amount = self.amount x_curr = self.curr1 self.curr2 = str(amount) currency2 = self.curr2.strip() curr2_list = currency2.split(',') y_amount = curr2_list[0][16::] y_curr = curr2_list[1][6:9] string ='http://www.google.com/ig/calculator?hl=en&q='+str(x_amount)+str(x_curr)+'=?'+str(y_curr) string_2 ='http://www.google.com/ig/calculator?hl=en&q='+str(y_amount)+str(y_curr)+'=?'+str(x_curr) web_obj = urllib.request.urlopen(string) web_obj_2 = urllib.request.urlopen(string_2) results_str = str(web_obj.read()) results_str_2 = str(web_obj_2.read()) """It then converts the currencies into both of the currencies selected. For instance if USD and EUR were selected currencies then the amount of USD would be converted into EUR and the EUR would be converted into USD.""" """Depending on if one of the currencies were EUR or not is needed because the list splicing is different for the EUR Then the rest of the currencies. The functiong then splices all the numbers and adds them together in both currencies. For instance if USD and EUR were selected then the amount would be convert into both USD and EUR currency.""" if x_curr == 'EUR' and y_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1 =(word_list[1][1::]) dollars_1 =(word_list[3][1::]) line2 = results_str_2.strip() word_list_2 = line2.split() dollars_2 = (word_list_2[1][1::]) currency_2 = (word_list_2[4][1::]) dog = float(dollars_1) + float(dollars_2) cat = float(currency_1) + float(currency_2) print('The amount in'+' '+ y_curr +' '+ 'when adding'+' '+ str(x_amount)+' '+str(x_curr)+' '+'to'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',dog) print('The amount in'+' '+ x_curr +' '+ 'when adding'+' '+ str(y_amount)+' '+str(y_curr)+' '+'to'+' '+str(x_amount)+' '+str(x_curr)+' '+ 'is:',cat) elif y_curr == 'EUR' and x_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1 =(word_list[1][1::]) dollars_1 =(word_list[4][1::]) line2 = results_str_2.strip() word_list_2 = line2.split() dollars_2 = (word_list_2[1][1::]) currency_2 = (word_list_2[3][1::]) dog = float(dollars_1) + float(dollars_2) cat = float(currency_1) + float(currency_2) print('The amount in'+' '+ y_curr +' '+ 'when adding'+' '+ str(x_amount)+' '+str(x_curr)+' '+'to'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',dog) print('The amount in'+' '+ x_curr +' '+ 'when adding'+' '+ str(y_amount)+' '+str(y_curr)+' '+'to'+' '+str(x_amount)+' '+str(x_curr)+' '+ 'is:',cat) else: line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[4][1::]) line2 = results_str_2.strip() word_list_2 = line2.split() currency_2_y = (word_list_2[1][1::]) currency_1_y = (word_list_2[4][1::]) dog = float(currency_1_x) + float(currency_1_y) cat = float(currency_2_x) + float(currency_2_y) print('The amount in'+' '+ y_curr +' '+ 'when adding'+' '+ str(x_amount)+' '+str(x_curr)+' '+'to'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',dog) print('The amount in'+' '+ x_curr +' '+ 'when adding'+' '+ str(y_amount)+' '+str(y_curr)+' '+'to'+' '+str(x_amount)+' '+str(x_curr)+' '+ 'is:',cat) def __sub__(self, amount = 1,curr2 ='USD'): """The same method is used for subtraction as the add function except this time the currencies are subtracted.""" """The currency first currency is subtracted from the second currency. Once again the currency amount left over after the subtraction is done is converted into both of the selected currencies as done so before with the add function above.""" if type(amount) == int: x = self.amount - amount print(str(self.amount)+' '+self.curr1+' '+'minus'+' '+str(amount)+' '+self.curr1+' '+'is equal to'+' '+str(x)+' '+self.curr1) elif type(amount) == float: x = self.amount - amount print(str(self.amount)+' '+self.curr1+' '+'minus'+' '+str(amount)+' '+self.curr1+' '+'is equal to'+' '+str(x)+' '+self.curr1) else: x_amount = self.amount x_curr = self.curr1 self.curr2 = str(amount) currency2 = self.curr2.strip() curr2_list = currency2.split(',') y_amount = curr2_list[0][16::] y_curr = curr2_list[1][6:9] string ='http://www.google.com/ig/calculator?hl=en&q='+str(x_amount)+str(x_curr)+'=?'+str(y_curr) string_2 ='http://www.google.com/ig/calculator?hl=en&q='+str(y_amount)+str(y_curr)+'=?'+str(x_curr) web_obj = urllib.request.urlopen(string) web_obj_2 = urllib.request.urlopen(string_2) results_str = str(web_obj.read()) results_str_2 = str(web_obj_2.read()) if x_curr == 'EUR' and y_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[3][1::]) line2 = results_str_2.strip() word_list_2 = line2.split() currency_2_y = (word_list_2[1][1::]) currency_1_y = (word_list_2[4][1::]) dog = float(currency_2_y) - float(currency_2_x) cat = float(currency_1_y) - float(currency_1_x) print('The amount in'+' '+ y_curr +' '+ 'when subtracting'+' '+ str(x_amount)+' '+str(x_curr)+' '+'from'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',dog) print('The amount in'+' '+ x_curr +' '+ 'when subtracting'+' '+ str(x_amount)+' '+str(x_curr)+' '+'from'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',cat) elif y_curr == 'EUR' and x_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[4][1::]) line2 = results_str_2.strip() word_list_2 = line2.split() currency_2_y = (word_list_2[1][1::]) currency_1_y = (word_list_2[3][1::]) dog = float(currency_2_y) - float(currency_2_x) cat = float(currency_1_y) - float(currency_1_x) print('The amount in'+' '+ y_curr +' '+ 'when subtracting'+' '+ str(x_amount)+' '+str(x_curr)+' '+'from'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',dog) print('The amount in'+' '+ x_curr +' '+ 'when subtracting'+' '+ str(x_amount)+' '+str(x_curr)+' '+'from'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',cat) else: line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[4][1::]) line2 = results_str_2.strip() word_list_2 = line2.split() currency_2_y = (word_list_2[1][1::]) currency_1_y = (word_list_2[4][1::]) dog = float(currency_2_y) - float(currency_2_x) cat = float(currency_1_y) - float(currency_1_x) print('The amount in'+' '+ y_curr +' '+ 'when subtracting'+' '+ str(x_amount)+' '+str(x_curr)+' '+'from'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',dog) print('The amount in'+' '+ x_curr +' '+ 'when subtracting'+' '+ str(x_amount)+' '+str(x_curr)+' '+'from'+' '+str(y_amount)+' '+str(y_curr)+' '+ 'is:',cat) def __gt__(self, amount = 1,curr2 ='USD'): """This function is the greater than function and tells you if the currency is more than an interger or a float. Also the function can tell you if a currency is greater than another one. To check if a currency is greater than another one the function must first convert the first currency into the second currency. Then from there it determines if the first currency now converted into the second currency is greater than the second currencies amount.""" if type(amount) == int: if self.amount > amount: print('True') else: print('False') elif type(amount) == float: if self.amount > amount: print('True') else: print('False') else: x_amount = self.amount x_curr = self.curr1 self.curr2 = str(amount) currency2 = self.curr2.strip() curr2_list = currency2.split(',') y_amount = curr2_list[0][16::] y_curr = curr2_list[1][6:9] string ='http://www.google.com/ig/calculator?hl=en&q='+str(x_amount)+str(x_curr)+'=?'+str(y_curr) web_obj = urllib.request.urlopen(string) results_str = str(web_obj.read()) if x_curr == 'EUR' and y_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[3][1::]) ratio = (float(currency_2_x)/float(currency_1_x)) if ratio > 1: print('True') else: print('False') elif y_curr == 'EUR' and x_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[4][1::]) ratio = (float(currency_2_x)/float(currency_1_x)) if ratio > 1: print('True') else: print('False') else: line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[4][1::]) ratio = (float(currency_2_x)/float(currency_1_x)) if ratio > 1: print('True') else: print('False') def __lt__(self, amount = 1,curr2 ='USD'): """This is the less than function. It is exactly the same as the greater than function except wherever the greater than symbol '>' was used is not made into the less than symbol '<'. Which as tested in the main function gives opposite answers of the greater than function""" if type(amount) == int: if self.amount < amount: print('True') else: print('False') elif type(amount) == float: if self.amount < amount: print('True') else: print('False') else: x_amount = self.amount x_curr = self.curr1 self.curr2 = str(amount) currency2 = self.curr2.strip() curr2_list = currency2.split(',') y_amount = curr2_list[0][16::] y_curr = curr2_list[1][6:9] string ='http://www.google.com/ig/calculator?hl=en&q='+str(x_amount)+str(x_curr)+'=?'+str(y_curr) web_obj = urllib.request.urlopen(string) results_str = str(web_obj.read()) if x_curr == 'EUR' and y_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[3][1::]) ratio = (float(currency_2_x)/float(currency_1_x)) if ratio < 1: print('True') else: print('False') elif y_curr == 'EUR' and x_curr != 'EUR': line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[4][1::]) ratio = (float(currency_2_x)/float(currency_1_x)) if ratio < 1: print('True') else: print('False') else: line = results_str.strip() word_list = line.split() currency_1_x =(word_list[1][1::]) currency_2_x =(word_list[4][1::]) ratio = (float(currency_2_x)/float(currency_1_x)) if ratio < 1: print('True') else: print('False') def __repr__(self): """This function lets you grab a previously made currency while in the python shell""" return 'Currency (amount=%s,curr1=%s)' %(self.amount,self.curr1) def main(): """This is the main function and it test everything that is needed... It test adding different currencies,subtracting different currencies, then it does both of them in reverse, adding an integer/float to a currency, subtracting a interger/float from a currency It test if a currency is greater than an integer, float, or another currency, and it finally test if a currency is less than an interger, float, or another currency.(for both the greater and less than test the first currency is coverted to second currency in order to compare correctly)""" print('#'*80) print("Here are the different currencies we will be using") curr1 = Currency(7,'USD') curr2 = Currency(6,'EUR') curr3 = Currency(5,'ARS') curr4 = Currency(4,'BHD') curr5 = Currency(3,'CNY') curr6 = Currency(2,'DZD') print(curr1,'= curr1') print(curr2,'= curr2') print(curr3,'= curr3') print(curr4,'= curr4') print(curr5,'= curr5') print(curr6,'= curr6') print('#'*80) print(' '*80) print('#'*80) print("Adding different currencies") new_curr2 = curr1+curr2 new_curr3 = curr2+curr3 new_curr4 = curr3+curr4 print('#'*80) print(' '*80) print('#'*80) print("Reverse adding works too") a = curr2+curr1 b = curr3+curr2 c = curr4+curr3 print('#'*80) print(' '*80) print('#'*80) print("Subtracting different currencies") new_curr5 = curr1-curr2 new_curr6 = curr2-curr3 new_curr7 = curr3-curr4 print('#'*80) print(' '*80) print('#'*80) print("Reverse subtraction works too") d = curr2-curr1 e = curr3-curr2 f = curr4-curr3 print('#'*80) print(' '*80) print('#'*80) print("Adding an interger and a float to a currency") new_curr8 = curr5 + 5 new_curr9 = curr6 +5.343 print('#'*80) print(' '*80) print('#'*80) print("Subtracting an interger and a float from a currency") new_curr10 = curr4 - 3 new_curr11 = curr2 - 7.5 print('#'*80) print(' '*80) print('#'*80) print("Testing if a currency is greater than an integer, a float, or a different currency") print('curr5 > 2') new_curr12 = curr5 > 2 print('curr6 > 3.5') new_curr13 = curr6 > 3.5 print('curr2 > curr5') new_curr14 = curr2 > curr5 print('#'*80) print(' '*80) print('#'*80) print("Testing if a currency is less than an integer, a float, or a different currency") print('curr5 < 2') new_curr15 = curr5 < 2 print('curr6 < 3.5') new_curr16 = curr6 < 3.5 print('curr2 < curr5') new_curr17 = curr2 < curr5 print('#'*80) main()