fetching and writing rates

This commit is contained in:
Pablo Martin 2024-06-11 20:33:35 +02:00
parent 7012cbec97
commit 41eae45f68
5 changed files with 140 additions and 43 deletions

34
xexe/rate_writing.py Normal file
View file

@ -0,0 +1,34 @@
import csv
import pathlib
from abc import ABC, abstractmethod
from xexe.exchange_rates import ExchangeRates
class RateWriter(ABC):
@abstractmethod
def write_rates(self, rates: ExchangeRates) -> None:
pass
class CSVRateWriter(RateWriter):
def __init__(self, output_file_path: pathlib.Path) -> None:
super().__init__()
self.output_file_path = output_file_path
def write_rates(self, rates: ExchangeRates) -> None:
with open(self.output_file_path, mode="w", newline="") as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(["from_currency", "to_currency", "rate", "rate_date"])
# Write the exchange rate data
for rate in rates._rate_index.values():
csv_writer.writerow(
[
rate.from_currency.value,
rate.to_currency.value,
rate.rate.amount,
rate.rate_date.strftime("%Y-%m-%d"),
]
)