Skip to content

Commit

Permalink
build it all basically
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunis Yilmaz authored and Yunis Yilmaz committed Jun 11, 2024
1 parent 79af0ce commit d73b1d9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
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?

![wowhmm](https://github.com/yoonthegoon/wowhmm/media/wowhmm.png)

## 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
```
Binary file added media/wowhmm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pandas==2.2.2
17 changes: 17 additions & 0 deletions setup.py
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",
],
)
1 change: 1 addition & 0 deletions wowhmm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .core import Ledger
58 changes: 58 additions & 0 deletions wowhmm/core.py
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

0 comments on commit d73b1d9

Please sign in to comment.