Skip to content

Commit 6c7e02b

Browse files
committed
Updated Main.py, Readme, Requirements, Docstrings, Tests Files
1 parent dbca250 commit 6c7e02b

File tree

7 files changed

+138
-16
lines changed

7 files changed

+138
-16
lines changed

Currency Script/main.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1-
# TODO IMPORT API / CONVERTER / CURRENCY MODULES
2-
# TODO UPDATE / POPULATE MAIN TO ENSURE THERE IS A INTUITIVE SIMPLE UI
1+
"""
2+
main.py - Simple CLI interface for Currency Converter
3+
4+
Prompts user for input currencies and amount, and displays the converted value.
5+
"""
6+
7+
from converter import CurrencyConverter
8+
9+
def main():
10+
"""
11+
Run the interactive currency converter CLI loop.
12+
Prompts user for source and target currencies and amount to convert.
13+
Continues until the user types 'no' to exit.
14+
"""
15+
print("Welcome to the Currency Converter\n")
16+
17+
converter = CurrencyConverter()
18+
19+
while True:
20+
try:
21+
from_currency = input("Enter source currency code (e.g., AUD): ").strip().upper()
22+
to_currency = input("Enter target currency code (e.g., USD): ").strip().upper()
23+
amount = float(input(f"Enter amount in {from_currency}: "))
24+
25+
converted = converter.convert(from_currency, to_currency, amount)
26+
print(f"\n{amount} {from_currency} = {converted} {to_currency}\n")
27+
28+
except ValueError as e:
29+
print(f"❌ Error: {e}\n")
30+
31+
# Ask to convert another or exit
32+
again = input("Do you want to convert another amount? (yes/no): ").strip().lower()
33+
if again != "yes":
34+
print("Thank you for using the Currency Converter. Goodbye!")
35+
break
36+
37+
if __name__ == "__main__":
38+
main()

Currency Script/readme.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1-
# TODO - UPDATE README
1+
Currency Converter
2+
==================
3+
4+
A simple Python CLI application that converts an amount from one currency to another
5+
using live exchange rates fetched from an open API.
6+
7+
Modules:
8+
--------
9+
10+
1. **api_handler.py**
11+
- Handles fetching exchange rate data from the online API.
12+
13+
2. **currencies.py**
14+
- Provides utility functions to list supported currencies and validate currency codes.
15+
16+
3. **converter.py**
17+
- Contains the core logic to convert between currencies using the exchange rates.
18+
19+
4. **main.py**
20+
- CLI interface where users input the currencies and amount to convert.
21+
22+
5. **test_*.py**
23+
- Unit tests for each module to ensure functionality.
24+
25+
Requirements:
26+
-------------
27+
- Python 3.7+
28+
- `requests` library
29+
30+
Install dependencies:
31+
---------------------
32+
Run the following command to install dependencies:
33+
- pip install -r requirements.txt
34+
35+
How to Use:
36+
-----------
37+
Run the main script from the terminal:
38+
39+
Follow the prompts to enter:
40+
- Source currency code (e.g., USD)
41+
- Target currency code (e.g., AUD)
42+
- Amount to convert
43+
44+
The app will display the converted amount and allow you to repeat or exit.
45+
46+
Testing:
47+
--------
48+
To run tests:
49+
- python -m unittest discover
50+
51+
Note:
52+
-----
53+
The conversion is based on live exchange rates retrieved from:
54+
https://open.er-api.com/v6/latest
55+
56+
Make sure you are connected to the internet when running the app.
57+

Currency Script/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# TODO - UPDATE REQUIREMENTS TO NOTE INSTALLS ARE NOTED
1+
requests

Currency Script/setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# UPDATE SETUP TO NOTE SET UP / PACKAGES
1+
from setuptools import setup
2+
3+
setup(
4+
name="currency_converter",
5+
version="0.1",
6+
install_requires=[
7+
"requests"
8+
],
9+
)

Currency Script/src/api_handler.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import requests
22

33
def get_exchange_data(api_url: str = "https://open.er-api.com/v6/latest") -> dict:
4-
"""Fetch latest exchange data from the API."""
4+
"""
5+
Retrieve the latest foreign exchange rates from the specified API.
6+
7+
Args:
8+
api_url (str): Endpoint to fetch exchange data from. Defaults to the open.er-api URL.
9+
10+
Returns:
11+
dict: A dictionary containing the base currency, timestamp, and exchange rates.
12+
13+
Raises:
14+
Exception: If the API request fails or returns a non-200 status.
15+
"""
516
response = requests.get(api_url)
617
if response.status_code != 200:
718
raise Exception(f"API request failed with status {response.status_code}")
819

920
data = response.json()
10-
# Ensure response was successful
11-
if data.get("result") != "success":
12-
raise Exception(f"API returned error: {data.get('error-type', 'Unknown error')}")
1321

1422
return data # Includes 'base_code', 'time_last_update_utc', and 'rates'
1523

Currency Script/src/converter.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""
2-
CurrencyConverter: Converts an amount from one currency to another using live exchange rates.
3-
"""
4-
51
from api_handler import get_exchange_data
62

73
class CurrencyConverter:
@@ -35,7 +31,8 @@ def convert(self, from_currency: str, to_currency: str, amount: float) -> float:
3531
converted_amount = round(amount_in_base * self.rates[to_currency], 2)
3632
return converted_amount
3733

38-
# --- DEBUG / MANUAL TEST ---
34+
# --- DEBUG / MANUAL TEST SECTION ---
35+
# This section runs only when you run this file directly (not when imported elsewhere)
3936
if __name__ == "__main__":
4037
print("Running manual test for CurrencyConverter...\n")
4138

Currency Script/src/currencies.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
from api_handler import get_exchange_data
22

3-
# Returns a list of all supported currency codes.
43
def get_supported_currencies(rates):
4+
"""
5+
Return a list of supported currency codes from the rates dictionary.
6+
7+
Args:
8+
rates (dict): Dictionary of currency codes and their exchange rates.
9+
10+
Returns:
11+
list: List of supported currency codes (e.g., ['USD', 'EUR']).
12+
"""
513
return list(rates.keys())
614

7-
# Checks if a currency code is supported.
815
def is_valid_currency(currency_code, rates):
16+
"""
17+
Check if the given currency code is valid and supported.
18+
19+
Args:
20+
currency_code (str): Currency code to validate (e.g., 'USD').
21+
rates (dict): Dictionary of available exchange rates.
22+
23+
Returns:
24+
bool: True if the currency code exists in the rates, False otherwise.
25+
"""
926
return currency_code.upper() in rates
1027

1128
# --- DEBUG / MANUAL TEST SECTION ---

0 commit comments

Comments
 (0)