Added Stat Calculation
This commit is contained in:
parent
cb97f13a2b
commit
ee0367b495
61
bankView.py
61
bankView.py
@ -1,7 +1,7 @@
|
||||
#BankView - Calculate and Build Banking Stats
|
||||
#Cal.W 2020
|
||||
|
||||
import csv
|
||||
import csv, statistics
|
||||
from datetime import datetime
|
||||
|
||||
TRANSACTION_TYPES = ['Other', "Tap and Pay", "Card", "Deposit"]
|
||||
@ -30,6 +30,7 @@ def transDetails(transDetails):
|
||||
return transDetails, transDate, transType, transAcc
|
||||
|
||||
#Return a list of transactions in the form:
|
||||
# 0 1 2 3 4 5 6
|
||||
#(date(Transaction Date), date(Initialized), float(Amount), str(Details), float(Acc Balance), str(Payment Type), str(Acc))
|
||||
def importTransactionData(fileName="import.csv", transactions=list(), delimiter=',', newline=''):
|
||||
rawTransactions = list()
|
||||
@ -115,7 +116,61 @@ def saveTransactionData(transactions, fileName="transData.csv"):
|
||||
|
||||
return
|
||||
|
||||
def calculateStats(transactions, yearRangeVal=None, monthRangeVal=None, otherFelid=None):
|
||||
yearRange = lambda x: True
|
||||
if isinstance(yearRangeVal, type(yearRange)): yearRange = yearRangeVal
|
||||
if isinstance(yearRangeVal, list) or isinstance(yearRangeVal, tuple): yearRange = lambda x: x[1].year in yearRangeVal
|
||||
if isinstance(yearRangeVal, int) or isinstance(yearRangeVal, float): yearRange = lambda x: x[1].year == int(yearRangeVal)
|
||||
|
||||
monthRange = lambda x: True
|
||||
if isinstance(monthRangeVal, type(monthRange)): monthRange = monthRangeVal
|
||||
if isinstance(monthRangeVal, list) or isinstance(monthRangeVal, tuple): monthRange = lambda x: x[1].month in monthRangeVal
|
||||
if isinstance(monthRangeVal, int) or isinstance(monthRangeVal, float): monthRange = lambda x: x[1].month == int(monthRangeVal)
|
||||
|
||||
if not isinstance(yearRangeVal, type(monthRangeVal)): otherFelid = lambda x: True
|
||||
|
||||
periodTransactions = [x for x in transactions if yearRange(x) and monthRange(x) and otherFelid(x)]
|
||||
print(periodTransactions)
|
||||
print("*"*70)
|
||||
periodTransactions = sorted(periodTransactions, reverse = True, key = lambda x: x[1])
|
||||
|
||||
print(periodTransactions)
|
||||
|
||||
input("$"*70)
|
||||
|
||||
|
||||
stats = {
|
||||
"credits" : round(sum([abs(x[2]) for x in periodTransactions if x[2] > 0]), 2) if len([abs(x[2]) for x in periodTransactions if x[2] > 0]) > 0 else float(0),
|
||||
"debits" : round(sum([abs(x[2]) for x in periodTransactions if x[2] < 0]), 2) if len([abs(x[2]) for x in periodTransactions if x[2] < 0]) > 0 else float(0),
|
||||
"largestCredit" : max(periodTransactions, key=lambda x: x[2]) if len(periodTransactions) > 0 else (datetime.now(), datetime.now(), 0, "", 0, "", ""),
|
||||
"largestDebit" : min(periodTransactions, key=lambda x: x[2]) if len(periodTransactions) > 0 else (datetime.now(), datetime.now(), 0, "", 0, "", ""),
|
||||
"averageCredit" : round(statistics.fmean([abs(x[2]) for x in periodTransactions if x[2] > 0]), 2) if len([abs(x[2]) for x in periodTransactions if x[2] > 0]) > 0 else float(0),
|
||||
"averageDebit" : round(statistics.fmean([abs(x[2]) for x in periodTransactions if x[2] < 0]), 2) if len([abs(x[2]) for x in periodTransactions if x[2] < 0]) > 0 else float(0),
|
||||
"netBalance" : round(sum([x[4] for x in periodTransactions]), 2) if len(periodTransactions) > 0 else float(0),
|
||||
"averageBalance" : round(statistics.fmean([x[4] for x in periodTransactions]), 2) if len(periodTransactions) > 0 else float(0),
|
||||
"highestBalance" : float(max(periodTransactions, key=lambda x: x[4])[4]) if len(periodTransactions) > 0 else float(0),
|
||||
"lowestBalance" : float(min(periodTransactions, key=lambda x: x[4])[4]) if len(periodTransactions) > 0 else float(0),
|
||||
}
|
||||
|
||||
return stats
|
||||
|
||||
def foo(transactions):
|
||||
years = []
|
||||
for trans in transactions:
|
||||
if trans[1].year not in years:
|
||||
years.append(trans[1].year)
|
||||
|
||||
for year in years:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
trans = importTransactionData() #importTransactionData("./Raw Transaction Data.csv")
|
||||
[print(x) for x in trans if x[-2] == TRANSACTION_TYPES[-1]]
|
||||
trans = loadTransactionData()
|
||||
y = lambda x: x[-2] == TRANSACTION_TYPES[0]
|
||||
print(calculateStats(trans, otherFelid=y))
|
||||
|
||||
#calculateStats(trans)
|
||||
|
||||
|
||||
|
||||
#[print(x) for x in trans if x[-2] == TRANSACTION_TYPES[-1]]
|
Loading…
x
Reference in New Issue
Block a user