python

lamlam
a1.py

""" 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: Anqi Bai/Hongyi Lu NetId:ab2897/hl2374 Date: 9/22/2019 """ import introcs def before_space(s): """ Returns a copy of s up to, but not including, the first space Parameter s: the string to slice Precondition: s is a string with at least one space """ pos = s.index(' ') before = s[:pos] return before def after_space(s): """ Returns a copy of s after the first space Parameter s: the string to slice Precondition: s is a string with at least one space """ pos = s.index(' ') after = s[pos+len(' '):] return after def first_inside_quotes(s): """ Returns the first substring of s between two (double) quotes 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. Examples: first_inside_quotes('A "B C" D') returns 'B C' 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 containing at least two double quotes """ pos1 = s.find('"') pos2 = s.find('"',pos1+1) return s[pos1+1:pos2] def get_lhs(json): """ Returns 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 '{ "ok":true, "lhs":"1 Bitcoin", "rhs":"9916.0137 Euros", "err":"" }' then this function returns '1 Bitcoin' (not '"1 Bitcoin"'). This function returns the empty string if the JSON response contains an error message. Parameter json: a json string to parse Precondition: json is the response to a currency query """ poslhs = (json).find('lhs') newjson = json[poslhs+4 :] return first_inside_quotes(newjson) 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 '{ "ok":true, "lhs":"1 Bitcoin", "rhs":"9916.0137 Euros", "err":"" }' then this function returns '9916.0137 Euros' (not '"9916.0137 Euros"'). This function returns the empty string if the JSON response contains an error message. Parameter json: a json string to parse Precondition: json is the response to a currency query """ posrhs = (json).find('rhs') newjson = json[posrhs+4 :] return first_inside_quotes(newjson) 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 "ok". For example, if the JSON is '{ "ok":false, "lhs":"", "rhs":"", "err":"Currency amount 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 """ pos1 = (json).find('"ok"') pos2 = (json).find(',',pos1+2) parameter = (json)[pos1+5:pos2] return bool(parameter =='false') def currency_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 '{ "ok":true, "lhs":"<old-amt>", "rhs":"<new-amt>", "err":"" }' 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 "ok" will be followed by the value false (and "err" will have an error message). Parameter src: the currency on hand (the LHS) Precondition: src is a string with no spaces or non-letters Parameter dst: the currency to convert to (the RHS) Precondition: dst is a string with no spaces or non-letters Parameter amt: amount of currency to convert Precondition: amt is a float """ stramt = str(amt) website = 'http://cs1110.cs.cornell.edu/2019fa/a1?'\ +'src='+src+'&'+'dst='+dst+'&'+'amt='+stramt response = introcs.urlread(website) return response def is_currency(code): """ Returns: True if code is a valid (3 letter code for a) currency It returns False otherwise. Parameter code: the currency code to verify Precondition: code is a string with no spaces or non-letters. """ s1 = currency_response(code,'CNY', '666') s2 = has_error(s1) return bool(s2==False) 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 dst. The value returned has type float. Parameter src: the currency on hand (the LHS) Precondition: src is a string for a valid currency code Parameter dst: the currency to convert to (the RHS) Precondition: dst is a string for a valid currency code Parameter amt: amount of currency to convert Precondition: amt is a float """ s1 = currency_response(src, dst, amt) s2 = get_rhs(s1) result = before_space(s2) result = float(result) return result