Don't use static methods. Use name by_code instead of bycode.

This commit is contained in:
Chris Berkhout 2021-05-25 15:26:24 +02:00
parent 231be6e62c
commit 03582cf52a
6 changed files with 25 additions and 39 deletions

View file

@ -66,7 +66,7 @@ def cmd_source(args):
if output != "": if output != "":
print(output) print(output)
source = sources.by_id[args.identifier]() source = sources.by_id[args.identifier]
if args.symbols: if args.symbols:
print("\n".join(source.symbols())) print("\n".join(source.symbols()))
@ -84,8 +84,8 @@ def cmd_source(args):
def cmd_fetch(args): def cmd_fetch(args):
source = sources.by_id[args.source]() source = sources.by_id[args.source]
output = outputs.by_type[args.output]() output = outputs.by_type[args.output]
start = args.start or source.start() start = args.start or source.start()
type = args.type or (source.types() + ["unknown"])[0] type = args.type or (source.types() + ["unknown"])[0]

View file

@ -18,7 +18,7 @@ Functions:
current_data_date() -> str current_data_date() -> str
historical_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"] return three.cssselect("ISO_4217")[0].attrib["Pblshd"]
def bycode(): def by_code():
result = {} result = {}
one = etree.fromstring(read_binary("pricehist.resources", "list_one.xml")) one = etree.fromstring(read_binary("pricehist.resources", "list_one.xml"))

View file

@ -6,8 +6,8 @@ from .ledger import Ledger
default = "ledger" default = "ledger"
by_type = { by_type = {
"beancount": Beancount, "beancount": Beancount(),
"csv": CSV, "csv": CSV(),
"gnucash-sql": GnuCashSQL, "gnucash-sql": GnuCashSQL(),
"ledger": Ledger, "ledger": Ledger(),
} }

View file

@ -2,4 +2,4 @@ from .coindesk import CoinDesk
from .coinmarketcap import CoinMarketCap from .coinmarketcap import CoinMarketCap
from .ecb import ECB 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()]}

View file

@ -7,35 +7,28 @@ from pricehist.price import Price
class CoinDesk: class CoinDesk:
@staticmethod def id(self):
def id():
return "coindesk" return "coindesk"
@staticmethod def name(self):
def name():
return "CoinDesk Bitcoin Price Index" return "CoinDesk Bitcoin Price Index"
@staticmethod def description(self):
def description():
return ( return (
"An average of bitcoin prices across leading global exchanges. \n" "An average of bitcoin prices across leading global exchanges. \n"
"Powered by CoinDesk, https://www.coindesk.com/price/bitcoin" "Powered by CoinDesk, https://www.coindesk.com/price/bitcoin"
) )
@staticmethod def source_url(self):
def source_url():
return "https://www.coindesk.com/coindesk-api" return "https://www.coindesk.com/coindesk-api"
@staticmethod def start(self):
def start():
return "2010-07-17" return "2010-07-17"
@staticmethod def types(self):
def types():
return ["close"] return ["close"]
@staticmethod def notes(self):
def notes():
return "" return ""
def symbols(self): def symbols(self):

View file

@ -9,32 +9,25 @@ from pricehist.price import Price
class ECB: class ECB:
@staticmethod def id(self):
def id():
return "ecb" return "ecb"
@staticmethod def name(self):
def name():
return "European Central Bank" return "European Central Bank"
@staticmethod def description(self):
def description():
return "European Central Bank Euro foreign exchange reference rates" return "European Central Bank Euro foreign exchange reference rates"
@staticmethod def source_url(self):
def source_url():
return "https://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html" return "https://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html"
@staticmethod def start(self):
def start():
return "1999-01-04" return "1999-01-04"
@staticmethod def types(self):
def types():
return ["reference"] return ["reference"]
@staticmethod def notes(self):
def notes():
return "" return ""
def symbols(self): def symbols(self):
@ -42,7 +35,7 @@ class ECB:
root = etree.fromstring(data) root = etree.fromstring(data)
nodes = root.cssselect("[currency]") nodes = root.cssselect("[currency]")
currencies = sorted(set([n.attrib["currency"] for n in nodes])) 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] pairs = [f"EUR/{c} Euro against {iso[c].name}" for c in currencies]
return pairs return pairs