Merge lp:~stefanor/ibid/exchange-minor-units into lp:ibid

Proposed by Stefano Rivera on 2011-08-20
Status: Merged
Approved by: Stefano Rivera on 2011-10-23
Approved revision: 1069
Merged at revision: 1036
Proposed branch: lp:~stefanor/ibid/exchange-minor-units
Merge into: lp:ibid
Prerequisite: lp:~stefanor/ibid/exchange-825217
Diff against target: 67 lines (+12/-6)
1 file modified
ibid/plugins/conversions.py (+12/-6)
To merge this branch: bzr merge lp:~stefanor/ibid/exchange-minor-units
Reviewer Review Type Date Requested Status
Jonathan Hitchcock Approve on 2011-10-23
Max Rabkin 2011-08-20 Approve on 2011-08-20
Review via email: mp+72312@code.launchpad.net

Commit message

Use ISO-4217's minor-units data, to output currency in the correct formatting.

Description of the change

Seemed the obvious next step

To post a comment you must log in.
1067. By Stefano Rivera on 2011-08-20

Change default minor_units to 2

1068. By Stefano Rivera on 2011-08-20

Use %.*f instead of two levels of string formatting

Max Rabkin (max-rabkin) :
review: Approve
1069. By Stefano Rivera on 2011-10-16

Merged exchange-825217

Jonathan Hitchcock (vhata) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/conversions.py'
2--- ibid/plugins/conversions.py 2011-10-16 08:32:24 +0000
3+++ ibid/plugins/conversions.py 2011-10-16 08:32:24 +0000
4@@ -316,7 +316,7 @@
5 'http://www.currency-iso.org/dl_iso_table_a1.xml',
6 'conversions/iso4217.xml')
7 document = ElementTree.parse(iso4127_file)
8- # Code -> [Countries..., Currency Name]
9+ # Code -> [Countries..., Currency Name, post-decimal digits]
10 self.currencies = {}
11 # Country -> Code
12 self.country_currencies = {}
13@@ -338,6 +338,10 @@
14 code = currency.findtext('ALPHABETIC_CODE').strip()
15 name = currency.findtext('CURRENCY').strip()
16 place = currency.findtext('ENTITY').strip().title()
17+ try:
18+ minor_units = int(currency.findtext('MINOR_UNIT').strip())
19+ except ValueError:
20+ minor_units = 2
21 if code == '' or code in non_currencies:
22 continue
23 # Fund codes
24@@ -346,7 +350,7 @@
25 if code in self.currencies:
26 self.currencies[code][0].append(place)
27 else:
28- self.currencies[code] = [[place], name]
29+ self.currencies[code] = [[place], name, minor_units]
30 if place in no_country_codes:
31 continue
32 if (code[:2] in self.country_codes
33@@ -411,7 +415,7 @@
34 return "USD"
35 if name == u'pound':
36 return "GBP"
37- for code, (places, currency) in self.currencies.iteritems():
38+ for code, (places, currency, units) in self.currencies.iteritems():
39 if name == currency.lower():
40 return code
41 if name.title() in places:
42@@ -424,7 +428,7 @@
43
44 # Second pass, not requiring exact match:
45 if rough:
46- for code, (places, currency) in self.currencies.iteritems():
47+ for code, (places, currency, units) in self.currencies.iteritems():
48 if name in currency.lower():
49 return code
50 if any(name in place.lower() for place in places):
51@@ -473,12 +477,14 @@
52 u"Whoops, looks like I couldn't make that conversion")
53 return
54
55+ converted = float(amount) * float(last_trade_rate)
56 event.addresponse(
57 u'%(fresult)s %(fcode)s (%(fcurrency)s) = '
58- u'%(tresult)0.2f %(tcode)s (%(tcurrency)s) '
59+ u'%(tresult)s %(tcode)s (%(tcurrency)s) '
60 u'(Last trade rate: %(rate)s, Bid: %(bid)s, Ask: %(ask)s)', {
61 'fresult': amount,
62- 'tresult': float(amount) * float(last_trade_rate),
63+ 'tresult': u'%0.*f' % (self.currencies[canonical_to][2],
64+ converted),
65 'fcurrency': self.currencies[canonical_frm][1],
66 'tcurrency': self.currencies[canonical_to][1],
67 'fcode': canonical_frm,

Subscribers

People subscribed via source and target branches

to all changes: