Skip to content

Commit 0223d6a

Browse files
authored
Add benchmarks and Optimize (#3)
* wip * benchmarking * wip * simplify * remove unused * line breaks * wip * wip * faster * faster * fix benchmarks * wip * wip * bench type fixes * faster * tweak * simpler * remove comment * join later * list instead of flatgroup * remove backticks * update mypy * remove func * wip * allow generators * update github actions * versions * poetry run * old_typehints * fix * lorem ipsum * no attrs tag * remove tag * remove None * clean * wip * wip * paren removal * fix tests * faster * slots * allow none * faster tags * tweak * wip * remove unused import * wip * tag_start * wip * tweak * wip * wip * remove render_with_doctype * some other benchmarks
1 parent 624bedc commit 0223d6a

20 files changed

Lines changed: 771 additions & 367 deletions

.DS_Store

8 KB
Binary file not shown.

.github/workflows/push.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ jobs:
2525
run: poetry run mypy simple_html
2626
- name: run tests
2727
run: poetry run pytest
28+
- name: run bench (pure python)
29+
run: poetry run python -m bench.run
30+
- name: compile
31+
run: poetry run mypyc simple_html
32+
- name: run bench (compiled)
33+
run: poetry run python -m bench.run

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,35 @@ normal Python. In most cases, the code will be more concise than standard HTML.
1515

1616

1717
### Usage
18+
1819
```python
1920
from simple_html.nodes import body, head, html, p
2021
from simple_html.render import render
2122

2223
node = html(
2324
head,
2425
body(
25-
p.attrs(id="hello")(
26+
p.attrs(id="hello")(
2627
"Hello World!"
2728
)
2829
)
2930
)
3031

31-
render(node) # returns: <html><head></head><body><p id="hello">Hello World!</p></body></html>
32+
render(
33+
node) # returns: <html><head></head><body><p id="hello">Hello World!</p></body></html>
3234
```
3335

3436

3537
Strings are escaped by default, but you can pass in `SafeString`s to avoid escaping.
3638

3739
```python
38-
from simple_html.nodes import br, p, SafeString
40+
from simple_html.nodes import br, p, safe_string
3941
from simple_html.render import render
4042

4143
node = p(
4244
"Escaped & stuff",
4345
br,
44-
SafeString("Not escaped & stuff")
46+
safe_string("Not escaped & stuff")
4547
)
4648

4749
render(node) # returns: <p>Escaped &amp; stuff<br/>Not escaped & stuff</p>
@@ -50,10 +52,10 @@ render(node) # returns: <p>Escaped &amp; stuff<br/>Not escaped & stuff</p>
5052
For convenience, many tags are provided, but you can create your own as well:
5153

5254
```python
53-
from simple_html.nodes import TagBase
55+
from simple_html.nodes import Tag
5456
from simple_html.render import render
5557

56-
custom_elem = TagBase("custom-elem")
58+
custom_elem = Tag("custom-elem")
5759

5860
render(
5961
custom_elem.attrs(id="some-custom-elem-id")(

bench/current_code.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

bench/dom.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import List
2+
3+
from dominate.tags import h1
4+
5+
6+
def hello_world_empty(objs: List[None]) -> None:
7+
for _ in objs:
8+
h1("Hello, World!").render()

bench/fast.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import List
2+
3+
from fast_html import h1, render
4+
5+
6+
def hello_world_empty(objs: List[None]) -> None:
7+
for _ in objs:
8+
render(h1("Hello, World!"))

bench/jin.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
from pathlib import Path
3+
from typing import List, Tuple
4+
5+
from jinja2 import Environment, PackageLoader, select_autoescape
6+
7+
sys.path.append(str(Path(__file__).parent))
8+
9+
env = Environment(loader=PackageLoader("jinja_example"), autoescape=select_autoescape())
10+
11+
12+
def hello_world_empty(objs: List[None]) -> None:
13+
for _ in objs:
14+
env.get_template("hello_world.html").render()
15+
16+
17+
def basic(objs: List[Tuple[str, str, List[str]]]) -> None:
18+
for title, content, oks in objs:
19+
env.get_template("basic.html").render(title=title, content=content, oks=oks)
20+
21+
22+
def basic_long(objs: List[Tuple[str, str, List[str]]]) -> None:
23+
for title, content, oks in objs:
24+
env.get_template("basic_long.html").render(
25+
title=title, content=content, oks=oks
26+
)
27+
28+
29+
def lorem_ipsum(titles: List[str]) -> None:
30+
for t in titles:
31+
env.get_template("lorem_ipsum.html").render(title=t)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
<head><title>{title}</title></head>
4+
<body>
5+
<h1 class="great header" other_attr="5" id="header1"></h1>
6+
<div><p>{content}<br><br></p>
7+
<ul>
8+
{% for item in oks %}
9+
<li class="item-stuff">{{ item|safe }}}</li>
10+
{% endfor %}
11+
</ul>
12+
</div>
13+
</body>
14+
</html>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!doctype html>
2+
<html>
3+
<head><title>{title}</title></head>
4+
<body>
5+
<h1 class="great header" other_attr="5" id="header1"></h1>
6+
<div><p>{content}<br><br></p>
7+
<ul>
8+
{% for item in oks %}
9+
<li class="item-stuff">{{ item|safe }}}</li>
10+
{% endfor %}
11+
</ul>
12+
</div>
13+
<h1 class="great header" other_attr="5" id="header1"></h1>
14+
<div><p>{content}<br><br></p>
15+
<ul>
16+
{% for item in oks %}
17+
<li class="item-stuff">{{ item|safe }}}</li>
18+
{% endfor %}
19+
</ul>
20+
</div>
21+
<h1 class="great header" other_attr="5" id="header1"></h1>
22+
<div><p>{content}<br><br></p>
23+
<ul>
24+
{% for item in oks %}
25+
<li class="item-stuff">{{ item|safe }}}</li>
26+
{% endfor %}
27+
</ul>
28+
</div>
29+
<h1 class="great header" other_attr="5" id="header1"></h1>
30+
<div><p>{content}<br><br></p>
31+
<ul>
32+
{% for item in oks %}
33+
<li class="item-stuff">{{ item|safe }}}</li>
34+
{% endfor %}
35+
</ul>
36+
</div>
37+
<h1 class="great header" other_attr="5" id="header1"></h1>
38+
<div><p>{content}<br><br></p>
39+
<ul>
40+
{% for item in oks %}
41+
<li class="item-stuff">{{ item|safe }}}</li>
42+
{% endfor %}
43+
</ul>
44+
</div>
45+
</body>
46+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello, World!</h1>

0 commit comments

Comments
 (0)