-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha1.py
211 lines (169 loc) · 6.97 KB
/
a1.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
"""
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.
Philip Cipollina (pjc272) Luke Marcinkiewicz (lam365)
September 14th, 2017
"""
def before_space(s):
"""
Returns: Substring of s; up to, but not including, the first space
Parameter s: the string to slice
Precondition: s has at least one space in it
"""
#find the space
a=s.index(' ')
#slice from beginning to the space
result=s[:a]
return result
def after_space(s):
"""
Returns: Substring of s after the first space
Parameter s: the string to slice
Precondition: s has at least one space in it
"""
#find first space
b=s.index(' ')
#slice from 1 character after space to end
result2=s[b+1:]
return result2
def first_inside_quotes(s):
"""
Returns: The first substring of s between two (double) quote characters
A quote character is one that is inside a string, not one that delimits it.
We typically use single quotes (') to delimit a string if want to use a
double quote character (") inside of it.
Example: If s is 'A "B C" D', this function returns 'B C'
Example: If s is 'A "B C" D "E F" G', this function still returns 'B C'
because it only picks the first such substring.
Parameter s: a string to search
Precondition: s a string with at least two (double) quote characters.
"""
#find beginning quotes
start=s.index('"')
#store portion of text after it
tail=s[start+1:]
#find end quotes
end=tail.index('"')
result3=tail[:end]
return result3
def get_lhs(json):
"""
Returns: The the LHS value in the response to a currency query.
Given a JSON response to a currency query, this returns the string inside
double quotes (") immediately following the keyword "lhs". For example, if
the JSON is
'{"success":true, "lhs":"2 United States Dollars", "rhs":"1.825936 Euros",
"error":""}'
then this function returns '2 United States Dollars' (not '"2 United States
Dollars"'). It returns the empty string if the JSON is the result of an
invalid query.
Parameter json: a json string to parse
Precondition: json is the response to a currency query
"""
#find string lhs
left=json.index('"lhs"')
#get value inside quotes after lhs
val=json[left+5:]
result4=first_inside_quotes(val)
return result4
def get_rhs(json):
"""
Returns: The RHS value in the response to a currency query.
Given a JSON response to a currency query, this returns the string inside
double quotes (") immediately following the keyword "rhs". For example, if
the JSON is
'{"success":true, "lhs":"2 United States Dollars", "rhs": "1.825936 Euros",
"error":""}'
then this function returns '1.825936 Euros' (not '"1.825936 Euros"'). It
returns the empty string if the JSON is the result of on invalid query.
Parameter json: a json string to parse
Precondition: json is the response to a currency query
"""
#find the string rhs
right=json.index('"rhs"')
#find value inside quotes following rhs
valr=json[right+5:]
result5=first_inside_quotes(valr)
return result5
def has_error(json):
"""
Returns: True if the query has an error; False otherwise.
Given a JSON response to a currency query, this returns the opposite of
the value following the keyword "success". For example, if the JSON is
'{"success":false, "lhs":"", "rhs":"", "error":"Source currency code is
invalid."}'
then the query is not valid, so this function returns True (It does NOT
return the message 'Source currency code is invalid').
Parameter json: a json string to parse
Precondition: json is the response to a currency query
"""
#find the beginning and end of segment where error value will appear
w=json.index(':')
x=json.index(',')
#slice the substring
y=json[w+2:x]
#compare to string 'false'
z=bool(y=='false')
return z
def currency_response(currency_from, currency_to, amount_from):
"""
Returns: a JSON string that is a response to a currency query.
A currency query converts amount_from money in currency currency_from
to the currency currency_to. The response should be a string of the form
'{"success":true, "lhs":"<old-amt>", "rhs":"<new-amt>", "error":""}'
where the values old-amount and new-amount contain the value and name for
the original and new currencies. If the query is invalid, both old-amount
and new-amount will be empty, while "success" will be followed by the value
false.
Parameter currency_from: the currency on hand (the LHS)
Precondition: currency_from is a string
Parameter currency_to: the currency to convert to (the RHS)
Precondition: currency_to is a string
Parameter amount_from: amount of currency to convert
Precondition: amount_from is a float
"""
import cornell
#set url variable
url='http://cs1110.cs.cornell.edu/2017fa/a1server.php?src='
#add proper values to variable url
url=url+currency_from + '&dst='+ currency_to +'&amt='+str(amount_from)
result=cornell.urlread(url)
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 string.
"""
#input string to webserver
input=currency_response('USD', currency, 1.0)
#determine if there is an error using has_error
err=has_error(input)
result=bool(err==False)
return result
def exchange(currency_from, currency_to, amount_from):
"""
Returns: amount of currency received in the given exchange.
In this exchange, the user is changing amount_from money in currency
currency_from to the currency currency_to. The value returned represents the
amount in currency currency_to.
The value returned has type float.
Parameter currency_from: the currency on hand (the LHS)
Precondition: currency_from is a string for a valid currency code
Parameter currency_to: the currency to convert to (the RHS)
Precondition: currency_to is a string for a valid currency code
Parameter amount_from: amount of currency to convert
Precondition: amount_from is a float
"""
#get json string using currency response
str=currency_response(currency_from, currency_to, amount_from)
#find the returned (right side) value
rhs=get_rhs(str)
#strip to find only numerical value
num=before_space(rhs)
#convert string into float
result=float(num)
return result