diff --git a/bpfin/financials/liability.py b/bpfin/financials/liability.py new file mode 100644 index 0000000000000000000000000000000000000000..c35ab6ba2ea8b6b2e12a510717db77bb01263b14 --- /dev/null +++ b/bpfin/financials/liability.py @@ -0,0 +1,99 @@ +import calendar +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]) + return max(date_list) + + +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: + year = year + ((month + months) // 12) + if (month + months) % 12 == 0: + month = 12 + year = year - 1 + else: + month = month + months - ((month + months) // 12 * 12) + else: + month = month + months + day = min(sourcedate.day, calendar.monthrange(year, month)[1]) + return date(year, month, day) + + +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)) + return calendar + + +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]) + temp_dict = {} + for date in cal: + temp_dict[date] = value[0] + final_dict.append(temp_dict) + return final_dict + + +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) + 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/tests/test_financials/test_liability.py b/bpfin/tests/test_financials/test_liability.py new file mode 100644 index 0000000000000000000000000000000000000000..ea26cf9aaaabd993fc15082d7a6a7f82e438f082 --- /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 diff --git a/bpfin/tests/test_utilbills/test_bill_proj_reg.py b/bpfin/tests/test_utilbills/test_bill_proj_reg.py index 7301e9e14ad8728ec0332252996b73f8748647c4..ce1419ed13d728c1f5b7803115bd16666ab992cb 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]) diff --git a/bpfin/utilbills/bill_lib.py b/bpfin/utilbills/bill_lib.py index d872fbee0ed63811eee2b0ee9164680f1508f4ce..8aa7522ff0d7359cc48d5b9e0ecf06d43805ffda 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.