Extract Format#format_quote_amount.
This commit is contained in:
parent
8060c92092
commit
b26b3494d0
3 changed files with 20 additions and 24 deletions
|
@ -9,6 +9,22 @@ class Format:
|
|||
symbol: str = "rightspace"
|
||||
datesep: str = "-"
|
||||
|
||||
def format_quote_amount(self, quote, amount):
|
||||
formatted_amount = self.format_num(amount)
|
||||
|
||||
if self.symbol == "left":
|
||||
qa_parts = [quote, formatted_amount]
|
||||
elif self.symbol == "leftspace":
|
||||
qa_parts = [quote, " ", formatted_amount]
|
||||
elif self.symbol == "right":
|
||||
qa_parts = [formatted_amount, quote]
|
||||
else:
|
||||
qa_parts = [formatted_amount, " ", quote]
|
||||
|
||||
quote_amount = "".join(qa_parts)
|
||||
|
||||
return quote_amount
|
||||
|
||||
def format_num(self, num):
|
||||
parts = f"{num:,}".split(".")
|
||||
parts[0] = parts[0].replace(",", self.thousands)
|
||||
|
|
|
@ -7,16 +7,9 @@ class Beancount(BaseOutput):
|
|||
def format(self, series, source=None, fmt=Format()):
|
||||
lines = []
|
||||
for price in series.prices:
|
||||
|
||||
amount = fmt.format_num(price.amount)
|
||||
# TODO warn if fmt settings make an invalid number
|
||||
|
||||
qa_parts = [amount]
|
||||
if fmt.symbol == "right":
|
||||
qa_parts = qa_parts + [series.quote]
|
||||
else:
|
||||
qa_parts = qa_parts + [" ", series.quote]
|
||||
quote_amount = "".join(qa_parts)
|
||||
quote_amount = fmt.format_quote_amount(series.quote, price.amount)
|
||||
# TODO warn if fmt settings make an invalid number (not . for decimal)
|
||||
# TODO warn if fmt settings make an invalid quote (not right/rightspace)
|
||||
|
||||
date = str(price.date).replace("-", fmt.datesep)
|
||||
lines.append(f"{date} price {series.base} {quote_amount}")
|
||||
|
|
|
@ -8,20 +8,7 @@ class Ledger(BaseOutput):
|
|||
lines = []
|
||||
for price in series.prices:
|
||||
date = str(price.date).replace("-", fmt.datesep)
|
||||
|
||||
amount = fmt.format_num(price.amount)
|
||||
|
||||
qa_parts = [amount]
|
||||
if fmt.symbol == "left":
|
||||
qa_parts = [series.quote] + qa_parts
|
||||
elif fmt.symbol == "leftspace":
|
||||
qa_parts = [series.quote, " "] + qa_parts
|
||||
elif fmt.symbol == "right":
|
||||
qa_parts = qa_parts + [series.quote]
|
||||
else:
|
||||
qa_parts = qa_parts + [" ", series.quote]
|
||||
quote_amount = "".join(qa_parts)
|
||||
|
||||
quote_amount = fmt.format_quote_amount(series.quote, price.amount)
|
||||
lines.append(f"P {date} {fmt.time} {series.base} {quote_amount}")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue