Skip to content

Commit acb90cc

Browse files
authored
Updated Test Suite to Include Documentation and More! (#52)
* Added article link * Automated the saving of the plot * Added list emptying visualization * Reorganized file to include main * Added doc strings * Created a function which executes all tests * Executed minor code cleanup
1 parent c0dce75 commit acb90cc

File tree

5 files changed

+108
-24
lines changed

5 files changed

+108
-24
lines changed

testing/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import glob
2+
from os.path import dirname, basename, isfile, join
3+
4+
modules = glob.glob(join(dirname(__file__), "*.py"))
5+
__all__ = [basename(f)[:-3] for f in modules if
6+
isfile(f) and not f.endswith('__init__.py') and not f.endswith("test_bench.py")]

testing/how_to_capitalize_a_string.py

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,97 @@
1+
"""
2+
Tests the performance of all of the solutions listed in the following article:
3+
https://therenegadecoder.com/code/how-to-capitalize-a-string-in-python/
4+
"""
5+
16
from test_bench import test_bench
27

3-
def control(string):
8+
9+
def control(_) -> None:
10+
"""
11+
Provides a control scenario for testing. In this case, none of the functions
12+
share any overhead, so this function is empty.
13+
14+
:param _: a placeholder for the string input
15+
:return: None
16+
"""
417
pass
518

6-
def capitalize_by_hand(string):
19+
20+
def capitalize_by_hand(string: str) -> str:
21+
"""
22+
Capitalizes a string by performing a code point shift
23+
for the first character.
24+
25+
:param string: an input string
26+
:return: the capitalized string
27+
"""
728
character = string[0]
829
if 97 <= ord(character) <= 122:
9-
shift = ord(character) - 32
30+
shift = ord(character) - 32
1031
uppercase = chr(shift)
1132
return uppercase + string[1:]
1233
return string
13-
14-
def capitalize_by_mapping(string):
34+
35+
36+
def capitalize_by_mapping(string: str) -> str:
37+
"""
38+
Capitalizes a string by mapping between a set of lowercase
39+
characters and a set of uppercase characters.
40+
41+
:param string: an input string
42+
:return: the capitalized string
43+
"""
1544
lowercase = "abcdefghijklmnopqrstuvwxyz"
1645
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1746
character = string[0]
1847
i = lowercase.find(character)
1948
return string if i == -1 else uppercase[i] + string[1:]
2049

21-
def capitalize_with_upper(string):
50+
51+
def capitalize_with_upper(string: str) -> str:
52+
"""
53+
Capitalizes a string by leveraging the upper() method of
54+
strings on the first character.
55+
56+
:param string: an input string
57+
:return: the capitalized string
58+
"""
2259
character = string[0]
2360
return character.upper() + string[1:]
2461

25-
def capitalize(string):
62+
63+
def capitalize(string: str) -> str:
64+
"""
65+
Capitalizes a string by leveraging the capitalize() method.
66+
The behavior of this function differs from he previous
67+
functions because the capitalize() method also converts all
68+
other characters int he string to lowercase.
69+
70+
:param string: an input string
71+
:return: the capitalized string
72+
"""
2673
return string.capitalize()
2774

2875

29-
test_bench(
30-
[
31-
control,
32-
capitalize_by_hand,
33-
capitalize_by_mapping,
34-
capitalize_with_upper,
35-
capitalize
36-
],
37-
{
38-
"One Letter String": ["a"],
39-
"Small String": ["how now brown cow"],
40-
"Large String": ["One Punch Man" * 100]
41-
}
42-
)
76+
def main() -> None:
77+
"""
78+
Tests the performance of all the functions defined in this file.
79+
"""
80+
test_bench(
81+
[
82+
control,
83+
capitalize_by_hand,
84+
capitalize_by_mapping,
85+
capitalize_with_upper,
86+
capitalize
87+
],
88+
{
89+
"One Letter String": ["a"],
90+
"Small String": ["how now brown cow"],
91+
"Large String": ["One Punch Man" * 100]
92+
}
93+
)
94+
95+
96+
if __name__ == '__main__':
97+
main()

testing/test_bench.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
import importlib
2+
import inspect
3+
import os
14
import timeit
2-
import seaborn as sns
3-
import pandas as pd
5+
46
import matplotlib.pyplot as plt
7+
import pandas as pd
8+
import seaborn as sns
9+
10+
import testing
11+
12+
13+
def run_suite() -> None:
14+
"""
15+
An experimental function which allows us to run the main function of
16+
all of our test files.
17+
18+
:return: None
19+
"""
20+
for module_name in testing.__all__:
21+
module = importlib.import_module(module_name)
22+
module.main()
523

624

725
def test_bench(funcs: list, test_data: dict):
@@ -70,4 +88,9 @@ def _show_results(results: pd.DataFrame):
7088
plt.title("How to Python: Function Performance Comparison", fontsize=16)
7189
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, title="Functions", fontsize='12', title_fontsize='12')
7290
plt.tight_layout()
73-
plt.show()
91+
filename = os.path.splitext(os.path.basename(inspect.stack()[2].filename))[0]
92+
plt.savefig(f"{os.path.join('visualizations', filename)}.png")
93+
94+
95+
if __name__ == '__main__':
96+
run_suite()
Loading
53.4 KB
Loading

0 commit comments

Comments
 (0)