A Python library for splitting shared expenses between groups of people
Project description
expensesplit
A Python library for splitting shared expenses between a group of people. Inspired by Splitwise. Uses the Minimize Cash Flow algorithm to produce the fewest possible settlement transactions.
Install
pip install expensesplit
Quick Start
from expensesplit import calculate_balances, calculate_settlements
members = ['Arya', 'John', 'Sara']
expenses = [
{'payer': 'Arya', 'amount': 90.0}, # Arya paid $90 for dinner
{'payer': 'John', 'amount': 30.0}, # John paid $30 for taxi
]
# Step 1 — calculate each person's net balance
balances = calculate_balances(members, expenses)
# {'Arya': 50.0, 'John': -10.0, 'Sara': -40.0}
# Arya is owed $50, John owes $10, Sara owes $40
# Step 2 — calculate minimum payments to settle everything
settlements = calculate_settlements(balances)
# [{'from': 'Sara', 'to': 'Arya', 'amount': 40.0},
# {'from': 'John', 'to': 'Arya', 'amount': 10.0}]
for s in settlements:
print(f"{s['from']} pays {s['to']} ${s['amount']:.2f}")
Split Modes
Equal Split (default)
from expensesplit import split_amount
shares = split_amount(90.0, ['Arya', 'John', 'Sara'])
# {'Arya': 30.0, 'John': 30.0, 'Sara': 30.0}
Custom Amounts
shares = split_amount(
100.0,
['Arya', 'John', 'Sara'],
split_type='custom',
values={'Arya': 50.0, 'John': 30.0, 'Sara': 20.0}
)
# {'Arya': 50.0, 'John': 30.0, 'Sara': 20.0}
Percentage Split
shares = split_amount(
200.0,
['Arya', 'John', 'Sara'],
split_type='percentage',
values={'Arya': 50, 'John': 30, 'Sara': 20}
)
# {'Arya': 100.0, 'John': 60.0, 'Sara': 40.0}
Custom Shares on Expenses
expenses = [
{
'payer': 'Arya',
'amount': 100.0,
'shares': {'Arya': 50.0, 'John': 30.0, 'Sara': 20.0} # custom split
},
{
'payer': 'John',
'amount': 60.0
# no 'shares' key = equal split
},
]
balances = calculate_balances(['Arya', 'John', 'Sara'], expenses)
Settlement Status
from expensesplit import get_settlement_status
get_settlement_status(total_owed=100.0, amount_paid=100.0) # 'settled'
get_settlement_status(total_owed=100.0, amount_paid=40.0) # 'partial'
get_settlement_status(total_owed=100.0, amount_paid=0.0) # 'pending'
How the Algorithm Works
The library uses the Minimize Cash Flow algorithm:
- Calculate each member's net balance (paid minus owed)
- Separate into creditors (positive balance) and debtors (negative balance)
- Greedily match the largest debtor with the largest creditor
- Repeat until all balances are zero
This guarantees the fewest possible transactions to settle all debts.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file expensesplit-1.0.0.tar.gz.
File metadata
- Download URL: expensesplit-1.0.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0917df5d3b17ee2ea8d0fb05e8ef82861a902e6bb7c050244c20b6ae913fb2b
|
|
| MD5 |
792e6da1a2e355426877dfaf7fdcdb3e
|
|
| BLAKE2b-256 |
2fa91501bde9b0646c36e65d3e9e7d025a4a65ae74a71276efc6600b942dae7b
|
File details
Details for the file expensesplit-1.0.0-py3-none-any.whl.
File metadata
- Download URL: expensesplit-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6363356a6f9a574ef02cb927a473f41734dc66522e6206ac58d3b5a7237f2b0a
|
|
| MD5 |
b626abad4d169c04ce6f943f393712d3
|
|
| BLAKE2b-256 |
d048bcdfb75add8f6e8d5e73752ac5a23b50d7cd72cb736b383f0e00dc02f78e
|