Ejemplo n.º 1
0
def currency_response(src="USD", dst="EUR", amt=2.5):
    """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 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"""
    json = introcs.urlread('http://cs1110.cs.cornell.edu/2019fa/a1?src=' +
                           str(src) + '&dst=' + str(dst) + '&amt=' + str(amt))
    return json
Ejemplo n.º 2
0
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

        '{ "src" : "<old-amt>", "dst" : "<new-amt>", "valid" : true,
        "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 "valid" will be followed by the value false.

    Parameter currency_from: the currency on hand (the LHS)
    Precondition: currency_from is a string with no spaces

    Parameter currency_to: the currency to convert to (the RHS)
    Precondition: currency_to is a string with no spaces

    Parameter amount_from: amount of currency to convert
    Precondition: amount_from is a float
    """

    s = 'http://cs1110.cs.cornell.edu/2018fa/a1server.php?from=' + \
    str(currency_from) + '&to=' + str(currency_to) + '&amt='+ str(amount_from)
    r = introcs.urlread(s)
    return r
Ejemplo n.º 3
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
    assert len(src) > 0
    assert type(dst) == str
    assert len(dst) > 0
    assert type(amt) == float  #or assert type(amt) == int

    q = 'https://ecpyfac.ecornell.com/python/currency/fixed?src=src&dst=dst&amt=amt&key=kWOoUqiL9a2IJJ41Ee4BxfAZ7U6FM2qpCZf1VtM8v7Ae'
    readval = introcs.urlread(q)
    return readval
Ejemplo n.º 4
0
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'''

    url = "http://cs1110.cs.cornell.edu/2019fa/a1?src=" + src + "&dst=" + dst + "&amt=" + str(
        amt)
    result = introcs.urlread(url)
    return result
Ejemplo n.º 5
0
def currency_response(currency_from, currency_to, amount_from):
    """
    Returns: Returns a JSON string that is a response to a currency query.
    Parameter currency_from: the currency on hand (the LHS)
    Precondition: currency_from is a string with no spaces

    Parameter currency_to: the currency to convert to (the RHS)
    Precondition: currency_to is a string with no spaces

    Parameter amount_from: amount of currency to convert
    Precondition: amount_from is a float
    """
    return introcs.urlread('http://cs1110.cs.cornell.edu/2019fa/a1?src=' +
                           currency_from + '&dst=' + currency_to + '&amt=' +
                           str(amount_from))
Ejemplo n.º 6
0
def amt_has_error(src, dst, amt):
    """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"""
    temp = introcs.urlread('http://cs1110.cs.cornell.edu/2019fa/a1?src=' +
                           str(src) + '&dst=' + str(dst) + '&amt=' + str(amt))
    a = temp[temp.index('err') + 6:temp.find('"', temp.index('err') + 6)]
    if a == "Currency amount is invalid.":
        return True
    else:
        return False
Ejemplo n.º 7
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 and len(src) == 3, 'Precondition violation'
    assert introcs.count_str(src, " ") == 0, 'Precondition violation'
    assert type(dst) == str and len(dst) == 3, 'Precondition violation'
    assert introcs.count_str(dst, " ") == 0, 'Precondition violation'
    assert type(amt) == float or type(amt) == int, 'Precondition violation'

    url = 'https://ecpyfac.ecornell.com/python/currency/fixed?src=' + src + '&dst='
    url += dst + '&amt=' + str(amt) + '&key=' + APIKEY
    json = introcs.urlread(url)
    return json