From 03582cf52a9cb69ee6a1e9a48cdaa20d6ec66311 Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Tue, 25 May 2021 15:26:24 +0200 Subject: [PATCH] Don't use static methods. Use name by_code instead of bycode. --- src/pricehist/cli.py | 6 +++--- src/pricehist/isocurrencies.py | 4 ++-- src/pricehist/outputs/__init__.py | 8 ++++---- src/pricehist/sources/__init__.py | 2 +- src/pricehist/sources/coindesk.py | 21 +++++++-------------- src/pricehist/sources/ecb.py | 23 ++++++++--------------- 6 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/pricehist/cli.py b/src/pricehist/cli.py index 4f200f9..feb97ca 100644 --- a/src/pricehist/cli.py +++ b/src/pricehist/cli.py @@ -66,7 +66,7 @@ def cmd_source(args): if output != "": print(output) - source = sources.by_id[args.identifier]() + source = sources.by_id[args.identifier] if args.symbols: print("\n".join(source.symbols())) @@ -84,8 +84,8 @@ def cmd_source(args): def cmd_fetch(args): - source = sources.by_id[args.source]() - output = outputs.by_type[args.output]() + source = sources.by_id[args.source] + output = outputs.by_type[args.output] start = args.start or source.start() type = args.type or (source.types() + ["unknown"])[0] diff --git a/src/pricehist/isocurrencies.py b/src/pricehist/isocurrencies.py index 0871c31..4f9ffbe 100644 --- a/src/pricehist/isocurrencies.py +++ b/src/pricehist/isocurrencies.py @@ -18,7 +18,7 @@ Functions: current_data_date() -> str historical_data_date() -> str - bycode() -> dict[str, ISOCurrency] + by_code() -> dict[str, ISOCurrency] """ @@ -50,7 +50,7 @@ def historical_data_date(): return three.cssselect("ISO_4217")[0].attrib["Pblshd"] -def bycode(): +def by_code(): result = {} one = etree.fromstring(read_binary("pricehist.resources", "list_one.xml")) diff --git a/src/pricehist/outputs/__init__.py b/src/pricehist/outputs/__init__.py index bc6836e..96381de 100644 --- a/src/pricehist/outputs/__init__.py +++ b/src/pricehist/outputs/__init__.py @@ -6,8 +6,8 @@ from .ledger import Ledger default = "ledger" by_type = { - "beancount": Beancount, - "csv": CSV, - "gnucash-sql": GnuCashSQL, - "ledger": Ledger, + "beancount": Beancount(), + "csv": CSV(), + "gnucash-sql": GnuCashSQL(), + "ledger": Ledger(), } diff --git a/src/pricehist/sources/__init__.py b/src/pricehist/sources/__init__.py index 696e828..8bf1acf 100644 --- a/src/pricehist/sources/__init__.py +++ b/src/pricehist/sources/__init__.py @@ -2,4 +2,4 @@ from .coindesk import CoinDesk from .coinmarketcap import CoinMarketCap from .ecb import ECB -by_id = {CoinDesk.id(): CoinDesk, CoinMarketCap.id(): CoinMarketCap, ECB.id(): ECB} +by_id = {source.id(): source for source in [CoinDesk(), CoinMarketCap(), ECB()]} diff --git a/src/pricehist/sources/coindesk.py b/src/pricehist/sources/coindesk.py index f90ee19..21f51de 100644 --- a/src/pricehist/sources/coindesk.py +++ b/src/pricehist/sources/coindesk.py @@ -7,35 +7,28 @@ from pricehist.price import Price class CoinDesk: - @staticmethod - def id(): + def id(self): return "coindesk" - @staticmethod - def name(): + def name(self): return "CoinDesk Bitcoin Price Index" - @staticmethod - def description(): + def description(self): return ( "An average of bitcoin prices across leading global exchanges. \n" "Powered by CoinDesk, https://www.coindesk.com/price/bitcoin" ) - @staticmethod - def source_url(): + def source_url(self): return "https://www.coindesk.com/coindesk-api" - @staticmethod - def start(): + def start(self): return "2010-07-17" - @staticmethod - def types(): + def types(self): return ["close"] - @staticmethod - def notes(): + def notes(self): return "" def symbols(self): diff --git a/src/pricehist/sources/ecb.py b/src/pricehist/sources/ecb.py index ecf6377..97e4260 100644 --- a/src/pricehist/sources/ecb.py +++ b/src/pricehist/sources/ecb.py @@ -9,32 +9,25 @@ from pricehist.price import Price class ECB: - @staticmethod - def id(): + def id(self): return "ecb" - @staticmethod - def name(): + def name(self): return "European Central Bank" - @staticmethod - def description(): + def description(self): return "European Central Bank Euro foreign exchange reference rates" - @staticmethod - def source_url(): + def source_url(self): return "https://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html" - @staticmethod - def start(): + def start(self): return "1999-01-04" - @staticmethod - def types(): + def types(self): return ["reference"] - @staticmethod - def notes(): + def notes(self): return "" def symbols(self): @@ -42,7 +35,7 @@ class ECB: root = etree.fromstring(data) nodes = root.cssselect("[currency]") currencies = sorted(set([n.attrib["currency"] for n in nodes])) - iso = isocurrencies.bycode() + iso = isocurrencies.by_code() pairs = [f"EUR/{c} Euro against {iso[c].name}" for c in currencies] return pairs