Skip to content

Commit c0dce75

Browse files
authored
Added Testing Documentation (#51)
* PEP8'd the Testing Code * Updated file formatting * Adding documentation to test bench * Added yet more documentation * Finished documenting the test bench
1 parent abe46ca commit c0dce75

File tree

8 files changed

+100
-12
lines changed

8 files changed

+100
-12
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/how-to-python-code.iml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testing/how_to_empty_a_list.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from test_bench import test_bench
77

8-
def control(anime: list(str)) -> None:
8+
9+
def control(anime: list[str]) -> None:
910
"""
1011
Provides a control scenario for testing. In this case, all of the solutions
1112
rely on copying the input list, so the control function accounts for that.
@@ -14,7 +15,8 @@ def control(anime: list(str)) -> None:
1415
"""
1516
anime = anime.copy()
1617

17-
def empty_list_by_hand(anime: list(str)) -> None:
18+
19+
def empty_list_by_hand(anime: list[str]) -> None:
1820
"""
1921
Empties a list by repeatedly removing elements from a list.
2022
@@ -24,16 +26,18 @@ def empty_list_by_hand(anime: list(str)) -> None:
2426
while anime:
2527
anime.pop()
2628

27-
def empty_list_by_assignment(anime: list(str)) -> None:
29+
30+
def empty_list_by_assignment(anime: list[str]) -> None:
2831
"""
2932
Empties a list by reassigning the reference.
3033
3134
:param anime: a list of anime
3235
"""
3336
anime = anime.copy()
34-
anime = [] # Wouldn't actually work as a function
37+
anime = [] # Wouldn't actually work as a function
3538

36-
def empty_list_by_clear(anime: list(str)) -> None:
39+
40+
def empty_list_by_clear(anime: list[str]) -> None:
3741
"""
3842
Empties a list by calling the clear method.
3943
@@ -42,7 +46,8 @@ def empty_list_by_clear(anime: list(str)) -> None:
4246
anime = anime.copy()
4347
anime.clear()
4448

45-
def empty_list_by_del(anime: list(str)) -> None:
49+
50+
def empty_list_by_del(anime: list[str]) -> None:
4651
"""
4752
Empties a list by deleting a slice of the list.
4853
@@ -51,7 +56,8 @@ def empty_list_by_del(anime: list(str)) -> None:
5156
anime = anime.copy()
5257
del anime[:]
5358

54-
def empty_list_by_slice_assignment(anime: list(str)) -> None:
59+
60+
def empty_list_by_slice_assignment(anime: list[str]) -> None:
5561
"""
5662
Empties a list by replacing a slice of the list with an empty list.
5763
@@ -60,7 +66,8 @@ def empty_list_by_slice_assignment(anime: list(str)) -> None:
6066
anime = anime.copy()
6167
anime[:] = []
6268

63-
def empty_list_by_multiplication(anime: list(str)) -> None:
69+
70+
def empty_list_by_multiplication(anime: list[str]) -> None:
6471
"""
6572
Empties a list by multiplication.
6673
@@ -69,6 +76,7 @@ def empty_list_by_multiplication(anime: list(str)) -> None:
6976
anime = anime.copy()
7077
anime *= 0 # Also, would not work as a function
7178

79+
7280
def main() -> None:
7381
"""
7482
Tests the performance of all the functions defined in this file.
@@ -90,6 +98,7 @@ def main() -> None:
9098
"Large List": [["One Punch Man"] * 100]
9199
}
92100
)
93-
101+
102+
94103
if __name__ == "__main__":
95104
main()

testing/test_bench.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@
22
import seaborn as sns
33
import pandas as pd
44
import matplotlib.pyplot as plt
5-
import copy
5+
66

77
def test_bench(funcs: list, test_data: dict):
8+
"""
9+
Given a list of functions and a list of dictionary of test data,
10+
this function will execute performance testing using all of the items
11+
in the dictionary on all of the functions provided.
12+
13+
Dictionary must be in the form of the following:
14+
15+
{"test_1_name": ["list", "of", "arguments"]}
16+
17+
:param funcs: a list of functions
18+
:param test_data: a dictionary of test data
19+
"""
820
results = _test_performance(funcs, test_data)
921
_show_results(results)
1022

23+
1124
def _test_performance(funcs: list, test_data: dict) -> pd.DataFrame:
25+
"""
26+
Given a list of functions and a dictionary of test data, this function
27+
creates a DataFrame containing the name of the function, the input to
28+
that function, and the resulting performance of that combination. A row
29+
is generated for all possible combinations of functions and test data items.
30+
31+
:param funcs: a list of functions
32+
:param test_data: a dictionary of test data
33+
"""
1234
num_tests = len(funcs) * len(test_data)
1335
print(f"> Collecting {num_tests} test(s)")
1436
results = []
@@ -24,11 +46,27 @@ def _test_performance(funcs: list, test_data: dict) -> pd.DataFrame:
2446
print(f"> Testing Complete")
2547
return pd.DataFrame(results, columns=["Function", "Input", "Performance"])
2648

49+
2750
def _show_results(results: pd.DataFrame):
28-
print(results.to_string())
51+
"""
52+
Given a DataFrame of performance testing results, this function
53+
plots the results in a figure. In addition, it dumps the results as a string.
54+
55+
:param results: a DataFrame containing the results of a performance test
56+
"""
57+
print(results.to_string())
2958
sns.set_theme()
3059
with sns.plotting_context("paper", font_scale=1.5):
31-
sns.catplot(x="Input", y="Performance", hue="Function", kind="bar", data=pd.DataFrame(results), legend=False, height=8, aspect=2)
60+
sns.catplot(
61+
x="Input",
62+
y="Performance",
63+
hue="Function",
64+
kind="bar",
65+
data=pd.DataFrame(results),
66+
legend=False,
67+
height=8,
68+
aspect=2
69+
)
3270
plt.title("How to Python: Function Performance Comparison", fontsize=16)
3371
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, title="Functions", fontsize='12', title_fontsize='12')
3472
plt.tight_layout()

0 commit comments

Comments
 (0)