Skip to content

Commit 064e76e

Browse files
author
ravi
committed
converting amount to other currency
2 parents eac5959 + 7311e9b commit 064e76e

5 files changed

Lines changed: 107 additions & 18 deletions

File tree

.gitignore

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,63 @@
1-
env
2-
*.pyc
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
38
*.*~
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
55+
# Sphinx documentation
56+
docs/_build/
57+
58+
# PyBuilder
59+
target/
60+
61+
#Ipython Notebook
62+
.ipynb_checkpoints
63+
env

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 MicroPyramid
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# forex-python
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@ def __init__(self):
2424
def _source_url(self):
2525
return "http://api.fixer.io/"
2626

27-
def _validate_date(self, date_str):
27+
# def _validate_date(self, date_str):
28+
29+
def _get_date_string(self, date_str):
30+
if date_str is None:
31+
return 'latest'
2832
try:
2933
datetime.datetime.strptime(date_str, '%Y-%m-%d')
34+
return date_str
3035
except ValueError:
3136
raise ValueError("Incorrect date String, date_str should be YYYY-MM-DD")
3237

33-
38+
3439
class CurrencyRates(Common):
3540

3641
def get_rates(self, base_cur, date_str=None):
37-
38-
if date_str is None:
39-
date_str = 'latest'
40-
else:
41-
self._validate_date(date_str)
42-
date_str = str(date_str)
43-
42+
date_str = self._get_date_string(date_str)
4443
payload = {'base': base_cur}
4544
source_url = self._source_url() + date_str
4645
response = requests.get(source_url, params=payload)
@@ -50,12 +49,7 @@ def get_rates(self, base_cur, date_str=None):
5049
raise RatesNotAvailableError("Currency Rates Source Not Ready")
5150

5251
def get_rate(self, base_cur, dest_cur, date_str=None):
53-
if date_str is None:
54-
date_str = 'latest'
55-
else:
56-
self._validate_date(date_str)
57-
date_str = str(date_str)
58-
52+
date_str = self._get_date_string(date_str)
5953
payload = {'base': base_cur, 'symbols': dest_cur}
6054
source_url = self._source_url() + date_str
6155
response = requests.get(source_url, params=payload)
@@ -65,4 +59,17 @@ def get_rate(self, base_cur, dest_cur, date_str=None):
6559
return rate
6660
raise RatesNotAvailableError("Currency Rates Source Not Ready")
6761

68-
# def convert(self, base_cur, dest_cur, amount, date_str=None)
62+
def convert(self, base_cur, dest_cur, amount, date_str=None):
63+
date_str = self._get_date_string(date_str)
64+
payload = {'base': base_cur, 'symbols': dest_cur}
65+
source_url = self._source_url() + date_str
66+
response = requests.get(source_url, params=payload)
67+
if response.status_code == 200:
68+
print response.json()
69+
rate = response.json().get('rates', {}).get(dest_cur, None)
70+
if not rate:
71+
raise RatesNotAvailableError("Currency {0} => {1} rate not available for Date {2}.".format(
72+
source_url, dest_cur, date_str))
73+
converted_amount = rate * amount
74+
return converted_amount
75+
raise RatesNotAvailableError("Currency Rates Source Not Ready")

0 commit comments

Comments
 (0)