From 658c0156c8fceb505774d261c4b06339894b9760 Mon Sep 17 00:00:00 2001 From: RujitRaval Date: Tue, 27 Aug 2019 12:12:07 -0400 Subject: [PATCH 1/2] Code Refectoring --- bpeng/pna/score_calculation.py | 87 ++++++++++++---------------------- 1 file changed, 31 insertions(+), 56 deletions(-) diff --git a/bpeng/pna/score_calculation.py b/bpeng/pna/score_calculation.py index c097eae..927d1c1 100644 --- a/bpeng/pna/score_calculation.py +++ b/bpeng/pna/score_calculation.py @@ -1,64 +1,39 @@ from .discrete_bar_graph import create_graph_factory -def calculate_score_weight(weights_list, details_list): - - score_weight_1 = 0 - score_weight_2 = 0 - score_weight_3 = 0 - score_weight_4 = 0 - - final_criteria_score = [] - - for i in weights_list: - if i[0] == 1: - score_weight_1 += i[1] - elif i[0] == 2: - score_weight_2 += i[1] - elif i[0] == 3: - score_weight_3 += i[1] - elif i[0] == 4: - score_weight_4 += i[1] - elif i[0] == 5: - continue - - if details_list['num_of_heating_violations'] == 'None': - score_weight_1 += 0 - elif details_list['num_of_heating_violations'] in range(1, 3): - score_weight_1 += 1 - elif details_list['num_of_heating_violations'] in range(3, 11): - score_weight_1 += 3 - elif details_list['num_of_heating_violations'] >= 11: - score_weight_1 += 5 - else: - score_weight_1 += 0 - - if details_list['num_of_dob_violations'] == 'None': - score_weight_4 += 5 - elif details_list['num_of_dob_violations'] in range(1, 4): - score_weight_4 += 4 - elif details_list['num_of_dob_violations'] in range(4, 20): - score_weight_4 += 3 - elif details_list['num_of_dob_violations'] in range(20, 100): - score_weight_4 += 4 - elif details_list['num_of_dob_violations'] >= 100: - score_weight_4 += 5 - else: - score_weight_4 += 0 +def calculate_score_weight(weight_list, details_list): + score_weight = [0, 0, 0, 0] + final_criteria_scores = [] + + for i in range(len(weight_list)): + index = weight_list[i][0]-1 + score_weight[index] += weight_list[i][1] + + heating_violations_score = [0, 1, 3, 5] + num = [0,1,3,11] + for i in range(len([0, 1, 3, 11])): + if not details_list['num_of_heating_violations'] > num[i]: + score_weight[0] += heating_violations_score[i] + + dob_violations_score = [5, 4, 3, 4, 5] + num = [0, 1, 4, 20, 100] + for i in range(len([0, 1, 4, 20, 100])): + if not details_list['num_of_dob_violations'] > num[i]: + score_weight[3] += dob_violations_score[i] if details_list['legal_ownership'] == 'Single': - score_weight_4 += 3 + score_weight[3] += 3 elif details_list['legal_ownership'] == 'LLC': - score_weight_4 += 5 + score_weight[3] += 5 elif details_list['legal_ownership'] == 'Corporate': - score_weight_4 += 5 + score_weight[3] += 5 elif details_list['legal_ownership'] == 'Non-Profit': - score_weight_4 += 3 + score_weight[3] += 3 else: - score_weight_4 += 0 - - final_criteria_score.append((1, score_weight_1)) - final_criteria_score.append((2, score_weight_2)) - final_criteria_score.append((3, score_weight_3)) - final_criteria_score.append((4, score_weight_4)) - - return final_criteria_score + score_weight[3] += 0 + + final_criteria_scores.append((1, score_weight[0])) + final_criteria_scores.append((2, score_weight[1])) + final_criteria_scores.append((3, score_weight[2])) + final_criteria_scores.append((4, score_weight[3])) + + return final_criteria_scores -- GitLab From 85d9727278522c61dc01249cc04175f58dac7261 Mon Sep 17 00:00:00 2001 From: RujitRaval Date: Tue, 27 Aug 2019 12:41:16 -0400 Subject: [PATCH 2/2] Code Refector --- bpeng/pna/score_calculation.py | 46 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/bpeng/pna/score_calculation.py b/bpeng/pna/score_calculation.py index 927d1c1..8b58ca0 100644 --- a/bpeng/pna/score_calculation.py +++ b/bpeng/pna/score_calculation.py @@ -1,6 +1,7 @@ from .discrete_bar_graph import create_graph_factory def calculate_score_weight(weight_list, details_list): + score_weight = [0, 0, 0, 0] final_criteria_scores = [] @@ -9,31 +10,30 @@ def calculate_score_weight(weight_list, details_list): score_weight[index] += weight_list[i][1] heating_violations_score = [0, 1, 3, 5] - num = [0,1,3,11] - for i in range(len([0, 1, 3, 11])): - if not details_list['num_of_heating_violations'] > num[i]: - score_weight[0] += heating_violations_score[i] + num = [0, 1, 3, 11] + for i in range(len(num)): + initial_value = heating_violations_score[i] + if not i == len(num)-1 and details_list['num_of_heating_violations'] < num[i+1]: + break + score_weight[0] += initial_value dob_violations_score = [5, 4, 3, 4, 5] num = [0, 1, 4, 20, 100] - for i in range(len([0, 1, 4, 20, 100])): - if not details_list['num_of_dob_violations'] > num[i]: - score_weight[3] += dob_violations_score[i] - - if details_list['legal_ownership'] == 'Single': - score_weight[3] += 3 - elif details_list['legal_ownership'] == 'LLC': - score_weight[3] += 5 - elif details_list['legal_ownership'] == 'Corporate': - score_weight[3] += 5 - elif details_list['legal_ownership'] == 'Non-Profit': - score_weight[3] += 3 - else: - score_weight[3] += 0 - - final_criteria_scores.append((1, score_weight[0])) - final_criteria_scores.append((2, score_weight[1])) - final_criteria_scores.append((3, score_weight[2])) - final_criteria_scores.append((4, score_weight[3])) + for i in range(len(num)): + initial_value = dob_violations_score[i] + if not i == len(num)-1 and details_list['num_of_dob_violations'] < num[i+1]: + break + score_weight[3] += initial_value + + ownership_scores = { + 'Single': 3, + 'LLC': 5, + 'Corporate': 5, + 'Non-Profit': 3 + } + score_weight[3] += ownership_scores[details_list['legal_ownership']] + + for index in range(len(score_weight)): + final_criteria_scores.append((index+1, score_weight[index])) return final_criteria_scores -- GitLab