From cd8159943ded34b4154957a291d39d86c352c3a1 Mon Sep 17 00:00:00 2001 From: Sarey Hamarneh Date: Thu, 20 Apr 2017 11:14:00 -0400 Subject: [PATCH] Add annualizing projection to bill_lib --- bpfin/tests/test_utilbills/test_bill_lib.py | 15 ++++++++++- bpfin/utilbills/bill_lib.py | 30 ++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/bpfin/tests/test_utilbills/test_bill_lib.py b/bpfin/tests/test_utilbills/test_bill_lib.py index 32670f1..9b99fae 100644 --- a/bpfin/tests/test_utilbills/test_bill_lib.py +++ b/bpfin/tests/test_utilbills/test_bill_lib.py @@ -1,4 +1,5 @@ -from bpfin.utilbills.bill_lib import add_list +from bpfin.utilbills.bill_lib import add_list, annualizing_projection +from datetime import date, datetime def test_add_list(): @@ -8,3 +9,15 @@ def test_add_list(): for i in range(len(result)): assert result[i] == output[i] + + +def test_annualizing_projection(): + input_dates = [ + date(2011, 10, 1), date(2011, 4, 4), date(2012, 12, 4), date( + 2014, 9, 5), date(2014, 8, 8), date(2014, 10, 4) + ] + input_values = [100, 200, 300, 400, 500, 600] + output_dictionary = {2011: 300, 2012: 300, 2014: 1500} + result = annualizing_projection(input_dates, input_values) + + assert output_dictionary == result diff --git a/bpfin/utilbills/bill_lib.py b/bpfin/utilbills/bill_lib.py index d872fbe..6f1de5a 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. @@ -174,3 +173,32 @@ def occupancy_final_list(occupancy_rate_list): for occ in occupancy_rate_list: occupancy_list.append(occ * 24) return occupancy_list + + +def annualizing_projection(dates, values): + """Return annualized values of monthly projections. + Args: + dates (list): list of dates + values (list): list of values + Return: + dictionary: dictionary of annualized values + """ + matching_cal_dict = {} + for i in range(0, len(dates)): + matching_cal_dict[dates[i]] = values[i] + + cal_year = {} + for date, value in matching_cal_dict.items(): + if date.year not in cal_year: + cal_year[date.year] = [] + + for year in cal_year: + for date, value in matching_cal_dict.items(): + if year == date.year: + cal_year[date.year].append(value) + + final_dict = {} + for year, value in cal_year.items(): + final_dict[year] = sum(value) + + return final_dict -- GitLab