From 11c45059e4b4a53c29b3109d98ec4080cd5aa799 Mon Sep 17 00:00:00 2001 From: Sarey Hamarneh Date: Thu, 20 Apr 2017 10:24:38 -0400 Subject: [PATCH 1/4] Create liability functions --- bpfin/financials/liability.py | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 bpfin/financials/liability.py diff --git a/bpfin/financials/liability.py b/bpfin/financials/liability.py new file mode 100644 index 0000000..cdc404e --- /dev/null +++ b/bpfin/financials/liability.py @@ -0,0 +1,67 @@ +import calendar +from datetime import datetime, date + + +def define_date_start(liability_dictionary): + """Return start date for debt calendar. + Args: + liability_dictionary(dict): dictionary + Return: + date: max date + """ + date_list = [] + for key, value in liability_dictionary.items(): + date_list.append(value[3]) + return max(date_list) + + +def add_months(sourcedate, months): + """Add some month value to current month. + Args: + sourcedate (datetime): start date + months (integer): number of months to add + Return: + date: new date + """ + month = sourcedate.month + year = sourcedate.year + if month + months > 12: + year = year + (month + months + 12 // 2) // 12 + month = month + months - 12 + else: + year = year + month = month + months + day = min(sourcedate.day, calendar.monthrange(year, month)[1]) + return date(year, month, day) + + +def liability_calendar(liability_dictionary, months): + """create list of datetimes for the debt calendar. + Args: + liability_dictionary (dictionary): start date + months (integer): number of months to add + Return: + calendar (list): debt calendar + """ + calendar = [] + for i in range(0, months): + calendar.append(add_months(define_date_start(liability_dictionary), i)) + return calendar + + +def liability_dict_creation(liability_dictionary, liability_calendar, months): + final_dict = {} + liab_dict = {} + + sum_debt = [] + for dict_key, dict_value in liability_dictionary.items(): + sum_debt.append((dict_value[0])) + total_initial_debt = sum(sum_debt) + + for cal in liability_calendar: + debt = total_initial_debt + final_dict[cal] = debt + #need to recalculate debt everytime a term goes by + #need to add to the list one at a time + + return final_dict -- GitLab From 697837ed2e829ccdc607f5d1ecc0878db0063f30 Mon Sep 17 00:00:00 2001 From: Sarey Hamarneh Date: Thu, 20 Apr 2017 13:46:51 -0400 Subject: [PATCH 2/4] Fix a test --- bpfin/tests/test_utilbills/test_bill_proj_reg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bpfin/tests/test_utilbills/test_bill_proj_reg.py b/bpfin/tests/test_utilbills/test_bill_proj_reg.py index 7301e9e..ce1419e 100644 --- a/bpfin/tests/test_utilbills/test_bill_proj_reg.py +++ b/bpfin/tests/test_utilbills/test_bill_proj_reg.py @@ -8,6 +8,6 @@ def test_regression_coefficients(): output = [0, 2] result = regression_coefficients(input_x, input_y, is_true) - for number in output: - for coef in result: - number = coef + for i in range(len(output)): + # for coef in result: + assert output[i] == round(result[i]) -- GitLab From dd68d01de048961fb04ff54829384157b6ed98aa Mon Sep 17 00:00:00 2001 From: Sarey Hamarneh Date: Thu, 20 Apr 2017 18:57:49 -0400 Subject: [PATCH 3/4] Liability code --- bpfin/financials/liability.py | 74 +++++++++++++++++------------------ bpfin/utilbills/bill_lib.py | 1 - 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/bpfin/financials/liability.py b/bpfin/financials/liability.py index cdc404e..598e565 100644 --- a/bpfin/financials/liability.py +++ b/bpfin/financials/liability.py @@ -3,12 +3,6 @@ from datetime import datetime, date def define_date_start(liability_dictionary): - """Return start date for debt calendar. - Args: - liability_dictionary(dict): dictionary - Return: - date: max date - """ date_list = [] for key, value in liability_dictionary.items(): date_list.append(value[3]) @@ -16,52 +10,56 @@ def define_date_start(liability_dictionary): def add_months(sourcedate, months): - """Add some month value to current month. - Args: - sourcedate (datetime): start date - months (integer): number of months to add - Return: - date: new date - """ month = sourcedate.month year = sourcedate.year if month + months > 12: - year = year + (month + months + 12 // 2) // 12 - month = month + months - 12 + year = year + ((month + months) // 12) + if (month + months) % 12 == 0: + month = 12 + year = year - 1 + else: + month = month + months - ((month + months) // 12 * 12) else: - year = year month = month + months day = min(sourcedate.day, calendar.monthrange(year, month)[1]) return date(year, month, day) -def liability_calendar(liability_dictionary, months): - """create list of datetimes for the debt calendar. - Args: - liability_dictionary (dictionary): start date - months (integer): number of months to add - Return: - calendar (list): debt calendar - """ +def liability_calendar(date, months): calendar = [] - for i in range(0, months): - calendar.append(add_months(define_date_start(liability_dictionary), i)) + for i in range(months): + calendar.append(add_months(date, i)) return calendar -def liability_dict_creation(liability_dictionary, liability_calendar, months): - final_dict = {} - liab_dict = {} +def create_liability_dict(liability_dictionary): + final_dict = [] + for key, value in liability_dictionary.items(): + cal = liability_calendar(value[3], value[2]) + temp_dict = {} + for date in cal: + temp_dict[date] = value[0] + final_dict.append(temp_dict) + return final_dict - sum_debt = [] - for dict_key, dict_value in liability_dictionary.items(): - sum_debt.append((dict_value[0])) - total_initial_debt = sum(sum_debt) - for cal in liability_calendar: - debt = total_initial_debt - final_dict[cal] = debt - #need to recalculate debt everytime a term goes by - #need to add to the list one at a time +def final_liability_dict(start_date, liability_dictionary, months): + final_dict = {} + pro_forma_calendar = liability_calendar(start_date, months) + debt_list = create_liability_dict(liability_dictionary) + for date in pro_forma_calendar: + for debt_date_dict in debt_list: + for debt_date in debt_date_dict: + if date.year == debt_date.year and date.month == debt_date.month: + if (date.year, date.month) not in final_dict: + final_dict[(date.year, date.month)] = [] + final_dict[(date.year, + date.month)].append(debt_date_dict[debt_date]) + for date in pro_forma_calendar: + if (date.year, date.month) not in final_dict: + final_dict[(date.year, date.month)] = [0] + + for key, value in final_dict.items(): + final_dict[key] = sum(value) return final_dict diff --git a/bpfin/utilbills/bill_lib.py b/bpfin/utilbills/bill_lib.py index d872fbe..8aa7522 100644 --- a/bpfin/utilbills/bill_lib.py +++ b/bpfin/utilbills/bill_lib.py @@ -3,7 +3,6 @@ import datetime import calendar - def add_list(obj_list, number): """Add a number to each value in a list. -- GitLab From b408e1a92dce2304dd88cecbb7c0f2508fc7f789 Mon Sep 17 00:00:00 2001 From: Sarey Hamarneh Date: Thu, 20 Apr 2017 22:53:33 -0400 Subject: [PATCH 4/4] Create liability and test_liability --- bpfin/financials/liability.py | 34 +++++++++++++++++++ bpfin/tests/test_financials/test_liability.py | 27 +++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 bpfin/tests/test_financials/test_liability.py diff --git a/bpfin/financials/liability.py b/bpfin/financials/liability.py index 598e565..c35ab6b 100644 --- a/bpfin/financials/liability.py +++ b/bpfin/financials/liability.py @@ -3,6 +3,12 @@ from datetime import datetime, date def define_date_start(liability_dictionary): + """Return latest date from dictionary of debts with associated dates. + Args: + liability_dictionary (dictionary): dictionary of debts + Return: + datetime: max date + """ date_list = [] for key, value in liability_dictionary.items(): date_list.append(value[3]) @@ -10,6 +16,13 @@ def define_date_start(liability_dictionary): def add_months(sourcedate, months): + """Add desired number of months to current date + Args: + sourcedate (datetime): start date + months (int): number of months to add + Return: + datetime: new date value + """ month = sourcedate.month year = sourcedate.year if month + months > 12: @@ -26,6 +39,13 @@ def add_months(sourcedate, months): def liability_calendar(date, months): + """Create calendar of date to date+months + Args: + date (datetime): start date + months (int): number of months to add + Return: + list: list (calendar) of dates + """ calendar = [] for i in range(months): calendar.append(add_months(date, i)) @@ -33,6 +53,12 @@ def liability_calendar(date, months): def create_liability_dict(liability_dictionary): + """Create dictionary of calendars for each debt ID with debt values + Args: + liability_dictionary (dictionary): dictionary of debt ID key with associated values, including key + Return: + list: list of liability dictionaries with associated debts + """ final_dict = [] for key, value in liability_dictionary.items(): cal = liability_calendar(value[3], value[2]) @@ -44,6 +70,14 @@ def create_liability_dict(liability_dictionary): def final_liability_dict(start_date, liability_dictionary, months): + """Create dictionary of {(year,month):debt value}, which includes all debts + Args: + start_date (datetime): first date of the dictionary + liability_dictionary (dictionary): dictionary of debt ID key with associated values, including key + months (integer): length of dictionary + Return: + dictionary: {(year,month):debt value} + """ final_dict = {} pro_forma_calendar = liability_calendar(start_date, months) debt_list = create_liability_dict(liability_dictionary) diff --git a/bpfin/tests/test_financials/test_liability.py b/bpfin/tests/test_financials/test_liability.py new file mode 100644 index 0000000..ea26cf9 --- /dev/null +++ b/bpfin/tests/test_financials/test_liability.py @@ -0,0 +1,27 @@ +from bpfin.financials.liability import liability_calendar, create_liability_dict, final_liability_dict +from datetime import date, datetime + + +def test_final_liability_dict(): + input_start_date = date(2012, 10, 1) + input_dict = { + 'debt1': (150, 'NYSERDA', 10, date(2012, 12, 31)), + 'debt2': (100, 'NYCEEC', 20, date(2012, 8, 31)) + } + input_duration = 10 + output = { + (2012, 10): 100, + (2012, 11): 100, + (2012, 12): 250, + (2013, 1): 250, + (2013, 2): 250, + (2013, 3): 250, + (2013, 4): 250, + (2013, 5): 250, + (2013, 6): 250, + (2013, 7): 250 + } + + result = final_liability_dict(input_start_date, input_dict, input_duration) + + assert output == result -- GitLab