Question: can anyone help me to implement function for this spec and design test cases? Returns a JSON string that is a response to a

can anyone help me to implement function for this spec and design test cases?

""" 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": "", dst: "", 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 """

Implement the function according to the specification. To do this, you will need a function that connects to a web page/service. Fortunately, there is such a function. Look at the Web Access Functions in the IntroCS API and find the function urlread. The purpose of this function is to generate the corresponding URL query string with the provided currency values and return a JSON string as a response to the query.

Try this function out in the interactive shell using the query from the following step:

codio@mike-panther:~/workspace$ python3 Python 3.6.6 (default, Jul 31 2018, 22:09:10) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import introcs >>> q = 'https://ecpyfac.ecornell.com/python/currency/fixed?src=USD&dst=EUR&amt=2.5&key=a1b2c3d4' >>> introcs.urlread(q) '{"success": true, "src": "2.5 United States Dollars", "dst": "2.2160175 Euros", "error": ""}' 

See how this works? In this example, the source currency is USD (for US Dollars) and the destination is EUR (for Euros), and the amount of dollars to convert to Euros is the value of amt, 2.5. In the returned JSON string, the value of 2.4 Dollars in Euros is shown as "2.2160175". For other amounts or currencies, you would change the value of src, dst, or amt.

Use this function to implement service_response. Use the test script testcurrency.py to aid your development before checking your answer below.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!