-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.py
64 lines (51 loc) · 2.83 KB
/
currency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import web_utility
def convert(amount,home_currency_code,location_currency_code):
url_string = "https://www.google.com/finance/converter?a="+ str(amount) + "&from=" + str(home_currency_code) + "&to=" + str(location_currency_code)
results = web_utility.load_page(url_string)
if (home_currency_code == location_currency_code) or not home_currency_code + " = <span class=bld>" in results:
return -1
else:
result = (results[results.index(home_currency_code + " = <span class=bld>") + 22:results.index(location_currency_code + "</span>")])
return "{:.3f}".format(float(result))
def get_details(currency):
with open("currency_details.txt", "r", encoding= "utf8") as text_file:
for line in text_file:
if currency in line:
return line.strip("\n")
elif "\n" not in line:
return False
def get_all_details():
dict = {}
with open("currency_details.txt", "r", encoding= "utf8") as text_file:
for line in text_file:
dict[line.split(",")[0]] = tuple(line.strip().split(","))
return dict
if __name__ == '__main__':
#testing code
def check_detail(value,currency):
if value is False:
print("{:>15} {:>10} : {:>5}".format("invalid detail",currency,"()"))
else:
print("{:>15} {:>10} : {:>5}".format("valid detail",currency,value))
def check_currency(value,amount,home_currency_code,location_currency_code):
if value == -1:
print("{:>24} {:>20} {}->{} {}".format("invalid conversion",float(amount),str(home_currency_code),str(location_currency_code),(value)))
else:
print("{:>24} {:>20} {}->{} {}".format("valid conversion",float(amount),str(home_currency_code),str(location_currency_code),(value)))
print("{:>20} {:>20} {}->{} {}".format("valid conversion reverse",(value),str(location_currency_code),str(home_currency_code),float(amount)))
check_currency(convert(1,"AUD","AUD"),1,"AUD","AUD")
check_currency(convert(1,"JPY","ABC"),1,"JPY","ABC")
check_currency(convert(1,"ABC","USD"),1,"ABC","USD")
check_currency(convert(80,"JPY","AUD"),80,"JPY","AUD")
check_currency(convert(81,"JPY","AUD"),81,"JPY","AUD")
check_currency(convert(10.95,"AUD","JPY"),10.95,"AUD","JPY")
check_currency(convert(10.95,"AUD","BGN"),10.95,"AUD","BGN")
check_currency(convert(200.15,"BGN","JPY"),200.15,"BGN","JPY")
check_currency(convert(100,"JPY","USD"),100,"JPY","USD")
check_currency(convert(19.99,"USD","BGN"),19.99,"USD","BGN")
check_currency(convert(19.99,"USD","AUD"),19.99,"USD","AUD")
check_detail(get_details("Unknown"),"Unknown")
check_detail(get_details("Japanese"),"Japanese")
check_detail(get_details("()"),"()")
check_detail(get_details("Australia"),"Australia")
check_detail(get_details("Japan"),"Japan")