Giter Club home page Giter Club logo

currencyconverter's Introduction

Currency converter

This is a currency converter that uses historical rates against a reference currency (Euro).

Currency data sources

The default source is the European Central Bank. This is the ECB historical rates for 42 currencies against the Euro since 1999. It can be downloaded here: eurofxref-hist.zip. The converter can use different sources as long as the format is the same.

Installation

You can install directly after cloning:

$ python setup.py install --user

Or use the Python package:

$ pip install --user currencyconverter

Command line tool

After installation, you should have currency_converter in your $PATH:

$ currency_converter 100 USD --to EUR
100.000 USD = 87.512 EUR on 2016-05-06

Python API

Create once the currency converter object:

>>> from currency_converter import CurrencyConverter
>>> c = CurrencyConverter()

Convert from EUR to USD using the last available rate:

>>> c.convert(100, 'EUR', 'USD') # doctest: +SKIP
137.5...

Default target currency is EUR:

>>> c.convert(100, 'EUR')
100.0
>>> c.convert(100, 'USD') # doctest: +SKIP
72.67...

You can change the date of the rate:

>>> from datetime import date # datetime works too
>>> c.convert(100, 'EUR', 'USD', date=date(2013, 3, 21))
129...

Fallbacks

Some rates are missing:

>>> c.convert(100, 'BGN', date=date(2010, 11, 21))
Traceback (most recent call last):
RateNotFoundError: BGN has no rate for 2010-11-21

But we have a fallback mode for those, using a linear interpolation of the closest known rates, as long as you ask for a date within the currency date bounds:

>>> c = CurrencyConverter(fallback_on_missing_rate=True)
>>> c.convert(100, 'BGN', date=date(2010, 11, 21))
51.12...

We also have a fallback mode for dates outside the currency bounds:

>>> c = CurrencyConverter()
>>> c.convert(100, 'EUR', 'USD', date=date(1986, 2, 2))
Traceback (most recent call last):
RateNotFoundError: 1986-02-02 not in USD bounds 1999-01-04/2016-04-29
>>> 
>>> c = CurrencyConverter(fallback_on_wrong_date=True)
>>> c.convert(100, 'EUR', 'USD', date=date(1986, 2, 2)) # fallback to 1999-01-04
117.89...

Other attributes

  • bounds lets you know the first and last available date for each currency
>>> first_date, last_date = c.bounds['USD']
>>> first_date
datetime.date(1999, 1, 4)
>>> last_date # doctest: +SKIP
datetime.date(2016, 11, 14)
  • currencies is a set containing all available currencies
>>> c.currencies # doctest: +SKIP
set(['SGD', 'CAD', 'SEK', 'GBP', ...
>>> 'AAA' in c.currencies
False
>>> c.convert(100, 'AAA')
Traceback (most recent call last):
ValueError: AAA is not a supported currency

Finally, you can use your own currency file, as long as it has the same format (ECB):

# Load the packaged data (might not be up to date)
c = CurrencyConverter()

# Load the up to date full history
c = CurrencyConverter('http://www.ecb.int/stats/eurofxref/eurofxref-hist.zip')

# Load only the latest rates (single day data source)
c = CurrencyConverter('http://www.ecb.europa.eu/stats/eurofxref/eurofxref.zip')

# Load your custom file
c = CurrencyConverter('./path/to/currency/file.csv')

currencyconverter's People

Contributors

alexprengere avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.