-
-
Save ParitoshSingh07/6cdf332453ead76dafd022a3a39d86d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Sun Nov 17 15:55:04 2019 | |
@author: Pari | |
""" | |
from jinja2 import Template | |
import numpy as np | |
import string | |
import time | |
table_headers = list(range(30)) | |
table_body_letters = np.random.choice(list(string.ascii_letters), (5000, 20, 30)) | |
table_body = [] | |
for row in table_body_letters: | |
rebuilt_row = [] | |
for column in row: | |
rebuilt_row.append(''.join(column)) | |
table_body.append(rebuilt_row) | |
def by_hand(table_headers, table_body): | |
string_by_hand = """ | |
<table class="table"> | |
<thead>""" | |
for header in table_headers: | |
string_by_hand += f"<th>{ header }</th>" | |
string_by_hand += """ </thead> | |
<tbody>""" | |
for row in table_body: | |
string_by_hand += "<tr>" | |
for column in row: | |
string_by_hand += f"<td>{ column }</td>" | |
string_by_hand += "</tr>" | |
string_by_hand += """ </tbody> | |
</table>""" | |
tm = Template("{{string_by_hand}}") | |
msg = tm.render(string_by_hand=string_by_hand) | |
return msg | |
def by_hand_semi_optimized(table_headers, table_body): | |
string_by_hand = """ | |
<table class="table"> | |
<thead>""" | |
temp = [f"<th>{ header }</th>" for header in table_headers] | |
string_by_hand += ''.join(temp) | |
string_by_hand += """ </thead> | |
<tbody>""" | |
for row in table_body: | |
string_by_hand += "<tr>" | |
temp = [f"<td>{ column }</td>" for column in row] | |
string_by_hand += ''.join(temp) | |
string_by_hand += "</tr>" | |
string_by_hand += """ </tbody> | |
</table>""" | |
tm = Template("{{string_by_hand}}") | |
msg = tm.render(string_by_hand=string_by_hand) | |
return msg | |
def by_hand_optimized(table_headers, table_body): | |
string_by_hand = """ | |
<table class="table"> | |
<thead>""" | |
temp = [f"<th>{ header }</th>" for header in table_headers] | |
string_by_hand += ''.join(temp) | |
string_by_hand += """ </thead> | |
<tbody>""" | |
temp = ["<tr>" + ''.join([f"<td>{ column }</td>" for column in row]) + "</tr>" | |
for row in table_body] | |
string_by_hand += ''.join(temp) | |
string_by_hand += """ </tbody> | |
</table>""" | |
tm = Template("{{string_by_hand}}") | |
msg = tm.render(string_by_hand=string_by_hand) | |
return msg | |
def by_jinja(table_headers, table_body): | |
template_string = """ | |
<table class="table"> | |
<thead> | |
{% for header in headers %} | |
<th>{{ header }}</th> | |
{% endfor %} | |
</thead> | |
<tbody> | |
{% for row in table_body %} | |
<tr> | |
{% for column in row %} | |
<td>{{ column }}</td> | |
{% endfor %} | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
""" | |
tm = Template(template_string) | |
msg = tm.render(headers=table_headers, table_body=table_body) | |
return msg | |
func_list = [by_hand, | |
by_hand_semi_optimized, | |
by_hand_optimized, | |
by_jinja] | |
for func in func_list: | |
start = time.time() | |
out = func(table_headers, table_body) | |
end = time.time() | |
print(f"{func.__name__}: {end - start}s") | |
#%timeit by_hand(table_headers, table_body) | |
#3.12 ms ± 209 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) | |
# | |
#%timeit by_jinja(table_headers, table_body) | |
#4.76 ms ± 193 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) | |
%timeit by_hand(table_headers, table_body) | |
%timeit by_hand_optimized(table_headers, table_body) | |
%timeit by_hand_semi_optimized(table_headers, table_body) | |
%timeit by_jinja(table_headers, table_body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment