From: Mauro Scomparin Date: Tue, 17 Jul 2018 20:12:18 +0000 (+0200) Subject: added formatter X-Git-Url: https://git.scompo.it/?a=commitdiff_plain;h=935cb8105c3041b16a651b676cb17cd2cb544602;p=money.git added formatter --- diff --git a/money/money.py b/money/money.py index 2982bcb..4cd3727 100755 --- a/money/money.py +++ b/money/money.py @@ -220,6 +220,14 @@ def value_message(str, value): return str.format(value) +def format_movement(movement): + return '|{0: >10}|{1: <10}|{2: <5}|{3: <18}|'.format( + movement[VALUE_FIELD_NAME][:10], + movement[DATE_FIELD_NAME][:10], + movement[HOUR_FIELD_NAME][:5], + movement[DESCRIPTION_FIELD_NAME][:18]) + + def write_latest_movements(data_file, conf): movements = load_file(data_file) ordered_movements = sort_movements(movements) @@ -239,4 +247,5 @@ def write_latest_movements(data_file, conf): print(last_movement_number_message(conf[LAST_MOVEMENT_NUMBER])) for i in range(conf[LAST_MOVEMENT_NUMBER]): if i < len(ordered_movements): - print(ordered_movements[i]) + formatted_movement = format_movement(ordered_movements[i]) + print(formatted_movement) diff --git a/money/tests/test_money.py b/money/tests/test_money.py index 9ab8de2..909c703 100644 --- a/money/tests/test_money.py +++ b/money/tests/test_money.py @@ -20,6 +20,15 @@ class MoneyTest(unittest.TestCase): self.assertEqual(money.last_movement_number_message( 10), "latest 10 movements:") + def test_format_movement(self): + self.assertEqual('| -100.00|18/04/2017|00:01|testings stuffffff|', + money.format_movement({ + money.VALUE_FIELD_NAME: '-100.00', + money.DATE_FIELD_NAME: '18/04/2017', + money.HOUR_FIELD_NAME: '00:01', + money.DESCRIPTION_FIELD_NAME: 'testings stuffffff stuffff', + })) + def test_write_movement_to_stream(self): test_str = io.StringIO() m = { @@ -31,7 +40,8 @@ class MoneyTest(unittest.TestCase): } money.write_movement_to_stream(test_str, m) test_str.seek(0) - self.assertEqual('-100;18/04/2017;00:01;testings\n', test_str.readline(), 'wrong string read') + self.assertEqual('-100;18/04/2017;00:01;testings\n', + test_str.readline(), 'wrong string read') def test_load_movements_from_stream(self): test_str = io.StringIO()