Exemplo n.º 1
0
    def test_get_rates_valid_code(self):
        all_rates = get_rates('USD')

        # Check if return value of get_rates dictionary
        self.assertTrue(isinstance(all_rates, dict))

        # Test at least one rate value returned
        self.assertTrue(len(all_rates.keys()))

        # Test one rate in returned dict is float value
        self.assertTrue(isinstance(all_rates.get('INR'), float))
Exemplo n.º 2
0
    def test_get_rates_valid_code(self):
        all_rates = get_rates('USD')

        # Check if return value of get_rates dictionary
        self.assertTrue(isinstance(all_rates, dict))

        # Test at least one rate value returned
        self.assertTrue(len(all_rates.keys()))

        # Test one rate in returned dict is float value
        self.assertTrue(isinstance(all_rates.get('INR'), float))
Exemplo n.º 3
0
    def test_get_rates_with_date(self):
        date_obj = datetime.datetime.strptime('2010-05-10', "%Y-%m-%d").date()
        all_rates = get_rates('USD', date_obj)

        # Check if return value of get_rates dictionary
        self.assertTrue(isinstance(all_rates, dict))

        # Test at least one rate value returned
        self.assertTrue(len(all_rates.keys()))

        # Test one rate in returned dict is float value
        self.assertTrue(isinstance(all_rates.get('INR'), float))
Exemplo n.º 4
0
def getFxRates():
    """Retrieve the current foreign exchange rates for USD and GBP"""
    forex = []
    forex = get_rates('USD')
    forex = pd.DataFrame([forex], columns=forex.keys())
    #self.forex.loc[:, 'GBP']
    GBPUSD = forex['GBP'].values
    date_obj = datetime.datetime(2020, 5, 17)
    # print(date_obj)
    test = get_rate('USD', 'GBP', date_obj)
    # print(test)
    return GBPUSD
Exemplo n.º 5
0
    def test_get_rates_with_date(self):
        date_obj = datetime.datetime.strptime('2010-05-10', "%Y-%m-%d").date()
        all_rates = get_rates('USD', date_obj)

        # Check if return value of get_rates dictionary
        self.assertTrue(isinstance(all_rates, dict))

        # Test at least one rate value returned
        self.assertTrue(len(all_rates.keys()))

        # Test one rate in returned dict is float value
        self.assertTrue(isinstance(all_rates.get('INR'), float))
Exemplo n.º 6
0
def check_abb(abb):
    if abb not in get_rates('USD'):
        return False
    else:
        return True
def main(args):
    # argument parser setup
    parser = argparse.ArgumentParser(description='currency converter')

    parser.add_argument('-a',
                        '--amount',
                        type=float,
                        help='Amount to convert',
                        required=True)
    parser.add_argument('-i',
                        '--input_currency',
                        type=str,
                        help='Currency to convert',
                        required=True)
    parser.add_argument('-o',
                        '--output_currency',
                        type=str,
                        help='Currency to be converted to',
                        required=False)

    if args:
        arguments = parser.parse_args(args)
    else:
        arguments = parser.parse_args()

    # get currency exchange rates
    converter = forex_python.converter.CurrencyRates()
    output = {}
    # check input arguments, convert symbols to 3 letter code
    if len(arguments.input_currency) > 3:
        fatalError("Invalid input currency!")
    else:
        input_currency = symbol_converter(arguments.input_currency)

    if arguments.output_currency:

        if len(arguments.output_currency) > 3:
            fatal_error("Invalid output currency!")
        else:
            output_currency = symbol_converter(arguments.output_currency)

        # convert input currency to desired output currency
        try:
            output[arguments.output_currency] = converter.convert(
                input_currency, output_currency, arguments.amount)
        except (forex_python.converter.RatesNotAvailableError,
                forex_python.converter.DecimalFloatMismatchError):
            fatal_error("Unable to convert, "
                        "chceck currency format or "
                        "if currency you've entered is supported.")

        output = {
            symbol_converter(key): round(value, 3)
            for key, value in output.items()
        }

    else:
        # convert input currency to all availible currencies
        try:
            currency_dict = converter.get_rates(input_currency)
        except (forex_python.converter.RatesNotAvailableError,
                forex_python.converter.DecimalFloatMismatchError):
            fatal_error("Unable to convert, "
                        "chceck input currency format or "
                        "if currency you've entered is supported.")

        output = {
            key: round(value * arguments.amount, 3)
            for key, value in currency_dict.items()
        }
        del output[input_currency]

    # print or return results in specified format
    result = json.dumps(
        {
            "input": {
                "amount": arguments.amount,
                "currency": input_currency
            },
            "output": output
        },
        indent=4)

    if args:
        return result
    else:
        print(result)