Function(s) that downloads currency rate between PLN and the given currency in given date. Currencies are downloaded from The Polish National Bank site.
For example, if you need to know what was the currency rate between PLN and EUR in 2010-07-11 you can simply type:
>>> from datetime import date
>>> import nbp
>>> currency_data = nbp.download_exchange_rate(date(2010, 7, 11), 'EUR')
>>> assert currency_data == {
'search_date': '2010-07-11',
'table_no': u'132/A/NBP/2010',
'pub_date': u'2010-07-09',
'currency': {
'name': u'euro',
'rate': 4.0732999999999997,
'code': u'EUR',
},
}
pub_date equals to 2010-07-09 because 2010-07-11 was not the working day, so the algorithm tryies to find first previous working day currency.
Polish National Bank (NBP) provides three types of currency tables: A, B and C. The module uses only A and B. The A table is published every working day and B table only on wednesdays.
The module first search for the currency in the A table, and if there is no results it searches B. If the date you provide doesn't have record in the tables, algtorithm searches for the first previous working day.
Result of the 'download_exchange_rate' algorithm is dictionary with following keys:
{ 'search_date': <date which was passed to the algorithm as string>,
'table_no': <nbp currency table no eg. '132/A/NBP/2010'>,
'pub_date': <publication date of table, it can be diffrent then 'search_date'>,
'currency': {
'name': <name of currency eg. u'euro'>,
'rate': <value of the exchange rate>,
'code': <currency code eg 'EUR' or 'USD'>
}}
The simplest one is
nbp.download_exchange_rateis already explained abovenbp.get_tablecan be used to fetch whole table. Here's an example how you might use it:>>> import nbp >>> table = nbp.get_table(nbp.date(2012, 8, 12), nbp.A)
check if the given currency is in the table:
>>> 'EUR' in table
get given currency from table:
>>> c = table.get('EUR')iterate over currencies:
>>> for c in table: >>> do_sth_with_currency_object(c)
iterate with currencies short name:
>>> for name, currency in table.items(): >>> print name, currency
check table number, url, publication date (might be different then search date):
>>> print table.no >>> print table.url >>> print table.publication_date
To test package simply download it and type
$ python tests.py
- exceptions should be raised if user ask for non-existing currency
1.1 - caching mechanism added