Skip to content

Commit 0c6805f

Browse files
committed
Start to clean up
1 parent cf67a2e commit 0c6805f

File tree

5 files changed

+37
-40
lines changed

5 files changed

+37
-40
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,15 +599,12 @@ used internally by the `html()` function but can also be used independently:
599599

600600
```python
601601
from string.templatelib import Interpolation
602-
from tdom.utils import format_interpolation, convert
602+
from tdom.utils import convert
603603

604604
# Test convert function
605605
assert convert("hello", "s") == "hello"
606606
assert convert("hello", "r") == "'hello'"
607607
assert convert(42, None) == 42
608-
609-
# format_interpolation is used internally for custom format specifiers
610-
# The html() function uses this to implement :safe and :unsafe specifiers
611608
```
612609

613610
**`convert()`**: Applies conversion specifiers (`!a`, `!r`, `!s`) to values

tdom/processor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
TText,
2828
)
2929
from .placeholders import TemplateRef
30-
from .templating import format_interpolation, render_template_as_f
31-
from .utils import CachableTemplate, LastUpdatedOrderedDict, template_from_parts
30+
from .templating import format_interpolation, render_template_as_f, template_from_parts
31+
from .utils import CachableTemplate, LastUpdatedOrderedDict
3232

3333

3434
@t.runtime_checkable

tdom/templating.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from markupsafe import Markup
66

7-
from .utils import format_interpolation as base_format_interpolation
7+
from .utils import base_format_interpolation
88

99

1010
def _format_safe(value: object, format_spec: str) -> str:
@@ -29,6 +29,17 @@ def format_interpolation(interpolation: Interpolation) -> object:
2929
)
3030

3131

32+
def template_from_parts(
33+
strings: t.Sequence[str], interpolations: t.Sequence[Interpolation]
34+
) -> Template:
35+
"""Construct a template string from the given strings and parts."""
36+
assert len(strings) == len(interpolations) + 1, (
37+
"TemplateRef must have one more string than interpolation references."
38+
)
39+
flat = [x for pair in zip(strings, interpolations) for x in pair] + [strings[-1]]
40+
return Template(*flat)
41+
42+
3243
def render_template_as_f(template: Template) -> str:
3344
"""Fully render a template by formatting its interpolations."""
3445
parts: list[str] = []

tdom/utils.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _format_interpolation(
6565
return converted
6666

6767

68-
def format_interpolation(
68+
def base_format_interpolation(
6969
interpolation: Interpolation,
7070
*,
7171
formatters: t.Sequence[MatcherAndFormatter] = tuple(),
@@ -89,15 +89,12 @@ def format_interpolation(
8989
)
9090

9191

92-
def template_from_parts(
93-
strings: t.Sequence[str], interpolations: t.Sequence[Interpolation]
94-
) -> Template:
95-
"""Construct a template string from the given strings and parts."""
96-
assert len(strings) == len(interpolations) + 1, (
97-
"TemplateRef must have one more string than interpolation references."
98-
)
99-
flat = [x for pair in zip(strings, interpolations) for x in pair] + [strings[-1]]
100-
return Template(*flat)
92+
class LastUpdatedOrderedDict(OrderedDict):
93+
"Store items in the order the keys were last updated."
94+
95+
def __setitem__(self, key, value):
96+
super().__setitem__(key, value)
97+
self.move_to_end(key)
10198

10299

103100
class CachableTemplate:
@@ -115,11 +112,3 @@ def __eq__(self, other: object) -> bool:
115112

116113
def __hash__(self) -> int:
117114
return hash(self.template.strings)
118-
119-
120-
class LastUpdatedOrderedDict(OrderedDict):
121-
"Store items in the order the keys were last updated."
122-
123-
def __setitem__(self, key, value):
124-
super().__setitem__(key, value)
125-
self.move_to_end(key)

tdom/utils_test.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from string.templatelib import Interpolation
22

3-
from .utils import convert, format_interpolation
3+
from .utils import base_format_interpolation, convert
44

55

66
class Convertible:
@@ -32,50 +32,50 @@ def test_convert_s():
3232
assert convert(value, "s") == "Convertible str"
3333

3434

35-
def test_format_interpolation_no_formatting():
35+
def test_base_format_interpolation_no_formatting():
3636
value = Convertible()
3737
interp = Interpolation(value, expression="", conversion=None, format_spec="")
38-
assert format_interpolation(interp) is value
38+
assert base_format_interpolation(interp) is value
3939

4040

41-
def test_format_interpolation_a():
41+
def test_base_format_interpolation_a():
4242
value = Convertible()
4343
interp = Interpolation(value, expression="", conversion="a", format_spec="")
44-
assert format_interpolation(interp) == "Convertible repr"
44+
assert base_format_interpolation(interp) == "Convertible repr"
4545

4646

47-
def test_format_interpolation_r():
47+
def test_base_format_interpolation_r():
4848
value = Convertible()
4949
interp = Interpolation(value, expression="", conversion="r", format_spec="")
50-
assert format_interpolation(interp) == "Convertible repr"
50+
assert base_format_interpolation(interp) == "Convertible repr"
5151

5252

53-
def test_format_interpolation_s():
53+
def test_base_format_interpolation_s():
5454
value = Convertible()
5555
interp = Interpolation(value, expression="", conversion="s", format_spec="")
56-
assert format_interpolation(interp) == "Convertible str"
56+
assert base_format_interpolation(interp) == "Convertible str"
5757

5858

59-
def test_format_interpolation_default_formatting():
59+
def test_base_format_interpolation_default_formatting():
6060
value = 42
6161
interp = Interpolation(value, expression="", conversion=None, format_spec="5d")
62-
assert format_interpolation(interp) == " 42"
62+
assert base_format_interpolation(interp) == " 42"
6363

6464

65-
def test_format_interpolation_custom_formatter_match_exact():
65+
def test_base_format_interpolation_custom_formatter_match_exact():
6666
value = 42
6767
interp = Interpolation(value, expression="", conversion=None, format_spec="custom")
6868

6969
def formatter(val: object, spec: str) -> str:
7070
return f"formatted-{val}-{spec}"
7171

7272
assert (
73-
format_interpolation(interp, formatters=[("custom", formatter)])
73+
base_format_interpolation(interp, formatters=[("custom", formatter)])
7474
== "formatted-42-custom"
7575
)
7676

7777

78-
def test_format_interpolation_custom_formatter_match_predicate():
78+
def test_base_format_interpolation_custom_formatter_match_predicate():
7979
value = 42
8080
interp = Interpolation(
8181
value, expression="", conversion=None, format_spec="custom123"
@@ -88,6 +88,6 @@ def formatter(val: object, spec: str) -> str:
8888
return f"formatted-{val}-{spec}"
8989

9090
assert (
91-
format_interpolation(interp, formatters=[(matcher, formatter)])
91+
base_format_interpolation(interp, formatters=[(matcher, formatter)])
9292
== "formatted-42-custom123"
9393
)

0 commit comments

Comments
 (0)