implementation of the revenue sharing model #2

Merged
dachary merged 2 commits from dachary/accountant:wip-share into master 2022-08-16 05:12:08 +00:00

Signed-off-by: Loïc Dachary loic@dachary.org

Signed-off-by: Loïc Dachary <loic@dachary.org>
dachary added 1 commit 2022-08-15 15:41:38 +00:00
0c24ce9ac6
implementation of the revenue sharing model
Signed-off-by: Loïc Dachary <loic@dachary.org>
dachary requested review from realaravinth 2022-08-15 15:41:59 +00:00
dachary requested review from Gusted 2022-08-15 15:41:59 +00:00
Gusted reviewed 2022-08-15 17:26:13 +00:00
Gusted left a comment
Owner

Can confirm this is implemented according to specification.

Can confirm this is implemented according to specification.
share.py Outdated
@ -0,0 +9,4 @@
# See https://forum.hostea.org/t/decision-revenue-sharing-model/92
#
def share_income(income, members):
if income <= 0:

Add a comment:

# If the income is negative or zero, then return members as nobody could be paid out
Add a comment: ``` # If the income is negative or zero, then return members as nobody could be paid out ```
Poster
Owner

Your suggestions inspired me to extensively comment on the function and the implementation.

Your suggestions inspired me to extensively comment on the function and the implementation.
dachary marked this conversation as resolved
share.py Outdated
@ -0,0 +11,4 @@
def share_income(income, members):
if income <= 0:
return members
if income < len(members):
# If the income is lower or equal to the amount of members, everyone gets one share.

Just to simplify the case for income == len(members), you could do income <= len(members)

``` # If the income is lower or equal to the amount of members, everyone gets one share. ``` Just to simplify the case for `income == len(members)`, you could do `income <= len(members)`
Poster
Owner

done

done
dachary marked this conversation as resolved
share.py Outdated
@ -0,0 +15,4 @@
share = 1
count = income
else:
share = min(int(income / len(members)), min(members, key=lambda m: m[EXPENSE])[EXPENSE])
# Calculate the share by getting the minimum amount from:
# 1. Get the member with the least amount of expense. (This is to prevent that someone gets more income than necessary when the income is evenly divided among members)
# 2. Income divided by amount of members. (This is to prevent that when the member with least amount of expenses is higher than `amount of members times that amount of expense`, so we don't distrubute more income than we have) 
``` # Calculate the share by getting the minimum amount from: # 1. Get the member with the least amount of expense. (This is to prevent that someone gets more income than necessary when the income is evenly divided among members) # 2. Income divided by amount of members. (This is to prevent that when the member with least amount of expenses is higher than `amount of members times that amount of expense`, so we don't distrubute more income than we have) ```
dachary marked this conversation as resolved
share.py Outdated
@ -0,0 +18,4 @@
share = min(int(income / len(members)), min(members, key=lambda m: m[EXPENSE])[EXPENSE])
count = len(members)
remaining = []
for i in range(count):
# Loop trough the possible members and subtract the share of their expense. 
``` # Loop trough the possible members and subtract the share of their expense. ```
dachary marked this conversation as resolved
dachary force-pushed wip-share from 0c24ce9ac6 to ba9f610c61 2022-08-15 23:03:08 +00:00 Compare
Poster
Owner

@Gusted I'm glad you enjoyed the implementation :-) Since it is a terminal recursion it could be a loop but it would make it a little bit less elegant.

while income >= 0:
   ...
   income = income - share * count
   members = remaining
return members

instead of

return share_income(income - share * count, remaining)
@Gusted I'm glad you enjoyed the implementation :-) Since it is a terminal recursion it could be a loop but it would make it a little bit less elegant. ``` while income >= 0: ... income = income - share * count members = remaining return members ``` instead of ``` return share_income(income - share * count, remaining) ```

It could be a loop but it would make it a little bit less elegant.

You will end up with a big loop as you would need to handle the edge-cases already being checked for in the first few lines. The recusion seems better for now

> It could be a loop but it would make it a little bit less elegant. You will end up with a big loop as you would need to handle the edge-cases already being checked for in the first few lines. The recusion seems better for now
dachary force-pushed wip-share from ba9f610c61 to 332efe15d0 2022-08-16 00:22:04 +00:00 Compare
realaravinth approved these changes 2022-08-16 04:56:34 +00:00
realaravinth left a comment
Owner

LGTM. Ran tests locally, must configure CI if we are going to co-dev this tool :)

LGTM. Ran tests locally, must configure CI if we are going to co-dev this tool :)
dachary merged commit 4c56b2008d into master 2022-08-16 05:12:08 +00:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Hostea/accountant#2
There is no content yet.