budget-analytics 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""budget-analytics: pure functions for budget maths (totals, spending per category, budget status, recurring payments)."""
|
|
2
|
+
from budget_analytics.analytics import (
|
|
3
|
+
WARNING_PERCENT,
|
|
4
|
+
budget_status,
|
|
5
|
+
find_recurring,
|
|
6
|
+
monthly_totals,
|
|
7
|
+
normalise_description,
|
|
8
|
+
spending_by_category,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
__all__ = [ # what "from budget_analytics import *" gives you: the public functions
|
|
13
|
+
"WARNING_PERCENT",
|
|
14
|
+
"budget_status",
|
|
15
|
+
"find_recurring",
|
|
16
|
+
"monthly_totals",
|
|
17
|
+
"normalise_description",
|
|
18
|
+
"spending_by_category",
|
|
19
|
+
]
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Pure budget maths: no database and no web framework, so any Python app can use it.
|
|
2
|
+
|
|
3
|
+
Every amount is in whole cents (6435 means 64.35), so there are no float rounding errors.
|
|
4
|
+
A transaction is a plain dict with the keys "date", "description", "amount_cents", "type" and "category_id".
|
|
5
|
+
"""
|
|
6
|
+
import re
|
|
7
|
+
from collections import defaultdict
|
|
8
|
+
from statistics import median
|
|
9
|
+
|
|
10
|
+
WARNING_PERCENT = 80 # from 80% used, a budget is in "warning"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def monthly_totals(transactions: list[dict]) -> dict:
|
|
14
|
+
"""Income, expenses and what's left over, for whatever transactions you pass in (usually one month)."""
|
|
15
|
+
income = sum(t["amount_cents"] for t in transactions if t["type"] == "income") # add up only the income
|
|
16
|
+
expenses = sum(t["amount_cents"] for t in transactions if t["type"] == "expense") # add up only the spending
|
|
17
|
+
return {"income_cents": income, "expenses_cents": expenses, "balance_cents": income - expenses}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def spending_by_category(transactions: list[dict]) -> dict[int, int]:
|
|
21
|
+
"""Total spending per category id, e.g. {1: 17140, 3: 2500}. Income is not spending, so it's left out."""
|
|
22
|
+
spent = defaultdict(int) # a dict that starts every new key at 0, so += works the first time
|
|
23
|
+
for t in transactions:
|
|
24
|
+
if t["type"] == "expense":
|
|
25
|
+
spent[t["category_id"]] += t["amount_cents"]
|
|
26
|
+
return dict(spent) # hand back a normal dict
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def budget_status(limit_cents: int, spent_cents: int, warning_percent: int = WARNING_PERCENT) -> dict:
|
|
30
|
+
"""How much of one budget is used: ok under 80%, warning from 80% to 100%, over above 100%."""
|
|
31
|
+
if limit_cents <= 0: # a budget of 0 would mean dividing by 0 below
|
|
32
|
+
raise ValueError("limit_cents must be more than 0")
|
|
33
|
+
if spent_cents > limit_cents: # exactly at the limit is not over
|
|
34
|
+
status = "over"
|
|
35
|
+
elif spent_cents * 100 >= limit_cents * warning_percent: # whole numbers only: no float rounding at the 80% line
|
|
36
|
+
status = "warning"
|
|
37
|
+
else:
|
|
38
|
+
status = "ok"
|
|
39
|
+
return {
|
|
40
|
+
"limit_cents": limit_cents,
|
|
41
|
+
"spent_cents": spent_cents,
|
|
42
|
+
"left_cents": limit_cents - spent_cents, # negative when over: -2140 means 21.40 over
|
|
43
|
+
"percent_used": round(spent_cents * 100 / limit_cents), # for the bar and the label, e.g. 86
|
|
44
|
+
"status": status,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def normalise_description(text: str) -> str:
|
|
49
|
+
"""'NETFLIX.COM 8472' and 'Netflix.com 9921' are the same payment: keep only the lower-case letters."""
|
|
50
|
+
words = re.sub(r"[^a-z]+", " ", text.lower()) # anything that isn't a letter becomes a space
|
|
51
|
+
return " ".join(words.split()) # squash runs of spaces: "netflix com"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def find_recurring(transactions: list[dict], min_months: int = 3, tolerance_percent: int = 10) -> list[dict]:
|
|
55
|
+
"""Spot repeating payments, like rent or a subscription.
|
|
56
|
+
|
|
57
|
+
A payment counts as recurring when the same description (see normalise_description) is spent in at least
|
|
58
|
+
`min_months` different months, and every amount is within `tolerance_percent` of the typical amount.
|
|
59
|
+
"""
|
|
60
|
+
groups = defaultdict(list) # "netflix com" -> every Netflix expense
|
|
61
|
+
for t in transactions:
|
|
62
|
+
if t["type"] == "expense":
|
|
63
|
+
key = normalise_description(t["description"])
|
|
64
|
+
if key: # a description with no letters at all can't be matched with anything
|
|
65
|
+
groups[key].append(t)
|
|
66
|
+
|
|
67
|
+
found = []
|
|
68
|
+
for payments in groups.values():
|
|
69
|
+
months = {str(p["date"])[:7] for p in payments} # "2026-11-03" -> "2026-11"; a set keeps each month once
|
|
70
|
+
if len(months) < min_months:
|
|
71
|
+
continue
|
|
72
|
+
typical = round(median(p["amount_cents"] for p in payments)) # the middle amount, so one odd payment can't drag it
|
|
73
|
+
allowed = typical * tolerance_percent / 100 # how far an amount may be from the typical one
|
|
74
|
+
if any(abs(p["amount_cents"] - typical) > allowed for p in payments): # one amount too far off: not regular
|
|
75
|
+
continue
|
|
76
|
+
latest = max(payments, key=lambda p: str(p["date"])) # the most recent payment
|
|
77
|
+
found.append({
|
|
78
|
+
"description": latest["description"], # shown as the bank last wrote it
|
|
79
|
+
"category_id": latest["category_id"],
|
|
80
|
+
"amount_cents": typical,
|
|
81
|
+
"months": len(months),
|
|
82
|
+
"last_date": str(latest["date"]),
|
|
83
|
+
})
|
|
84
|
+
return sorted(found, key=lambda r: r["amount_cents"], reverse=True) # biggest first
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: budget-analytics
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pure budget maths in whole cents: monthly totals, spending per category, budget status and recurring payments
|
|
5
|
+
Project-URL: Source, https://github.com/NoMaRu123/budget-tracker
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# budget-analytics
|
|
14
|
+
|
|
15
|
+
Pure budget maths for Python. It has no database and no web framework, and no dependencies outside the standard library.
|
|
16
|
+
|
|
17
|
+
Every amount is in whole cents (`6435` means 64.35), so there are no float rounding errors. A transaction is a plain dict:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
{"date": "2026-11-14", "description": "Tesco", "amount_cents": 6435, "type": "expense", "category_id": 1}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
pip install budget-analytics
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Functions
|
|
30
|
+
|
|
31
|
+
| Function | What it gives back |
|
|
32
|
+
|---|---|
|
|
33
|
+
| `monthly_totals(transactions)` | `{"income_cents", "expenses_cents", "balance_cents"}` |
|
|
34
|
+
| `spending_by_category(transactions)` | `{category_id: cents spent}`; income isn't counted |
|
|
35
|
+
| `budget_status(limit_cents, spent_cents)` | `limit_cents`, `spent_cents`, `left_cents` (negative when over), `percent_used`, and `status`: `ok` under 80%, `warning` from 80% to 100%, `over` above 100% |
|
|
36
|
+
| `find_recurring(transactions, min_months=3, tolerance_percent=10)` | payments with the same description in at least 3 different months, each within 10% of the typical amount; biggest first |
|
|
37
|
+
| `normalise_description(text)` | `"NETFLIX.COM 8472"` → `"netflix com"`, used to match bank descriptions |
|
|
38
|
+
|
|
39
|
+
## Example
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from budget_analytics import budget_status, spending_by_category
|
|
43
|
+
|
|
44
|
+
transactions = [
|
|
45
|
+
{"date": "2026-11-10", "description": "Dinner", "amount_cents": 12860, "type": "expense", "category_id": 2},
|
|
46
|
+
{"date": "2026-11-20", "description": "Pizza night", "amount_cents": 4280, "type": "expense", "category_id": 2},
|
|
47
|
+
]
|
|
48
|
+
spent = spending_by_category(transactions) # {2: 17140}
|
|
49
|
+
budget_status(15000, spent[2])
|
|
50
|
+
# {'limit_cents': 15000, 'spent_cents': 17140, 'left_cents': -2140, 'percent_used': 114, 'status': 'over'}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Run the tests
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
cd library
|
|
57
|
+
pip install pytest
|
|
58
|
+
pytest
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Licence
|
|
62
|
+
|
|
63
|
+
MIT
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
budget_analytics/__init__.py,sha256=fv1_gDF6aDz83NDBuWtEyaYvzjto-nJmuFpInM2Baas,555
|
|
2
|
+
budget_analytics/analytics.py,sha256=FzL_Waui0SwzuWYKEhYUMkz4W1lYk269rcjffJEYK_s,4387
|
|
3
|
+
budget_analytics-0.1.0.dist-info/METADATA,sha256=i9NQgJhXM4NUNQ87BDIO8vqvx0Gs11PnkVMK9Bi9rnU,2284
|
|
4
|
+
budget_analytics-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
5
|
+
budget_analytics-0.1.0.dist-info/RECORD,,
|