Skip to content

Improved Currency Module #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
FACTS_SOURCE_FILE = 'data/facts.json'
JOKES_SOURCE_FILE = 'data/jokes.json'
QUOTES_SOURCE_FILE = 'data/quotes.json'
CURRENCIES_SOURCE_FILE = 'data/currencies.json'

# API Keys
GOODREADS_ACCESS_TOKEN = '<<GOODREADS_ACCESS_TOKEN>>'
Expand Down
34 changes: 34 additions & 0 deletions data/currencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"USD": "USD", "DOLLAR": "USD", "BUCK": "USD", "US DOLLAR": "USD",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be a more concise way of storing this information?
I don't like USD being repeated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it this way so that it would always require constant time to check for a translation. I could implement it in the following format:
{ "USD": ["DOLLAR", "BUCK", "US DOLLAR"] }

This would make it more concise, but it would also slow it down. The slowdown would be negligible in times with little usage, but if Jarvis ever came under heavy load it may be nice to have faster translation.

What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick search gave this: http://stackoverflow.com/questions/2974022
Can you explore more on this? The ideal solution would have a constant time lookup for a key, while removing the duplication of values.

"GBP": "GBP", "POUND": "GBP", "BRITISH POUND": "GBP", "LB": "GBP",
"IDR": "IDR", "RUPIAH": "IDR", "INDONESIAN RUPIAH": "IDR", "PERAK": "IDR", "RP": "IDR",
"BGN": "BGN", "LEV": "BGN", "BULGARIAN LEV": "BGN",
"ILS": "ILS", "SHEKEL": "ILS", "ISRAELI SHEKEL": "ILS",
"DKK": "DKK", "KRONE": "DKK", "DANISH KRONE": "DKK", "DANISH CROWN": "DKK",
"CAD": "CAD", "LOONIE": "CAD", "CANADIAN DOLLAR": "CAD",
"MXN": "MXN", "PESO": "MXN", "MEXICAN PESO": "MXN",
"HUF": "HUF", "FORINT": "HUF", "HUNGARIAN FORINT": "HUF",
"RON": "RON", "LEI": "RON", "LEU": "RON", "ROMANIAN LEU": "RON", "ROMANIAN LEI": "RON",
"MYR": "MYR", "RINGGIT": "MYR", "MALAYSIAN RINGGIT": "MYR", "MALAYSIAN DOLLAR": "MYR",
"SEK": "SEK", "KRONA": "SEK", "KRONOR": "SEK", "SWEDISH KRONA": "SEK", "SWEDISH KRONOR": "SEK", "SWEDISH CROWNS": "SEK",
"SGD": "SGD", "SINGAPORE DOLLAR": "SGD", "DOLAR SIGNAPURRA": "SGD",
"HKD": "HKD", "HONG KONG DOLLAR": "HKD", "HK DOLLAR": "HKD",
"AUD": "AUD", "AUSTRALIAN DOLLAR": "AUD", "AUSSIE DOLLAR": "AUD",
"CHF": "CHF", "FRANC": "CHF", "SWISS FRANC": "CHF",
"KRW": "KRW", "WON": "KRW", "KOREAN WON": "KRW", "KOREAN REPUBLIC WON": "KRW",
"CNY": "CNY", "RENMINBI": "CNY", "YUAN": "CNY", "CHINESE YUAN": "CNY",
"TRY": "TRY", "TL": "TRY", "TURKISH LIRA": "TRY", "LIRA": "TRY",
"HRK": "HRK", "KUNA": "HRK", "CROATIAN KUNA": "HRK",
"NZD": "NZD", "NEW ZEALAND DOLLAR": "NZD", "KIWI": "NZD",
"THB": "THB", "BAHT": "THB", "THAI BAHT": "THB",
"EUR": "EUR", "EURO": "EUR",
"NOK": "NOK", "NORWEGIAN KRONE": "NOK",
"RUB": "RUB", "ROUBLE": "RUB", "RUBLE": "RUB", "RUSSIAN ROUBLE": "RUB", "RUSSIAN RUBLE": "RUB",
"INR": "INR", "RUPEE": "INR", "INDIAN RUPEE": "INR",
"JPY": "JPY", "YEN": "JPY", "JAPANESE YEN": "JPY",
"CZK": "CZK", "KORUNA": "CZK", "CZECH CROWN": "CZK",
"BRL": "BRL", "REAL": "BRL", "BRAZILIAN REAL": "BRL",
"PLN": "PLN", "ZLOTY": "PLN", "POLISH ZLOTY": "PLN",
"PHP": "PHP", "PHILIPPINE PESO": "PHP",
"ZAR": "ZAR", "RAND": "ZAR", "SOUTH AFRICAN RAND": "ZAR"
}
18 changes: 16 additions & 2 deletions modules/src/currency.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import requests
from templates.text import TextTemplate
import json
import config


def currency_symbol(currency):
with open(config.CURRENCIES_SOURCE_FILE, 'r') as currencies_file:
currencies = json.load(currencies_file)

if currency in currencies:
return currencies[currency]
elif len(currency) > 1 and currency[:-1] in currencies:
return currencies[currency[:-1]]
return currency


def process(input, entities):
output = {}
try:
from_currency = entities['from_currency'][0]['value'].upper()
to_currency = entities['to_currency'][0]['value'].upper()
from_currency = currency_symbol(entities['from_currency'][0]['value'].upper())
to_currency = currency_symbol(entities['to_currency'][0]['value'].upper())
r = requests.get('http://api.fixer.io/latest?base=' + from_currency)
data = r.json()
conversion_rate = data['rates'][to_currency]
Expand Down