# Path: budgettracker/data.py
# def split_income_expenses(transactions):
#     income = filter(lambda tx: tx.amount > 0.0, transactions)
#     expenses = filter(lambda tx: tx.amount <= 0.0, transactions)
#     return income, expenses
# 
# def extract_transactions_by_label(transactions, labels):
#     def match_label(tx):
#         for exp in labels:
#             if re.match(exp, tx.label):
#                 return True
#         return False
#     matching = filter(match_label, transactions)
#     transactions = filter_out_transactions(transactions, matching)
#     return matching, transactions
# 
# def filter_transactions_period(transactions, start_date=None, end_date=None):
#     if not start_date and not end_date:
#         return transactions
#     return filter(
#         lambda tx: (not start_date or tx.date >= start_date) and (not end_date or tx.date < end_date),
#         transactions)
# 
# def sort_transactions(transactions):
#     return sorted(transactions, key=lambda tx: tx.date, reverse=True)
# 
# def period_to_months(start_date, end_date):
#     dates = [start_date.replace(day=1)]
#     end_date = end_date.replace(day=1) - monthdelta(1)
#     while dates[-1] < end_date:
#         dates.append(dates[-1] + monthdelta(1))
#     return dates


from collections import namedtuple, OrderedDict
from .data import (split_income_expenses, extract_transactions_by_label, filter_transactions_period,
                   sort_transactions, period_to_months)
from monthdelta import monthdelta
import datetime
            expenses_transactions, planned_expenses_labels)

    savings_goal = 0
    if budget_goals:
        savings_goal = sum([s.savings_per_month for s in budget_goals])
    
    income = sum([tx.amount for tx in income_transactions])
    expenses = abs(sum([tx.amount for tx in expenses_transactions]))
    planned_expenses = abs(sum([tx.amount for tx in planned_expenses_transactions]))
    real_balance = income - planned_expenses - expenses
    savings = income - expected_planned_expenses - expenses
    balance = savings - savings_goal

    expected_real_balance = max(income, expected_income) - planned_expenses - expenses
    expected_savings = max(income, expected_income) - expected_planned_expenses - expenses
    expected_balance = expected_savings - savings_goal
    expected_remaining = max(expected_balance, 0)

    return Budget(month=start_date,
                  transactions=transactions,
                  income_transactions=income_transactions,
                  planned_expenses_transactions=planned_expenses_transactions,
                  expenses_transactions=expenses_transactions,
                  real_balance=real_balance,
                  balance=balance,
                  income=income,
                  planned_expenses=planned_expenses,
                  expenses=expenses,
                  savings=savings,
                  savings_goal=savings_goal,

# Path: budgettracker/data.py
# def split_income_expenses(transactions):
#     income = filter(lambda tx: tx.amount > 0.0, transactions)
#     expenses = filter(lambda tx: tx.amount <= 0.0, transactions)
#     return income, expenses
# 
# def extract_transactions_by_label(transactions, labels):
#     def match_label(tx):
#         for exp in labels:
#             if re.match(exp, tx.label):
#                 return True
#         return False
#     matching = filter(match_label, transactions)
#     transactions = filter_out_transactions(transactions, matching)
#     return matching, transactions
# 
# def filter_transactions_period(transactions, start_date=None, end_date=None):
#     if not start_date and not end_date:
#         return transactions
#     return filter(
#         lambda tx: (not start_date or tx.date >= start_date) and (not end_date or tx.date < end_date),
#         transactions)
# 
# def sort_transactions(transactions):
#     return sorted(transactions, key=lambda tx: tx.date, reverse=True)
# 
# def period_to_months(start_date, end_date):
#     dates = [start_date.replace(day=1)]
#     end_date = end_date.replace(day=1) - monthdelta(1)
#     while dates[-1] < end_date:
#         dates.append(dates[-1] + monthdelta(1))
#     return dates


# Path: budgettracker/budget.py
from collections import namedtuple, OrderedDict
from .data import (split_income_expenses, extract_transactions_by_label, filter_transactions_period,
                   sort_transactions, period_to_months)
from monthdelta import monthdelta
import datetime

            expenses_transactions, planned_expenses_labels)

    savings_goal = 0
    if budget_goals:
        savings_goal = sum([s.savings_per_month for s in budget_goals])
    
    income = sum([tx.amount for tx in income_transactions])
    expenses = abs(sum([tx.amount for tx in expenses_transactions]))
    planned_expenses = abs(sum([tx.amount for tx in planned_expenses_transactions]))
    real_balance = income - planned_expenses - expenses
    savings = income - expected_planned_expenses - expenses
    balance = savings - savings_goal

    expected_real_balance = max(income, expected_income) - planned_expenses - expenses
    expected_savings = max(income, expected_income) - expected_planned_expenses - expenses
    expected_balance = expected_savings - savings_goal
    expected_remaining = max(expected_balance, 0)

    return Budget(month=start_date,
                  transactions=transactions,
                  income_transactions=income_transactions,
                  planned_expenses_transactions=planned_expenses_transactions,
                  expenses_transactions=expenses_transactions,
                  real_balance=real_balance,
                  balance=balance,
                  income=income,
                  planned_expenses=planned_expenses,
                  expenses=expenses,
                  savings=savings,
                  savings_goal=savings_goal,

                  expected_real_balance=expected_real_balance,