Skip to content

Commit fef0ca9

Browse files
authored
minor performance tweaks (#12)
* append method * tweaks * version bump
1 parent 1aeb016 commit fef0ca9

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "simple-html"
3-
version = "1.2.1"
3+
version = "1.2.2"
44
readme = "README.md"
55
description = "Template-less html rendering in Python"
66
authors = ["Keith Philpott <fakekeith@example.org>"]

simple_html/__init__.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from html import escape
22
from types import GeneratorType
3-
from typing import Tuple, Union, Dict, List, FrozenSet, Generator, Iterable, Any
3+
from typing import Tuple, Union, Dict, List, FrozenSet, Generator, Iterable, Any, Callable
44

55

66
class SafeString:
@@ -128,9 +128,9 @@ def __call__(
128128
# attributes values are always escaped (when they are `str`s)
129129
if key not in _common_safe_attribute_names:
130130
key = (
131-
key.safe_str
132-
if isinstance(key, SafeString)
133-
else escape_attribute_key(key)
131+
escape_attribute_key(key)
132+
if isinstance(key, str)
133+
else key.safe_str
134134
)
135135

136136
if isinstance(val, str):
@@ -266,25 +266,25 @@ def __call__(
266266
wbr = Tag("wbr")
267267

268268

269-
def _render(nodes: Iterable[Node], strs: List[str]) -> None:
269+
def _render(nodes: Iterable[Node], append_to_list: Callable[[str], None]) -> None:
270270
"""
271271
mutate a list instead of constantly rendering strings
272272
"""
273273
for node in nodes:
274274
if type(node) is tuple:
275-
strs.append(node[0])
276-
_render(node[1], strs)
277-
strs.append(node[2])
275+
append_to_list(node[0])
276+
_render(node[1], append_to_list)
277+
append_to_list(node[2])
278278
elif isinstance(node, SafeString):
279-
strs.append(node.safe_str)
279+
append_to_list(node.safe_str)
280280
elif isinstance(node, str):
281-
strs.append(escape(node))
281+
append_to_list(escape(node))
282282
elif isinstance(node, Tag):
283-
strs.append(node.rendered)
283+
append_to_list(node.rendered)
284284
elif isinstance(node, list):
285-
_render(node, strs)
285+
_render(node, append_to_list)
286286
elif isinstance(node, GeneratorType):
287-
_render(node, strs)
287+
_render(node, append_to_list)
288288
else:
289289
raise TypeError(f"Got unknown type: {type(node)}")
290290

@@ -526,6 +526,6 @@ def render_styles(
526526

527527
def render(*nodes: Node) -> str:
528528
results: List[str] = []
529-
_render(nodes, results)
529+
_render(nodes, results.append)
530530

531531
return "".join(results)

0 commit comments

Comments
 (0)