Changeset - cc0656dde9b1
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-06-03 22:52:44
brettcsmith@brettcsmith.org
reports: Add Balance.__abs__() method.
2 files changed with 26 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/core.py
Show inline comments
 
"""core.py - Common data classes for reporting functionality"""
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU Affero General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import collections
 
import operator
 

	
 
from decimal import Decimal
 

	
 
import babel.numbers  # type:ignore[import]
 

	
 
from beancount.core import amount as bc_amount
 

	
 
from .. import data
 

	
 
from typing import (
 
    overload,
 
    Any,
 
    Callable,
 
    DefaultDict,
 
    Dict,
 
    Iterable,
 
    Iterator,
 
    List,
 
    Mapping,
 
    Optional,
 
    Sequence,
 
    Set,
 
    Tuple,
 
    Type,
 
    TypeVar,
 
    Union,
 
)
 
from ..beancount_types import (
 
    MetaKey,
 
    MetaValue,
 
)
...
 
@@ -52,48 +54,53 @@ RelatedType = TypeVar('RelatedType', bound='RelatedPostings')
 
class Balance(Mapping[str, data.Amount]):
 
    """A collection of amounts mapped by currency
 

	
 
    Each key is a Beancount currency string, and each value represents the
 
    balance in that currency.
 
    """
 
    __slots__ = ('_currency_map',)
 

	
 
    def __init__(self,
 
                 source: Union[Iterable[Tuple[str, data.Amount]],
 
                               Mapping[str, data.Amount]]=(),
 
    ) -> None:
 
        if isinstance(source, Mapping):
 
            source = source.items()
 
        self._currency_map = {
 
            currency: amount.number for currency, amount in source
 
        }
 

	
 
    def __repr__(self) -> str:
 
        return f"{type(self).__name__}({self._currency_map!r})"
 

	
 
    def __str__(self) -> str:
 
        return self.format()
 

	
 
    def __abs__(self) -> 'Balance':
 
        return type(self)(
 
            (key, bc_amount.abs(amt)) for key, amt in self.items()
 
        )
 

	
 
    def __eq__(self, other: Any) -> bool:
 
        if (self.is_zero()
 
            and isinstance(other, Balance)
 
            and other.is_zero()):
 
            return True
 
        else:
 
            return super().__eq__(other)
 

	
 
    def __neg__(self) -> 'Balance':
 
        return type(self)(
 
            (key, -amt) for key, amt in self.items()
 
        )
 

	
 
    def __getitem__(self, key: str) -> data.Amount:
 
        return data.Amount(self._currency_map[key], key)
 

	
 
    def __iter__(self) -> Iterator[str]:
 
        return iter(self._currency_map)
 

	
 
    def __len__(self) -> int:
 
        return len(self._currency_map)
 

	
 
    def _all_amounts(self,
 
                     op_func: Callable[[DecimalCompat, DecimalCompat], bool],
tests/test_reports_balance.py
Show inline comments
...
 
@@ -106,48 +106,67 @@ def test_eq_zero(balance_map_kwargs, expected):
 
    ({'JPY': 10, 'BRL': 0}, True),
 
    ({'JPY': 10, 'BRL': 20}, True),
 
])
 
def test_ge_zero(balance_map_kwargs, expected):
 
    amounts = testutil.balance_map(**balance_map_kwargs)
 
    balance = core.Balance(amounts.items())
 
    assert balance.ge_zero() == expected
 

	
 
@pytest.mark.parametrize('balance_map_kwargs,expected', [
 
    ({}, True),
 
    ({'USD': 0}, True),
 
    ({'USD': 0, 'EUR': 0}, True),
 
    ({'EUR': -10}, True),
 
    ({'USD': 10, 'EUR': -20}, False),
 
    ({'USD': -10, 'EUR': -20}, True),
 
    ({'JPY': 10}, False),
 
    ({'JPY': 10, 'BRL': 0}, False),
 
    ({'JPY': 10, 'BRL': 20}, False),
 
])
 
def test_le_zero(balance_map_kwargs, expected):
 
    amounts = testutil.balance_map(**balance_map_kwargs)
 
    balance = core.Balance(amounts.items())
 
    assert balance.le_zero() == expected
 

	
 
@pytest.mark.parametrize('balance_map_kwargs', [
 
    {},
 
    {'USD': 0},
 
    {'EUR': 10},
 
    {'JPY': 20, 'BRL': 30},
 
    {'EUR': -15},
 
    {'JPY': -25, 'BRL': -35},
 
    {'JPY': 40, 'USD': 0, 'EUR': -50},
 
])
 
def test_abs(balance_map_kwargs):
 
    amounts = testutil.balance_map(**balance_map_kwargs)
 
    actual = abs(core.Balance(amounts.items()))
 
    assert set(actual) == set(balance_map_kwargs)
 
    abs_amounts = testutil.balance_map(**{
 
        key: abs(number) for key, number in balance_map_kwargs.items()
 
    })
 
    for key in balance_map_kwargs:
 
        assert actual[key] == abs_amounts[key]
 

	
 
@pytest.mark.parametrize('balance_map_kwargs', [
 
    {},
 
    {'USD': 0},
 
    {'EUR': 10},
 
    {'JPY': 20, 'BRL': 30},
 
    {'EUR': -15},
 
    {'JPY': -25, 'BRL': -35},
 
    {'JPY': 40, 'USD': 0, 'EUR': -50},
 
])
 
def test_neg(balance_map_kwargs):
 
    amounts = testutil.balance_map(**balance_map_kwargs)
 
    actual = -core.Balance(amounts.items())
 
    assert set(actual) == set(balance_map_kwargs)
 
    for key in balance_map_kwargs:
 
        assert actual[key] == -amounts[key]
 

	
 
@pytest.mark.parametrize('kwargs1,kwargs2,expected', [
 
    ({}, {}, True),
 
    ({}, {'USD': 0}, True),
 
    ({}, {'EUR': 1}, False),
 
    ({'USD': 1}, {'EUR': 1}, False),
 
    ({'USD': 1}, {'USD': '1.0'}, True),
 
    ({'USD': 1}, {'USD': '1.0', 'EUR': '2.0'}, False),
 
    ({'USD': 1, 'BRL': '2.0'}, {'USD': '1.0', 'EUR': '2.0'}, False),
0 comments (0 inline, 0 general)