From a0d7e425ba288d0a68b0551b8ee77f3b655610ef Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Mon, 22 May 2017 11:20:45 -0400 Subject: [PATCH] Create average_list_with_none() and its test files --- bpfin/lib/other.py | 17 +++++++++++++++++ bpfin/tests/test_lib/test_other.py | 18 ++++++++++++++++++ bpfin/tests/testdata/sample_data.py | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/bpfin/lib/other.py b/bpfin/lib/other.py index 7c080a7..0daf8f2 100644 --- a/bpfin/lib/other.py +++ b/bpfin/lib/other.py @@ -488,6 +488,23 @@ def month_shift(month): return month_shift_dict[month] +def average_list_with_none(list1): + """ + determine average value for all not-None elements in a list + Args: + list1 (list): list of float values + Return: + float: average value + """ + # if len(list1) == 0: + # return 0 + # else: + new_list = [] + for element in list1: + if element: + new_list.append(element) + return (sum(new_list)/len(new_list) if len(new_list) else 0) + ####inflation calculation #product inflation rate from starting date to target date diff --git a/bpfin/tests/test_lib/test_other.py b/bpfin/tests/test_lib/test_other.py index 0fdbb95..cc5f877 100644 --- a/bpfin/tests/test_lib/test_other.py +++ b/bpfin/tests/test_lib/test_other.py @@ -1,6 +1,7 @@ from bpfin.lib.other import add_year_dictionary from bpfin.lib.other import subtract_year_dictionary from bpfin.lib.other import divide_dscr_dict +from bpfin.lib.other import average_list_with_none from bpfin.tests.testdata import sample_data as db @@ -27,3 +28,20 @@ def test_divide_dscr_dict(): output = {2011: 0.0, 2012: 2.5, 2013: 0.3333333333333333, 2014: 2.0, 2015: None} result = divide_dscr_dict(year_1_input, year_2_input) assert result == output + + +def test_average_list_with_none(): + input_list = [] + output_list = [] + input_list.append([1, 2, 3]) + output_list.append(2.0) + input_list.append([1, 2, None]) + output_list.append(1.5) + input_list.append([1, None, None]) + output_list.append(1.0) + input_list.append([None, None, None]) + output_list.append(0) + input_list.append([1, None, 3]) + output_list.append(2.0) + for test_input, test_output in zip(input_list, output_list): + assert test_output == average_list_with_none(test_input) diff --git a/bpfin/tests/testdata/sample_data.py b/bpfin/tests/testdata/sample_data.py index 2fb3de6..34bab1b 100644 --- a/bpfin/tests/testdata/sample_data.py +++ b/bpfin/tests/testdata/sample_data.py @@ -2879,7 +2879,7 @@ raw_gas_bill_demo['date_from'] = [ datetime.date(2015, 12, 16), datetime.date(2015, 11, 16), datetime.date(2015, 10, 16), datetime.date(2015, 9, 19), datetime.date(2015, 8, 18), datetime.date(2015, 7, 20), datetime.date(2015, 6, 18), datetime.date(2015, 5, 19)] -raw__gas_bill_demo['date_to'] = [ +raw_gas_bill_demo['date_to'] = [ datetime.date(2016, 12, 16), datetime.date(2016, 11, 22), datetime.date(2016, 10, 18), datetime.date(2016, 9, 30), datetime.date(2016, 9, 19), datetime.date(2016, 8, 18), datetime.date(2016, 7, 20), datetime.date(2016, 6, 20), datetime.date(2016, 5, 18), -- GitLab