Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added event probability to function runner. Improved html export to handle multi-parameter funtions #2

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/export/html_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ def generate_html(input_values: list, function: callable) -> str:
html_text += "\n\t\t<h1>{}</h1>".format(function_name)
html_text += "\n\t\t<ul>"
for input_value in input_values:
if hasattr(input_value, '__iter__'):
result = function(*input_value)
input_to_print = ", ".join(["{}".format(i) for i in input_value])
else:
result = function(input_value)
input_to_print = "{}".format(input_value)

html_text += "\n\t\t\t<li>{}({}) = {}</li>".format(function_name,
input_value,
function(input_value))
input_to_print,
result)
html_text += "\n\t\t</ul>"
html_text += "\n\t</body>\n</html>"
return html_text
14 changes: 14 additions & 0 deletions src/function_runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from src.calculations.operations import fibonacci
from src.calculations.statistics import event_probability
from src.export.html_generator import generate_html


Expand All @@ -12,3 +13,16 @@ def get_fibonacci_series_html(maximum: int) -> str:
input_list = range(0, maximum)
html = generate_html(input_list, fibonacci)
return html


def get_event_probability_series_html(maximum: int) -> str:
"""
Get the html string showing the event probability calculations for the
numbers from zero to the selected maximum value as space size
:param maximum: limit for the series
:return: html string
"""
input_list = [(i, maximum) for i in range(0, maximum)]
html = generate_html(input_list, event_probability)
return html

10 changes: 10 additions & 0 deletions test/integration/test_function_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ def test_get_fibonacci_series_html():
assert result == "<!DOCTYPE html>\n<html>\n\t<body>\n\t\t" \
"<h1>fibonacci</h1>\n\t\t<ul>\n\t\t</ul>" \
"\n\t</body>\n</html>"


def test_get_get_event_probability_series_html():
result = src.function_runner.get_event_probability_series_html(3)
assert result == "<!DOCTYPE html>\n<html>\n\t<body>\n\t\t" \
"<h1>event_probability</h1>\n\t\t<ul>\n\t\t\t" \
"<li>event_probability(0, 3) = 0.0</li>\n\t\t\t" \
"<li>event_probability(1, 3) = 33.3</li>\n\t\t\t" \
"<li>event_probability(2, 3) = 66.7</li>\n\t\t" \
"</ul>\n\t</body>\n</html>"
18 changes: 17 additions & 1 deletion test/unit/export/test_html_generator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import src.export.html_generator


def test_generate_html():
def test_generate_html_single_parameter():
def calculate_square(x):
return x * x

Expand All @@ -15,3 +15,19 @@ def calculate_square(x):
"\n\t\t\t<li>calculate_square(3) = 9</li>" \
"\n\t\t</ul>" \
"\n\t</body>\n</html>"


def test_generate_html_multi_parameter():
def calculate_sum(x, y):
return x + y

input_list = [(1, 2), (3, 4)]
result = src.export.html_generator.generate_html(input_list,
calculate_sum)
assert result == "<!DOCTYPE html>\n<html>\n\t<body>" \
"\n\t\t<h1>calculate_sum</h1>"\
"\n\t\t<ul>" \
"\n\t\t\t<li>calculate_sum(1, 2) = 3</li>" \
"\n\t\t\t<li>calculate_sum(3, 4) = 7</li>" \
"\n\t\t</ul>" \
"\n\t</body>\n</html>"
Loading