From 96e19bbd1c5e087662867a716bef6db95972ff0c Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 26 Oct 2021 22:08:13 -0700 Subject: [PATCH 1/6] scoring_algorithm: Moves doctest into function docstring so it will be run --- other/scoring_algorithm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 77e614e2622c..095f2a83d5f8 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -20,9 +20,6 @@ lowest mileage but newest registration year. Thus the weights for each column are as follows: [0, 0, 1] - ->>> procentual_proximity([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]], [0, 0, 1]) -[[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]] """ @@ -33,6 +30,9 @@ def procentual_proximity(source_data: list, weights: list) -> list: possible values - 0 / 1 0 if lower values have higher weight in the data set 1 if higher values have higher weight in the data set + + >>> procentual_proximity([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]], [0, 0, 1]) + [[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]] """ # getting data From 67b33311923f19d7e6fc48d0681de6a3118beb68 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 26 Oct 2021 22:10:02 -0700 Subject: [PATCH 2/6] [mypy] annotates other/scoring_algorithm --- other/scoring_algorithm.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 095f2a83d5f8..428b3cf31176 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -23,7 +23,7 @@ """ -def procentual_proximity(source_data: list, weights: list) -> list: +def procentual_proximity(source_data: list[list[float]], weights: list[int]) -> list[list[float]]: """ weights - int list @@ -36,7 +36,7 @@ def procentual_proximity(source_data: list, weights: list) -> list: """ # getting data - data_lists = [] + data_lists: list[list[float]] = [] for item in source_data: for i in range(len(item)): try: @@ -46,13 +46,13 @@ def procentual_proximity(source_data: list, weights: list) -> list: data_lists.append([]) data_lists[i].append(float(item[i])) - score_lists = [] + score_lists: list[list[float]] = [] # calculating each score for dlist, weight in zip(data_lists, weights): mind = min(dlist) maxd = max(dlist) - score = [] + score: list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: @@ -75,7 +75,7 @@ def procentual_proximity(source_data: list, weights: list) -> list: score_lists.append(score) # initialize final scores - final_scores = [0 for i in range(len(score_lists[0]))] + final_scores: list[float] = [0 for i in range(len(score_lists[0]))] # generate final scores for i, slist in enumerate(score_lists): From 825e4e98b200c1de274dd891216ef34bb6af176b Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 26 Oct 2021 22:13:11 -0700 Subject: [PATCH 3/6] [mypy] renames temp var to unique value to work around mypy issue in other/scoring_algorithm reusing loop variables with the same name and different types gives this very confusing mypy error response. pyright correctly infers the types without issue. ``` scoring_algorithm.py:58: error: Incompatible types in assignment (expression has type "float", variable has type "List[float]") scoring_algorithm.py:60: error: Unsupported operand types for - ("List[float]" and "float") scoring_algorithm.py:65: error: Incompatible types in assignment (expression has type "float", variable has type "List[float]") scoring_algorithm.py:67: error: Unsupported operand types for - ("List[float]" and "float") Found 4 errors in 1 file (checked 1 source file) ``` --- other/scoring_algorithm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 428b3cf31176..00543e9f1d3b 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -37,14 +37,14 @@ def procentual_proximity(source_data: list[list[float]], weights: list[int]) -> # getting data data_lists: list[list[float]] = [] - for item in source_data: - for i in range(len(item)): + for data in source_data: + for i in range(len(data)): try: - data_lists[i].append(float(item[i])) + data_lists[i].append(float(data[i])) except IndexError: # generate corresponding number of lists data_lists.append([]) - data_lists[i].append(float(item[i])) + data_lists[i].append(float(data[i])) score_lists: list[list[float]] = [] # calculating each score From c50e70342a783469fd4cfabe0e57dcf746a64d83 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 26 Oct 2021 22:17:12 -0700 Subject: [PATCH 4/6] scoring_algorithm: uses enumeration instead of manual indexing on loop var --- other/scoring_algorithm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 00543e9f1d3b..5993e3b578ec 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -38,13 +38,13 @@ def procentual_proximity(source_data: list[list[float]], weights: list[int]) -> # getting data data_lists: list[list[float]] = [] for data in source_data: - for i in range(len(data)): + for i, el in enumerate(data): try: - data_lists[i].append(float(data[i])) + data_lists[i].append(float(el)) except IndexError: # generate corresponding number of lists data_lists.append([]) - data_lists[i].append(float(data[i])) + data_lists[i].append(float(el)) score_lists: list[list[float]] = [] # calculating each score From add94e8cef0384e719b94531af141ac92608f9e8 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 26 Oct 2021 22:22:38 -0700 Subject: [PATCH 5/6] scoring_algorithm: sometimes we look before we leap. --- other/scoring_algorithm.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 5993e3b578ec..e7e0bf3a95ab 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -39,12 +39,9 @@ def procentual_proximity(source_data: list[list[float]], weights: list[int]) -> data_lists: list[list[float]] = [] for data in source_data: for i, el in enumerate(data): - try: - data_lists[i].append(float(el)) - except IndexError: - # generate corresponding number of lists + if len(data_lists) < i + 1: data_lists.append([]) - data_lists[i].append(float(el)) + data_lists[i].append(float(el)) score_lists: list[list[float]] = [] # calculating each score From 44cbdb0748da8ebd93ea0e929df127063334fc1f Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 26 Oct 2021 23:47:36 -0700 Subject: [PATCH 6/6] clean-up: runs `black` to fix formatting --- other/scoring_algorithm.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index e7e0bf3a95ab..cc1744012671 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -23,7 +23,9 @@ """ -def procentual_proximity(source_data: list[list[float]], weights: list[int]) -> list[list[float]]: +def procentual_proximity( + source_data: list[list[float]], weights: list[int] +) -> list[list[float]]: """ weights - int list