Skip to content

Commit

Permalink
Merge pull request #30 from apromessi/ariana-refactor
Browse files Browse the repository at this point in the history
Refactor #1: Create new files for constants and utils
  • Loading branch information
jairtrejo authored May 24, 2024
2 parents 0f74901 + 1b41f95 commit a620af0
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 284 deletions.
33 changes: 33 additions & 0 deletions oldabe/accounting_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from decimal import Decimal

ROUNDING_TOLERANCE = Decimal("0.000001")


def get_rounding_difference(attributions):
"""
Get the difference of the total of the attributions from 1, which is
expected to occur due to finite precision. If the difference exceeds the
expected error tolerance, an error is signaled.
"""
total = _get_attributions_total(attributions)
difference = total - Decimal("1")
assert abs(difference) <= ROUNDING_TOLERANCE
return difference


def correct_rounding_error(attributions, incoming_attribution):
"""Due to finite precision, the Decimal module will round up or down
on the last decimal place. This could result in the aggregate value not
quite totaling to 1. This corrects that total by either adding or
subtracting the difference from the incoming attribution (by convention).
"""
difference = get_rounding_difference(attributions)
attributions[incoming_attribution.email] -= difference


def assert_attributions_normalized(attributions):
assert _get_attributions_total(attributions) == Decimal("1")


def _get_attributions_total(attributions):
return sum(attributions.values())
18 changes: 18 additions & 0 deletions oldabe/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

ABE_ROOT = './abe'
PAYOUTS_DIR = os.path.join(ABE_ROOT, 'payouts')
PAYMENTS_DIR = os.path.join(ABE_ROOT, 'payments')
NONATTRIBUTABLE_PAYMENTS_DIR = os.path.join(
ABE_ROOT, 'payments', 'nonattributable'
)

TRANSACTIONS_FILE = os.path.join(ABE_ROOT, 'transactions.txt')
DEBTS_FILE = os.path.join(ABE_ROOT, 'debts.txt')
ADVANCES_FILE = os.path.join(ABE_ROOT, 'advances.txt')
UNPAYABLE_CONTRIBUTORS_FILE = os.path.join(ABE_ROOT, 'unpayable_contributors.txt')
ITEMIZED_PAYMENTS_FILE = os.path.join(ABE_ROOT, 'itemized_payments.txt')
PRICE_FILE = os.path.join(ABE_ROOT, 'price.txt')
VALUATION_FILE = os.path.join(ABE_ROOT, 'valuation.txt')
ATTRIBUTIONS_FILE = 'attributions.txt'
INSTRUMENTS_FILE = 'instruments.txt'
9 changes: 9 additions & 0 deletions oldabe/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import subprocess

def get_git_revision_short_hash() -> str:
"""From https://stackoverflow.com/a/21901260"""
return (
subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'])
.decode('ascii')
.strip()
)
Loading

0 comments on commit a620af0

Please sign in to comment.