Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Last active December 22, 2015 01:58
Show Gist options
  • Save cameronp98/c5f8af2a235da9aba50f to your computer and use it in GitHub Desktop.
Save cameronp98/c5f8af2a235da9aba50f to your computer and use it in GitHub Desktop.
Currency converter
from json import load
from re import compile
def validate(query):
# amount, start currency, end currency, .dp
preg = compile(r"(\d+\.?\d*?)\s*?([a-zA-Z]{3})\s*?([a-zA-Z]{3})\s*?(\d*?)")
if preg.match(query):
return [i.upper() for i in preg.split(query) if len(i) > 0]
return False
def load_exchange_rates(l="rates.json"):
# load exchange rates
with open("rates.json", "r") as rates_file:
json = load(rates_file)
rates = json["rates"]
rates["base"] = json["base"]
return rates
def main():
query = validate(input("> "))
if query:
query[0] = float(query[0]) # float value
if len(query) > 3: # if .dp is set float it
query[3] = int(query[3])
else:
query.append(5) # else set to default
rates = load_exchange_rates()
if query[1] in rates and query[2] in rates:
usd = query[0] / rates[query[1]]
result = round(usd * rates[query[2]], query[3])
print(result, query[2])
else:
print("Both currencies must be valid")
else:
print("Invalid query")
if __name__ == '__main__':
while True:
main()
{
"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",
"license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",
"timestamp": 1388682062,
"base": "USD",
"rates": {
"AED": 3.67306,
"AFN": 56.139451,
"ALL": 102.287,
"AMD": 407.754002,
"ANG": 1.78898,
"AOA": 97.997351,
"ARS": 6.540306,
"AUD": 1.120609,
"AWG": 1.789825,
"AZN": 0.7841,
"BAM": 1.430071,
"BBD": 2,
"BDT": 77.70406,
"BGN": 1.43028,
"BHD": 0.375078,
"BIF": 1546.279267,
"BMD": 1,
"BND": 1.26805,
"BOB": 6.91737,
"BRL": 2.391847,
"BSD": 1,
"BTC": 0.0012875567,
"BTN": 62.138538,
"BWP": 8.789733,
"BYR": 9538.85,
"BZD": 1.99615,
"CAD": 1.063279,
"CDF": 922.310333,
"CHF": 0.89784,
"CLF": 0.022705,
"CLP": 528.632002,
"CNY": 6.073266,
"COP": 1936.766667,
"CRC": 501.155998,
"CUP": 22.682881,
"CVE": 80.026899,
"CZK": 20.03053,
"DJF": 177.99246,
"DKK": 5.450412,
"DOP": 42.64074,
"DZD": 78.440181,
"EEK": 11.662575,
"EGP": 6.957495,
"ERN": 14.981325,
"ETB": 19.16174,
"EUR": 0.73159,
"FJD": 1.880114,
"FKP": 0.608239,
"GBP": 0.608239,
"GEL": 1.74795,
"GHS": 2.33732,
"GIP": 0.608239,
"GMD": 38.192,
"GNF": 6973.65,
"GTQ": 7.85657,
"GYD": 204.238749,
"HKD": 7.753801,
"HNL": 20.2659,
"HRK": 5.579015,
"HTG": 43.6305,
"HUF": 217.701201,
"IDR": 12157.7,
"ILS": 3.486491,
"INR": 62.18248,
"IQD": 1162.985033,
"IRR": 24749.667967,
"ISK": 115.418999,
"JEP": 0.608239,
"JMD": 106.122499,
"JOD": 0.707782,
"JPY": 104.967401,
"KES": 86.779971,
"KGS": 49.1893,
"KHR": 3992.583516,
"KMF": 358.79217,
"KPW": 900,
"KRW": 1053.011685,
"KWD": 0.282482,
"KYD": 0.827492,
"KZT": 154.517001,
"LAK": 8032.366602,
"LBP": 1503.7,
"LKR": 130.755301,
"LRD": 80.779049,
"LSL": 10.55737,
"LTL": 2.522992,
"LVL": 0.512675,
"LYD": 1.24033,
"MAD": 8.209011,
"MDL": 13.03114,
"MGA": 2242.766667,
"MKD": 44.98572,
"MMK": 987.466275,
"MNT": 1642.666667,
"MOP": 7.99525,
"MRO": 291.621002,
"MTL": 0.683602,
"MUR": 30.22293,
"MVR": 15.37172,
"MWK": 432.66,
"MXN": 13.10622,
"MYR": 3.287194,
"MZN": 29.94395,
"NAD": 10.58889,
"NGN": 159.14,
"NIO": 25.49832,
"NOK": 6.126554,
"NPR": 99.38224,
"NZD": 1.21844,
"OMR": 0.385071,
"PAB": 1,
"PEN": 2.80293,
"PGK": 2.495964,
"PHP": 44.46257,
"PKR": 105.43966,
"PLN": 3.045351,
"PYG": 4605.50332,
"QAR": 3.641591,
"RON": 3.27826,
"RSD": 83.72113,
"RUB": 33.04588,
"RWF": 678.12102,
"SAR": 3.750735,
"SBD": 7.191706,
"SCR": 12.0626,
"SDG": 5.70085,
"SEK": 6.480564,
"SGD": 1.265604,
"SHP": 0.608239,
"SLL": 4347.166667,
"SOS": 1120.0417,
"SRD": 3.283333,
"STD": 17906.667333,
"SVC": 8.76193,
"SYP": 141.197499,
"SZL": 10.58763,
"THB": 32.89692,
"TJS": 4.774225,
"TMT": 2.8501,
"TND": 1.6504,
"TOP": 1.859269,
"TRY": 2.167617,
"TTD": 6.41698,
"TWD": 29.93288,
"TZS": 1577.4,
"UAH": 8.24798,
"UGX": 2531.233333,
"USD": 1,
"UYU": 21.22256,
"UZS": 2202.183317,
"VEF": 6.294525,
"VND": 21112,
"VUV": 96.665,
"WST": 2.325016,
"XAF": 479.897111,
"XAG": 0.05075862,
"XAU": 0.00081578,
"XCD": 2.70156,
"XDR": 0.650863,
"XOF": 480.673999,
"XPF": 87.04516,
"YER": 215.045,
"ZAR": 10.63664,
"ZMK": 5253.075255,
"ZMW": 5.518167,
"ZWL": 322.355006
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment