-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.py
264 lines (187 loc) · 9.15 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
Module for currency exchange
This module provides several string parsing functions to implement a simple
currency exchange routine using an online currency service. The primary function
in this module is exchange().
Author: Brantley G. Gusler
Date: 1/19/2022
"""
import introcs
#enter API key here:
APIKEY = ''
def before_space(s):
"""
Returns the substring of s up to, but not including, the first space.
Example: before_space('Hello World') returns 'Hello'
Parameter s: the string to slice
Precondition: s is a string with at least one space in it
"""
assert type(s) == str, repr(s)+' is not a string.'
assert introcs.count_str(s,' ') >= 1, 'The string '+repr(s)+' does not have enough spaces.'
a = introcs.find_str(s,' ')
slice = s[0:a]
return slice
def after_space(s):
"""
Returns the substring of s after the first space
Example: after_space('Hello World') returns 'World'
Parameter s: the string to slice
Precondition: s is a string with at least one space in it
"""
assert type(s) == str, repr(s)+' is not a string.'
assert introcs.count_str(s,' ') >= 1, 'The string '+repr(s)+' does not have enough spaces.'
a = introcs.find_str(s,' ')+1
slice = s[a:]
return slice
def first_inside_quotes(s):
"""
Returns the first substring of s between two (double) quote characters
Note that the double quotes must be part of the string. So "Hello World" is a
precondition violation, since there are no double quotes inside the string.
Example: first_inside_quotes('A "B C" D') returns 'B C'
Example: first_inside_quotes('A "B C" D "E F" G') returns 'B C', because it only
picks the first such substring.
Parameter s: a string to search
Precondition: s is a string with at least two (double) quote characters inside
"""
assert type(s) == str, repr(s)+' is not a string.'
assert introcs.count_str(s,'"') >= 2, 'The string '+repr(s)+' does not have enough double quotes.'
a = introcs.find_str(s,'"')+1
b = introcs.find_str(s,'"',a)
slice = s[a:b]
return slice
def get_src(json):
"""
Returns the src value in the response to a currency query.
Given a JSON string provided by the web service, this function returns the string
inside string quotes (") immediately following the substring '"src"'. For example,
if the json is
'{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}'
then this function returns '2 United States Dollars' (not '"2 United States Dollars"').
On the other hand if the json is
'{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}'
then this function returns the empty string.
The web server does NOT specify the number of spaces after the colons. The JSON
'{"success":true, "src":"2 United States Dollars", "dst":"1.772814 Euros", "error":""}'
is also valid (in addition to the examples above).
Parameter json: a json string to parse
Precondition: json a string provided by the web service (ONLY enforce the type)
"""
assert type(json) == str, repr(json)+' is not a string.'
a = introcs.find_str(json, '"src"')
b = introcs.find_str(json, '"', a+1)
c = introcs.find_str(json, '"', b+1)
d = introcs.find_str(json, '"', c+1)
result = json[c+1:d]
return result
def get_dst(json):
"""
Returns the dst value in the response to a currency query.
Given a JSON string provided by the web service, this function returns the string
inside string quotes (") immediately following the substring '"dst"'. For example,
if the json is
'{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}'
then this function returns '1.772814 Euros' (not '"1.772814 Euros"'). On the other
hand if the json is
'{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}'
then this function returns the empty string.
The web server does NOT specify the number of spaces after the colons. The JSON
'{"success":true, "src":"2 United States Dollars", "dst":"1.772814 Euros", "error":""}'
is also valid (in addition to the examples above).
Parameter json: a json string to parse
Precondition: json a string provided by the web service (ONLY enforce the type)
"""
assert type(json) == str, repr(json)+' is not a string.'
a = introcs.find_str(json, '"dst"')
b = introcs.find_str(json, '"', a+1)
c = introcs.find_str(json, '"', b+1)
d = introcs.find_str(json, '"', c+1)
result = json[c+1:d]
return result
def has_error(json):
"""
Returns True if the response to a currency query encountered an error.
Given a JSON string provided by the web service, this function returns True if the
query failed and there is an error message. For example, if the json is
'{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}'
then this function returns True (It does NOT return the error message
'Source currency code is invalid'). On the other hand if the json is
'{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}'
then this function returns False.
The web server does NOT specify the number of spaces after the colons. The JSON
'{"success":true, "src":"2 United States Dollars", "dst":"1.772814 Euros", "error":""}'
is also valid (in addition to the examples above).
Parameter json: a json string to parse
Precondition: json a string provided by the web service (ONLY enforce the type)
"""
assert type(json) == str, repr(json)+' is not a string.'
a = introcs.find_str(json, '"error"')
b = introcs.find_str(json, '"', a+1, len(json))
c = introcs.find_str(json, '"', b+1, len(json))
d = introcs.find_str(json, '"', c+1, len(json))
e = json[c+1:d]
return len(e) > 0
def service_response(src, dst, amt):
"""
Returns a JSON string that is a response to a currency query.
A currency query converts amt money in currency src to the currency dst. The response
should be a string of the form
'{"success": true, "src": "<src-amount>", dst: "<dst-amount>", error: ""}'
where the values src-amount and dst-amount contain the value and name for the src
and dst currencies, respectively. If the query is invalid, both src-amount and
dst-amount will be empty, and the error message will not be empty.
There may or may not be spaces after the colon. To test this function, you should
chose specific examples from your web browser.
Parameter src: the currency on hand
Precondition src is a nonempty string with only letters
Parameter dst: the currency to convert to
Precondition dst is a nonempty string with only letters
Parameter amt: amount of currency to convert
Precondition amt is a float or int
"""
assert type(src) == str, repr(src)+' is not a string.'
assert introcs.isalpha(src), repr(src)+' contains an integer.'
assert type(dst) == str, repr(dst)+' is not a string.'
assert introcs.isalpha(dst), repr(dst)+' contains an integer.'
assert type(amt) == float or int, repr(amt)+' is not a float or an integer.'
assert type(amt) != bool, repr(amt)+' is a bool.'
assert type(amt) != str, repr(amt)+' is a string.'
result = introcs.urlread('https://ecpyfac.ecornell.com/python/currency/fixed?src='+src+'&dst='+dst+'&amt='+str(amt)+'&key='+APIKEY)
return result
def iscurrency(currency):
"""
Returns True if currency is a valid (3 letter code for a) currency.
It returns False otherwise.
Parameter currency: the currency code to verify
Precondition: currency is a nonempty string with only letters
"""
assert type(currency) == str, repr(currency)+' is not a string.'
assert introcs.isalpha(currency), repr(currency)+' contains an integer.'
a = introcs.urlread('https://ecpyfac.ecornell.com/python/currency/fixed?src='+currency+'&dst=EUR&amt=2.5&key='+APIKEY)
result = get_src(a)
return len(result) > 0
def exchange(src, dst, amt):
"""
Returns the amount of currency received in the given exchange.
In this exchange, the user is changing amt money in currency src to the currency
dst. The value returned represents the amount in currency currency_to.
The value returned has type float.
Parameter src: the currency on hand
Precondition src is a string for a valid currency code
Parameter dst: the currency to convert to
Precondition dst is a string for a valid currency code
Parameter amt: amount of currency to convert
Precondition amt is a float or int
"""
assert type(src) == str, repr(src)+' is not a string.'
assert iscurrency(src), repr(src)+' is not a valid currency code.'
assert type(dst) == str, repr(dst)+' is not a string.'
assert iscurrency(dst), repr(dst)+' is not a valid currency code.'
assert type(amt) == float or int, repr(amt)+' is not a float or an integer.'
assert type(amt) != bool, repr(amt)+' is a bool.'
assert type(amt) != str, repr(amt)+' is a string.'
a = service_response(src, dst, amt)
b = get_dst(a)
c = before_space(b)
result = float(c)
return result