Skip to content

Commit

Permalink
Use TSV file instead of JSON for currencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Nov 28, 2015
1 parent 18197c1 commit fc9f1a5
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 198 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

### [2.4][v2.4] ###

Released XXXX-XX-XX.
Released 2015-11-28.

- Poop
- New money icon
- Update pint, docopt and Alfred-Workflow libraries
- Reorganise code and repo


### [2.3][v2.3] ###
Expand Down
Binary file removed Convert-2.3.alfredworkflow
Binary file not shown.
Binary file added Convert-2.4.alfredworkflow
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ See [CHANGELOG][changelog] for more information.

| Release | Date |
|-------------|----------------|
| [2.4][v2.4] | XXXX-XX-XX |
| [2.4][v2.4] | 2015-11-28 |
| [2.3][v2.3] | 2015-11-26 |
| [2.2][v2.2] | 2015-07-16 |
| 2.1 | Never released |
Expand Down
16 changes: 8 additions & 8 deletions currencies/currencies_yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# Max = 50
symbols_per_request = 50

parse_yahoo_response = re.compile(r'{}(.+)=X'.format(reference_currency)).match
parse_yahoo_response = re.compile(r'{0}(.+)=X'.format(reference_currency)).match


def grouper(n, iterable, fillvalue=None):
Expand Down Expand Up @@ -78,12 +78,12 @@ def load_yahoo_rates(symbols):
if symbol == reference_currency:
count -= 1
continue
parts.append('{}{}=X'.format(reference_currency, symbol))
parts.append('{0}{1}=X'.format(reference_currency, symbol))
query = ','.join(parts)
url = yahoo_base_url.format(query)

# Fetch data
print('Fetching {} ...'.format(url), file=sys.stderr)
print('Fetching {0} ...'.format(url), file=sys.stderr)
r = requests.get(url)
r.raise_for_status()

Expand All @@ -96,7 +96,7 @@ def load_yahoo_rates(symbols):
name, rate = row
m = parse_yahoo_response(name)
if not m:
print('Invalid currency : {}'.format(name), file=sys.stderr)
print('Invalid currency : {0}'.format(name), file=sys.stderr)
continue
symbol = m.group(1)
try:
Expand All @@ -108,7 +108,7 @@ def load_yahoo_rates(symbols):
ycount += 1
# print(row)

assert ycount == count, 'Yahoo! returned {} results, not {}'.format(
assert ycount == count, 'Yahoo! returned {0} results, not {1}'.format(
ycount, count)

return rates
Expand Down Expand Up @@ -158,20 +158,20 @@ def main():
all_currencies = load_currencies(*currency_source_files)

to_check = all_currencies
print('{} currencies to check ...'.format(len(to_check)), file=sys.stderr)
print('{0} currencies to check ...'.format(len(to_check)), file=sys.stderr)

rates = get_exchange_rates(to_check)
for symbol in sorted(rates):
rate = rates[symbol]
if rate == 0:
unknown_currencies.append(symbol)
else:
print('{}\t{}'.format(symbol, rate))
print('{0}\t{1}'.format(symbol, rate))

print('\n\nUnsupported currencies:')
print('-----------------------')
for symbol in unknown_currencies:
print('{}\t{}'.format(symbol, all_currencies[symbol]))
print('{0}\t{1}'.format(symbol, all_currencies[symbol]))

supported_currencies = {k: v for k, v in all_currencies.items()
if k not in unknown_currencies}
Expand Down
10 changes: 7 additions & 3 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from __future__ import print_function, unicode_literals

import json
import csv
import os

# ----------------------------------------------------------------------
Expand All @@ -38,13 +38,17 @@
# ----------------------------------------------------------------------
# Unit definition files
# ----------------------------------------------------------------------
CURRENCIES = {}
CUSTOM_DEFINITIONS_FILENAME = 'unit_definitions.txt'
BUILTIN_UNIT_DEFINITIONS = os.path.join(os.path.dirname(__file__),
CUSTOM_DEFINITIONS_FILENAME)

with open(os.path.join(os.path.dirname(__file__),
'currencies.json'), 'rb') as fp:
CURRENCIES = json.load(fp)
'currencies.tsv'), 'rb') as fp:
reader = csv.reader(fp, delimiter=b'\t')
for sym, rate in reader:
sym = unicode(sym, 'utf-8')
CURRENCIES[sym] = rate

# ----------------------------------------------------------------------
# Help/support URLs
Expand Down
6 changes: 3 additions & 3 deletions src/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def register_units():
ureg.load_definitions(user_definitions)
else: # Copy template to data dir
shutil.copy(
wf.workflowfile('{}.sample'.format(CUSTOM_DEFINITIONS_FILENAME)),
wf.workflowfile('{0}.sample'.format(CUSTOM_DEFINITIONS_FILENAME)),
wf.datafile(CUSTOM_DEFINITIONS_FILENAME))


Expand Down Expand Up @@ -221,11 +221,11 @@ def main(wf):
2))
except UndefinedUnitError as err:
log.critical('Unknown unit : %s', err.unit_names)
error = 'Unknown unit : {}'.format(err.unit_names)
error = 'Unknown unit : {0}'.format(err.unit_names)

except DimensionalityError as err:
log.critical('Invalid conversion : %s', err)
error = "Can't convert from {} {} to {} {}".format(
error = "Can't convert from {0} {1} to {2} {3}".format(
err.units1, err.dim1, err.units2, err.dim2)

except ValueError as err:
Expand Down
159 changes: 0 additions & 159 deletions src/currencies.json

This file was deleted.

31 changes: 20 additions & 11 deletions src/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
# Created on 2014-02-24
#

"""
Script to update exchange rates from Yahoo! in the background.
"""
"""Script to update exchange rates from Yahoo! in the background."""

from __future__ import print_function, unicode_literals

Expand All @@ -31,7 +29,7 @@

log = None

parse_yahoo_response = re.compile(r'{}(.+)=X'.format(REFERENCE_CURRENCY)).match
parse_yahoo_response = re.compile(r'{0}(.+)=X'.format(REFERENCE_CURRENCY)).match


def grouper(n, iterable, fillvalue=None):
Expand Down Expand Up @@ -71,13 +69,13 @@ def load_yahoo_rates(symbols):
if symbol == REFERENCE_CURRENCY:
count -= 1
continue
parts.append('{}{}=X'.format(REFERENCE_CURRENCY, symbol))
parts.append('{0}{1}=X'.format(REFERENCE_CURRENCY, symbol))

query = ','.join(parts)
url = YAHOO_BASE_URL.format(query)

# Fetch data
# log.debug('Fetching {} ...'.format(url))
# log.debug('Fetching {0} ...'.format(url))
r = web.get(url)
r.raise_for_status()

Expand All @@ -92,21 +90,32 @@ def load_yahoo_rates(symbols):
m = parse_yahoo_response(name)

if not m: # Couldn't get symbol
log.error('Invalid currency : {}'.format(name))
log.error('Invalid currency : {0}'.format(name))
ycount += 1
continue
symbol = m.group(1)
rate = float(rate)

if rate == 0: # Yahoo! returns 0.0 as rate for unsupported currencies
log.error('No exchange rate for : {}'.format(name))
# Yahoo! returns 0.0 as rate for unsupported currencies
# NOTE: This has changed. "N/A" is now returned for
# unsupported currencies. That's handled in the script
# that generates the currency list, however: an invalid
# currency should never end up here.

try:
rate = float(rate)
except ValueError:
log.error('No exchange rate for : {0}'.format(name))
continue

if rate == 0:
log.error('No exchange rate for : {0}'.format(name))
ycount += 1
continue

rates[symbol] = rate
ycount += 1

assert ycount == count, 'Yahoo! returned {} results, not {}'.format(
assert ycount == count, 'Yahoo! returned {0} results, not {1}'.format(
ycount, count)

return rates
Expand Down
Loading

0 comments on commit fc9f1a5

Please sign in to comment.