0 1 0
master
Brett Smith - 3 months ago 2020-05-20 20:02:47
brettcsmith@brettcsmith.org
historical: Move normalize_rate and _pretty_rate to base Formatter. This reduces the number of method overrides to help readability, and gets rid of the annoying format_rate/pretty_rate distinction.
1 file changed with 27 insertions and 33 deletions:
↑ Collapse Diff ↑
...
 
@@ -43,17 +43,33 @@ class Formatter:
43 43
 
        return decimal.Decimal(amt_s)
44 44
 

	
45 45
 
    def normalize_rate(self, rate, prec=None):
46
 
        return rate
46
 
        if prec is None:
47
 
            prec = self.rate_prec
48
 
        _, digits, exponent = rate.normalize().as_tuple()
49
 
        # Return ``prec`` nonzero digits of precision, if available.
50
 
        prec -= min(0, exponent + len(digits))
51
 
        quant_to = '1.{}'.format('0' * prec)
52
 
        try:
53
 
            qrate = rate.quantize(decimal.Decimal(quant_to))
54
 
        except decimal.InvalidOperation:
55
 
            # The original rate doesn't have that much precision, so use it raw.
56
 
            qrate = rate
57
 
        return qrate.normalize()
47 58
 

	
48
 
    def format_rate(self, rate):
49
 
        return "{:g}".format(rate)
59
 
    def format_rate(self, rate, curr, fmt='{}', prec=None):
60
 
        rate_s = self.format_currency(
61
 
            self.normalize_rate(rate, prec),
62
 
            curr,
63
 
            currency_digits=False,
64
 
        )
65
 
        return fmt.format(rate_s)
50 66
 

	
51 67
 
    def format_rate_pair(self, from_curr, to_curr):
52
 
        from_amt = 1
68
 
        from_amt = decimal.Decimal(1)
53 69
 
        to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr)
54
 
        return "{} {} = {} {}".format(
55
 
            self.format_rate(from_amt), from_curr,
56
 
            self.format_rate(to_amt), to_curr,
70
 
        return "{} = {}".format(
71
 
            self.format_rate(from_amt, from_curr),
72
 
            self.format_rate(to_amt, to_curr),
57 73
 
        )
58 74
 

	
59 75
 
    def format_rate_pair_bidir(self, from_curr, to_curr, sep='\n'):
...
 
@@ -81,28 +97,6 @@ class BeancountFormatter(Formatter):
81 97
 
        else:
82 98
 
            return self.price_rates.convert(from_amt, from_curr, to_curr)
83 99
 

	
84
 
    def normalize_rate(self, rate, prec=None):
85
 
        if prec is None:
86
 
            prec = self.rate_prec
87
 
        _, digits, exponent = rate.normalize().as_tuple()
88
 
        # Return ``prec`` nonzero digits of precision, if available.
89
 
        prec -= min(0, exponent + len(digits))
90
 
        quant_to = '1.{}'.format('0' * prec)
91
 
        try:
92
 
            qrate = rate.quantize(decimal.Decimal(quant_to))
93
 
        except decimal.InvalidOperation:
94
 
            # The original rate doesn't have that much precision, so use it raw.
95
 
            qrate = rate
96
 
        return qrate.normalize()
97
 

	
98
 
    def _pretty_rate(self, fmt, rate, curr):
99
 
        rate_s = self.format_currency(
100
 
            self.normalize_rate(rate),
101
 
            curr,
102
 
            currency_digits=False,
103
 
        )
104
 
        return fmt.format(rate_s)
105
 

	
106 100
 
    def format_rate_pair(self, from_curr, to_curr):
107 101
 
        from_amt = 1
108 102
 
        cost = self.cost_rates.convert(from_amt, from_curr, to_curr)
...
 
@@ -110,11 +104,11 @@ class BeancountFormatter(Formatter):
110 104
 
        if price is None:
111 105
 
            price_s = ''
112 106
 
        else:
113
 
            price_s = self._pretty_rate(self.PRICE_FMT, price, to_curr)
107
 
            price_s = self.format_rate(price, to_curr, self.PRICE_FMT)
114 108
 
        return "{} {} {}{}".format(
115 109
 
            from_amt,
116 110
 
            from_curr,
117
 
            self._pretty_rate(self.COST_FMT, cost, to_curr),
111
 
            self.format_rate(cost, to_curr, self.COST_FMT),
118 112
 
            price_s,
119 113
 
        )
120 114
 

	
...
 
@@ -137,10 +131,10 @@ class BeancountFormatter(Formatter):
137 131
 
        if price is None:
138 132
 
            price_s = ''
139 133
 
        else:
140
 
            price_s = self._pretty_rate(self.PRICE_FMT, price, denomination)
134
 
            price_s = self.format_rate(price, denomination, self.PRICE_FMT)
141 135
 
        return "{} {}{}".format(
142 136
 
            amt_s,
143
 
            self._pretty_rate(self.COST_FMT, cost, denomination),
137
 
            self.format_rate(cost, denomination, self.COST_FMT),
144 138
 
            price_s,
145 139
 
        )
146 140
 

	
0 comments (0 inline, 0 general)