Skip to content

Commit a620af0

Browse files
authored
Merge pull request #30 from apromessi/ariana-refactor
Refactor #1: Create new files for constants and utils
2 parents 0f74901 + 1b41f95 commit a620af0

File tree

10 files changed

+264
-284
lines changed

10 files changed

+264
-284
lines changed

oldabe/accounting_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from decimal import Decimal
2+
3+
ROUNDING_TOLERANCE = Decimal("0.000001")
4+
5+
6+
def get_rounding_difference(attributions):
7+
"""
8+
Get the difference of the total of the attributions from 1, which is
9+
expected to occur due to finite precision. If the difference exceeds the
10+
expected error tolerance, an error is signaled.
11+
"""
12+
total = _get_attributions_total(attributions)
13+
difference = total - Decimal("1")
14+
assert abs(difference) <= ROUNDING_TOLERANCE
15+
return difference
16+
17+
18+
def correct_rounding_error(attributions, incoming_attribution):
19+
"""Due to finite precision, the Decimal module will round up or down
20+
on the last decimal place. This could result in the aggregate value not
21+
quite totaling to 1. This corrects that total by either adding or
22+
subtracting the difference from the incoming attribution (by convention).
23+
"""
24+
difference = get_rounding_difference(attributions)
25+
attributions[incoming_attribution.email] -= difference
26+
27+
28+
def assert_attributions_normalized(attributions):
29+
assert _get_attributions_total(attributions) == Decimal("1")
30+
31+
32+
def _get_attributions_total(attributions):
33+
return sum(attributions.values())

oldabe/constants.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
ABE_ROOT = './abe'
4+
PAYOUTS_DIR = os.path.join(ABE_ROOT, 'payouts')
5+
PAYMENTS_DIR = os.path.join(ABE_ROOT, 'payments')
6+
NONATTRIBUTABLE_PAYMENTS_DIR = os.path.join(
7+
ABE_ROOT, 'payments', 'nonattributable'
8+
)
9+
10+
TRANSACTIONS_FILE = os.path.join(ABE_ROOT, 'transactions.txt')
11+
DEBTS_FILE = os.path.join(ABE_ROOT, 'debts.txt')
12+
ADVANCES_FILE = os.path.join(ABE_ROOT, 'advances.txt')
13+
UNPAYABLE_CONTRIBUTORS_FILE = os.path.join(ABE_ROOT, 'unpayable_contributors.txt')
14+
ITEMIZED_PAYMENTS_FILE = os.path.join(ABE_ROOT, 'itemized_payments.txt')
15+
PRICE_FILE = os.path.join(ABE_ROOT, 'price.txt')
16+
VALUATION_FILE = os.path.join(ABE_ROOT, 'valuation.txt')
17+
ATTRIBUTIONS_FILE = 'attributions.txt'
18+
INSTRUMENTS_FILE = 'instruments.txt'

oldabe/git.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import subprocess
2+
3+
def get_git_revision_short_hash() -> str:
4+
"""From https://stackoverflow.com/a/21901260"""
5+
return (
6+
subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'])
7+
.decode('ascii')
8+
.strip()
9+
)

0 commit comments

Comments
 (0)