Re: [Tutor] PyCountry currency formatting woes
Thank you all, babel works just fine. I also tried ccy, which isn't bad either - except that it returns non-unicode currency letters for countries in the Eurozone. On Mon, May 5, 2014 at 10:10 AM, Peter Otten <__pete...@web.de> wrote: > Sithembewena Lloyd Dube wrote: > > > Thanks, i was actually getting the error information to update the post. > > Apoligies to waste your time posting here - I could not find an > > appropriate PyCountry discussion list and my next best bet seemed to be a > > Python users' list. > > > > For those who care to look, the error is as follows (a concise example > > from an interactive shell: > > > > import pycountry > > country = pycountry.countries.get(alpha2='DE') > > currency = pycountry.currencies.get(numeric=country.numeric) > > Traceback (most recent call last): > > File "", line 1, in > > File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get > > return self.indices[field][value] > > KeyError: '276' > > > > The obvious issue here is that the pycountry.countries collection does > not > > contain a currency with a numeric of 276 (Germany's numeric) - yet it > does > > contain the Euro. Any ideas as to what the way around this may be? > > It looks like the development version of babel > > > http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies > > can do what you want: > > $ LANG=en_US.UTF-8 python > Python 2.7.5+ (default, Feb 27 2014, 19:37:08) > [GCC 4.8.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import babel.numbers as bn > >>> bn.get_territory_currencies("DE") > ['EUR'] > >>> print bn.format_currency(1.234, "EUR") > €1.23 > >>> print bn.format_currency(1.234, "EUR", locale="DE") > 1,23 € > >>> import babel > >>> babel.__version__ > '2.0-dev' > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Regards, Sithu Lloyd Dube ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyCountry currency formatting woes
Sithembewena Lloyd Dube wrote: > Thanks, i was actually getting the error information to update the post. > Apoligies to waste your time posting here - I could not find an > appropriate PyCountry discussion list and my next best bet seemed to be a > Python users' list. > > For those who care to look, the error is as follows (a concise example > from an interactive shell: > > import pycountry > country = pycountry.countries.get(alpha2='DE') > currency = pycountry.currencies.get(numeric=country.numeric) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get > return self.indices[field][value] > KeyError: '276' > > The obvious issue here is that the pycountry.countries collection does not > contain a currency with a numeric of 276 (Germany's numeric) - yet it does > contain the Euro. Any ideas as to what the way around this may be? It looks like the development version of babel http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies can do what you want: $ LANG=en_US.UTF-8 python Python 2.7.5+ (default, Feb 27 2014, 19:37:08) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import babel.numbers as bn >>> bn.get_territory_currencies("DE") ['EUR'] >>> print bn.format_currency(1.234, "EUR") €1.23 >>> print bn.format_currency(1.234, "EUR", locale="DE") 1,23 € >>> import babel >>> babel.__version__ '2.0-dev' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyCountry currency formatting woes
On 04/05/14 21:25, Sithembewena Lloyd Dube wrote: currency = pycountry.currencies.get(numeric=country.numeric) Have you tried using locales? import locale as loc loc.setlocale(loc.LC_ALL,'') # selects default - do this at start ... print(loc.currency(myNum_here)) I don't know the module you reference but locale is the standard library option... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyCountry currency formatting woes
Thanks for this response, this is exactly what I needed to know. On Mon, May 5, 2014 at 6:26 AM, Marc Tompkins wrote: > On Sun, May 4, 2014 at 1:55 PM, Sithembewena Lloyd Dube > wrote: > >> Thanks, i was actually getting the error information to update the post. >> Apoligies to waste your time posting here - I could not find an appropriate >> PyCountry discussion list and my next best bet seemed to be a Python users' >> list. >> >> > You also posted on StackOverflow; I just answered you there. In short: > the currency numeric is not guaranteed to be the same as the country > numeric (in the case of the Euro, how could it possibly be?) The numeric > for DE is 276; the numeric for the Euro is 978. Obviously they don't match. > > PyCountry is a wrapper around some tables provided by Debian; those tables > don't include a country/currency mapping. You can find those mapping > tables at > http://www.currency-iso.org/en/home/tables/table-a1.html > and roll your own wrapper; I'm sure it's been done a thousand times > before, but I'm not aware of a Python package that does this. > -- Regards, Sithu Lloyd Dube ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyCountry currency formatting woes
On Sun, May 4, 2014 at 1:55 PM, Sithembewena Lloyd Dube wrote: > Thanks, i was actually getting the error information to update the post. > Apoligies to waste your time posting here - I could not find an appropriate > PyCountry discussion list and my next best bet seemed to be a Python users' > list. > > You also posted on StackOverflow; I just answered you there. In short: the currency numeric is not guaranteed to be the same as the country numeric (in the case of the Euro, how could it possibly be?) The numeric for DE is 276; the numeric for the Euro is 978. Obviously they don't match. PyCountry is a wrapper around some tables provided by Debian; those tables don't include a country/currency mapping. You can find those mapping tables at http://www.currency-iso.org/en/home/tables/table-a1.html and roll your own wrapper; I'm sure it's been done a thousand times before, but I'm not aware of a Python package that does this. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyCountry currency formatting woes
Thanks, i was actually getting the error information to update the post. Apoligies to waste your time posting here - I could not find an appropriate PyCountry discussion list and my next best bet seemed to be a Python users' list. For those who care to look, the error is as follows (a concise example from an interactive shell: import pycountry country = pycountry.countries.get(alpha2='DE') currency = pycountry.currencies.get(numeric=country.numeric) Traceback (most recent call last): File "", line 1, in File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get return self.indices[field][value] KeyError: '276' The obvious issue here is that the pycountry.countries collection does not contain a currency with a numeric of 276 (Germany's numeric) - yet it does contain the Euro. Any ideas as to what the way around this may be? On Sun, May 4, 2014 at 10:25 PM, Sithembewena Lloyd Dube wrote: > Hi everyone, > > I have a function which accepts an alpha2 country code and a price string, > where the aim is to get the country's currency and use the currency.letter > property of that currency to format the supplied price string. > > The above works fine so far - yet it falls over when called with Germany > as the country as follows: > > currency = pycountry.currencies.get(numeric=country.numeric) > > The function implementation is as follows: > > def formatPrice(self, alpha2CountryCode, price): > """ > @param alpha2CountryCode: The 2-character country code for which > to format the price value > @param price: The price value as a string > @return: A string representing the formatted monetary value for > this country for this price. > > #Get country by alpha2 code > country = pc.countries.get(alpha2=alpha2CountryCode.upper()) > > #Get currency by country's numeric and format price > currency = pc.currencies.get(numeric=country.numeric) > letter = currency.letter > formattedCurrency = "%s %s" % (letter, price) > > return formattedCurrency > > Any ideas as to what the issue may be? > > Thanks :) > > -- > Regards, > Sithu Lloyd Dube > -- Regards, Sithu Lloyd Dube ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyCountry currency formatting woes
On 04/05/2014 21:25, Sithembewena Lloyd Dube wrote: Hi everyone, I have a function which accepts an alpha2 country code and a price string, where the aim is to get the country's currency and use the currency.letter property of that currency to format the supplied price string. The above works fine so far - yet it falls over when called with Germany as the country as follows: currency = pycountry.currencies.get(numeric=country.numeric) The function implementation is as follows: def formatPrice(self, alpha2CountryCode, price): """ @param alpha2CountryCode: The 2-character country code for which to format the price value @param price: The price value as a string @return: A string representing the formatted monetary value for this country for this price. #Get country by alpha2 code country = pc.countries.get(alpha2=alpha2CountryCode.upper()) #Get currency by country's numeric and format price currency = pc.currencies.get(numeric=country.numeric) letter = currency.letter formattedCurrency = "%s %s" % (letter, price) return formattedCurrency Any ideas as to what the issue may be? Thanks :) -- Regards, Sithu Lloyd Dube Sorry but this list is aimed at people learning the core Python language, not a third party module, which I've never heard of incidentally. You might get lucky if some regular here has used the package, but even then "it falls over" isn't too helpful. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] PyCountry currency formatting woes
Hi everyone, I have a function which accepts an alpha2 country code and a price string, where the aim is to get the country's currency and use the currency.letter property of that currency to format the supplied price string. The above works fine so far - yet it falls over when called with Germany as the country as follows: currency = pycountry.currencies.get(numeric=country.numeric) The function implementation is as follows: def formatPrice(self, alpha2CountryCode, price): """ @param alpha2CountryCode: The 2-character country code for which to format the price value @param price: The price value as a string @return: A string representing the formatted monetary value for this country for this price. #Get country by alpha2 code country = pc.countries.get(alpha2=alpha2CountryCode.upper()) #Get currency by country's numeric and format price currency = pc.currencies.get(numeric=country.numeric) letter = currency.letter formattedCurrency = "%s %s" % (letter, price) return formattedCurrency Any ideas as to what the issue may be? Thanks :) -- Regards, Sithu Lloyd Dube ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor