commit 1af8e7036cc73edb4c4050ea2ca20010a26c9b88
parent 463f1768a7dea1904b8468989761f443eda3dafb
Author: lash <dev@holbrook.no>
Date: Wed, 25 Mar 2026 16:39:06 -0600
Add balancer to entry
Diffstat:
4 files changed, 34 insertions(+), 95 deletions(-)
diff --git a/dummy/tests/balance.py b/dummy/tests/balance.py
@@ -41,6 +41,24 @@ class TestBalancer(unittest.TestCase):
self.assertTrue(o.balanced())
+ def test_balancer_parts_simple(self):
+ o = Balancer(self.uidx)
+ self.uidx.set_rate('BAR', 230000000)
+ src = EntryPart('BAR', 'income', 'baz', 420000, debit=True)
+ o.apply_part(src)
+ dst = EntryPart('FOO', 'asset', 'foo', 9660)
+ o.apply_part(dst)
+ self.assertTrue(o.balanced())
+
+
+ def test_balancer_parts_simple(self):
+ o = Balancer(self.uidx)
+ self.uidx.set_rate('BAR', 230000000)
+ src = EntryPart('BAR', 'income', 'baz', 420000, debit=True)
+ o.apply_part(src)
+ dst = EntryPart('FOO', 'asset', 'foo', 9660)
+ o.apply_part(dst)
+ self.assertTrue(o.balanced())
if __name__ == '__main__':
unittest.main()
diff --git a/dummy/tests/entry.py b/dummy/tests/entry.py
@@ -136,7 +136,16 @@ class TestEntry(unittest.TestCase):
o.add_part(dst_two)
wallet = DemoWallet()
data = o.wrap(wallet=wallet)
- print(list(o))
+
+
+ def test_entry_balance(self):
+ dst_one = EntryPart('FOO', 'asset', 'foo', 1337)
+ dst_two = EntryPart('FOO', 'asset', 'bar', 42)
+ src = EntryPart('FOO', 'income', 'baz', 1337+42, debit=True)
+ o = Entry(42, datetime.datetime.strptime('2025-11-11', '%Y-%m-%d'), parent=self.parent, ref=self.ref, description=self.description, tx_datereg=self.dtreg, unitindex=self.uidx)
+ o.add_part(src, debit=True)
+ o.add_part(dst_one)
+ o.add_part(dst_two)
if __name__ == '__main__':
diff --git a/dummy/usawa/balance.py b/dummy/usawa/balance.py
@@ -49,96 +49,3 @@ class Balancer:
self.r -= amount
else:
self.r += amount
-
-
-#class Balancer:
-#
-# def __init__(self, uidx, entry=None, base=None):
-# self.r = 0
-# self.uidx = uidx
-# self.max_precision = 0
-# self.ex = {}
-# self.running = {}
-# self.base = base
-# if entry != None:
-# self.scan(entry)
-# self.process_entry(entry)
-#
-#
-# def scan_units(self, uidx):
-#
-#
-# def scan(self, entry):
-# base = None
-# for part in entry:
-# precision = self.uidx.get(part.unit)
-# if precision > self.max_precision:
-# self.max_precision = precision
-# self.ex[part.unit] = 1000000000
-# if base == None:
-# base = part.unit
-# continue
-# if base != part.unit:
-# if self.base == None:
-# logg.warning('base is not set with entry with different units')
-# logg.debug('have max precision {}'.format(self.max_precision))
-# self.base = base
-#
-#
-# def init_unit(self, unit):
-# self.ex[unit] = 100000000
-# self.
-#
-#
-# def set_rate(self, unit, rate):
-# if unit == self.base:
-# raise ValueError('base rate against itself')
-# self.ex[unit] = rate
-#
-#
-# def process_entry(self, entry):
-# for part in entry:
-# self.apply_part(part)
-#
-#
-# def apply_part(self, part):
-# precision = self.uidx.get(part.unit)
-# mod = 10 ** (self.max_precision - precision)
-# amount = part.amount * mod
-# ex = self.ex[part.unit] / 1000000000
-# amount *= ex
-# fn = getattr(self, 'handle_' + part.typ)
-# fn(amount, part.isdebit)
-# logg.debug('after {} {} (ex {}) = {}'.format(part.unit, amount, ex, self.r))
-#
-#
-# def balanced(self):
-# return self.r == 0
-#
-#
-# def handle_income(self, amount, issrc=False):
-# if issrc:
-# self.r += amount
-# else:
-# self.r -= amount
-#
-#
-# def handle_expense(self, amount, issrc=False):
-# if issrc:
-# self.r -= amount
-# else:
-# self.r += amount
-#
-#
-# def handle_asset(self, amount, issrc=False):
-# if issrc:
-# self.r += amount
-# else:
-# self.r -= amount
-#
-#
-# def handle_liability(self, amount, issrc=False):
-# if issrc:
-# self.r -= amount
-# else:
-# self.r += amount
diff --git a/dummy/usawa/entry.py b/dummy/usawa/entry.py
@@ -7,6 +7,7 @@ import lxml.etree
import rencode
from .base import UsawaElement
+from .balance import Balancer
from .constant import DEFAULTPARENT, NSPREFIX
from .crypto import DemoWallet
from .error import ACLError, VerifyError
@@ -205,7 +206,9 @@ class Entry(UsawaElement):
self.lookup = None
self.lookup_algo = None
self.parts = []
-
+ self.balancer = None
+ if unitindex != None:
+ self.balancer = Balancer(unitindex)
@staticmethod
def empty(*args, **kwargs):
@@ -230,6 +233,8 @@ class Entry(UsawaElement):
self.debit.append(part)
else:
self.credit.append(part)
+ if self.balancer != None:
+ self.balancer.apply_part(part)
"""Append a single media asset to the attachment list for the entry.