-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yunis Yilmaz
authored and
Yunis Yilmaz
committed
Jun 11, 2024
1 parent
79af0ce
commit d73b1d9
Showing
6 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,36 @@ | ||
# wowhmm | ||
Who owes whom how much money? | ||
|
||
Who owes [whom](https://en.wiktionary.org/wiki/whom#Usage_notes) how much money? | ||
|
||
 | ||
|
||
## Installation | ||
|
||
<!-- TODO: Set this up with PyPI. --> | ||
|
||
```bash | ||
pip install wowhmm | ||
``` | ||
|
||
## Usage | ||
|
||
```python | ||
>>> from wowhmm import Ledger | ||
>>> | ||
>>> ledger = Ledger( | ||
... [ | ||
... ("Alice", 349.95, ["Alice", "Bob", "Carol", "Dan"]), # BnB | ||
... ("Bob", 68.42, ["Alice", "Dan"]), # Alcohol | ||
... ("Bob", 42.02, ["Alice", "Bob", "Carol", "Dan"]), # Groceries | ||
... ("Dan", 72.48, ["Alice", "Bob", "Carol", "Dan"]), # Transportation | ||
... ("Carol", 28.98, ["Carol", "Dan"]), # Movies | ||
... ] | ||
... ) | ||
>>> | ||
>>> ledger.tabulate() | ||
Alice Bob Carol Dan | ||
Alice 0.00 -42.77 -87.49 -69.37 | ||
Bob 42.77 0.00 -10.51 -26.60 | ||
Carol 87.49 10.51 0.00 3.63 | ||
Dan 69.37 26.60 -3.63 0.00 | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pandas==2.2.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from setuptools import find_packages, setup | ||
|
||
with open("README.md", "r") as f: | ||
long_description = f.read() | ||
|
||
setup( | ||
name="wowhmm", | ||
version="0.1.4", | ||
description="Who owes whom how much money", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/yoonthegoon/wowhmm", | ||
packages=find_packages(), | ||
install_requires=[ | ||
"pandas==2.2.2", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .core import Ledger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import List, NamedTuple | ||
|
||
import pandas as pd | ||
|
||
|
||
class Spent(NamedTuple): | ||
""" | ||
Who spent amount for whom. | ||
:param who: The person who spent the money | ||
:param amount: The amount of money spent | ||
:param for_whom: The people for whom the money was spent | ||
""" | ||
|
||
who: str | ||
amount: float | ||
for_whom: List[str] | ||
|
||
|
||
class Ledger: | ||
def __init__(self, transactions: List[Spent] = None): | ||
""" | ||
A ledger of transactions. | ||
:param transactions: A list of transactions | ||
""" | ||
|
||
self.transactions = transactions or [] | ||
if not all(isinstance(transaction, Spent) for transaction in self.transactions): | ||
self.transactions = [ | ||
Spent(who, amount, for_whom) for who, amount, for_whom in transactions | ||
] | ||
|
||
def tabulate(self) -> pd.DataFrame: | ||
""" | ||
Tabulate who owes whom how much. | ||
Values are negative if the person owes money and positive if the person is owed money. | ||
Values are rounded to two decimal places. | ||
:return: A DataFrame of who owes whom how much | ||
""" | ||
|
||
names = set( | ||
name for transaction in self.transactions for name in transaction[2] | ||
) | ||
names.update(transaction[0] for transaction in self.transactions) | ||
names = sorted(names) | ||
|
||
df = pd.DataFrame(0.0, index=names, columns=names) | ||
|
||
for payer, amount, payees in self.transactions: | ||
for payee in payees: | ||
if payer != payee: | ||
value = round(amount / len(payees), 2) | ||
df.loc[payer, payee] -= value | ||
df.loc[payee, payer] += value | ||
|
||
return df |