From 1ec2e276762ba76067fc35a45d165ad06fe9ecc7 Mon Sep 17 00:00:00 2001 From: oittaa Date: Fri, 19 Nov 2021 09:30:27 +0100 Subject: [PATCH] Tolerate broken CSV files (#62) --- ibkr_report/exchangerates.py | 7 +++---- test-data/eurofxref-broken.csv | 3 +++ test.py | 8 ++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 test-data/eurofxref-broken.csv diff --git a/ibkr_report/exchangerates.py b/ibkr_report/exchangerates.py index 5e423a93..d2bfbad7 100644 --- a/ibkr_report/exchangerates.py +++ b/ibkr_report/exchangerates.py @@ -68,10 +68,9 @@ def add_to_exchange_rates(self, rates_file: Iterable[bytes]) -> None: if currencies and re.match(r"^\d\d\d\d-\d\d-\d\d$", items[0]): # And the following rows like "2015-01-20,1.1579,137.37,..." date_rates = {} - for key, val in enumerate(items): - if key == 0 or not currencies[key] or not is_number(val): - continue - date_rates[currencies[key]] = val + for cur, val in zip(currencies, items): + if is_number(val): + date_rates[cur] = val if date_rates: rates[items[0]] = date_rates log.debug("Adding currency data from %d rows.", len(rates)) diff --git a/test-data/eurofxref-broken.csv b/test-data/eurofxref-broken.csv new file mode 100644 index 00000000..1f34b266 --- /dev/null +++ b/test-data/eurofxref-broken.csv @@ -0,0 +1,3 @@ +Date,USD +2021-10-27 +2021-10-26,1.1618,132.47 diff --git a/test.py b/test.py index 948f3861..c067d3f9 100644 --- a/test.py +++ b/test.py @@ -274,6 +274,14 @@ def test_far_in_the_future(self): with self.assertRaises(ValueError): self.rates.get_rate("USD", "CAD", "2500-01-01") + def test_broken_input(self): + with open("test-data/eurofxref-broken.csv", "rb") as file: + self.rates.add_to_exchange_rates(file) + eur_usd = self.rates.get_rate("EUR", "USD", "2021-10-26") + self.assertEqual(eur_usd, Decimal("1.1618")) + eur_usd = self.rates.get_rate("EUR", "USD", "2021-10-25") + self.assertEqual(eur_usd, Decimal("1.1603")) + if __name__ == "__main__": unittest.main()