Skip to content

Commit 02c857f

Browse files
committed
Remove a bunch of barely used fields to save memory
1 parent 3b92b4a commit 02c857f

File tree

14 files changed

+191
-315
lines changed

14 files changed

+191
-315
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# 0.13.0
2+
- **BREAKING** Refactor Tag and implementation to significantly reduce memory usage
3+
- The Tag's `tag` is now a ClassVar instead of an atom member. Use a subclass to change tags.
4+
- The Tag's `clickable` and `draggable` are now removed. `clickable` is automatically inferred if a `clicked` handler is defined. `draggable` is inferred if `dragstart` is defined.
5+
- Due to their minimal usage `onclick`, `ondragstart`, `ondragover`, `ondragend`, `ondragenter` and `ondragleave` are removed. Use
6+
the `attrs` dict to define these instead.
7+
- The `alt` attribute was moved from `Tag` to `A` and `Img`, for other nodes use the `attrs` dict.
8+
- The `cls` attributes now only accept strings. These values are now internd to save memory
9+
- Tags now have a `find_by_id` method to retrieve a node by it's `id` field
10+
- Fix bug where an item removed attrs dict was not removed from the html
11+
- Any proxy `set_{attr}` methods now take a second argument that includes the oldvalue.
12+
- Optimize speedups extension
13+
114
# 0.12.1
215
- Support python 3.13
316
- Cleanup type errors and lint errors

tests/test_html.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ def test_hello_world(app):
5555
"tag, attr, query",
5656
(
5757
("Div", 'cls = "right"', '//div[@class="right"]'),
58-
("Div", 'cls = ["btn", "btn-large"]', '//div[@class="btn btn-large"]'),
58+
("Div", 'cls = "btn btn-large"', '//div[@class="btn btn-large"]'),
5959
("Span", 'style = "float: left;"', '//span[@style="float: left;"]'),
6060
(
6161
"Span",
6262
'style = {"background": "#fff", "color": "blue"}',
6363
'//span[(@style="background:#fff;color:blue" or '
6464
'@style="color:blue;background:#fff")]',
6565
),
66-
("Li", "clickable = True", '//li[@clickable="true"]'),
67-
("Li", "draggable = True", '//li[@draggable="true"]'),
66+
("Li", "clicked :: print('clicked')", '//li[@clickable="true"]'),
67+
("Li", "dragstart :: print('dragstart')", '//li[@draggable="true"]'),
6868
("Img", 'id = "logo"', '//img[@id="logo"]'),
6969
# Use attrs for special or non-python html attributes
7070
("Div", 'attrs = {"data-tooltip":"Tooltip"}', '//div[@data-tooltip="Tooltip"]'),
@@ -99,7 +99,6 @@ def test_html(app, tag, attr, query):
9999
"tag, attr, default, change, query",
100100
(
101101
("A", "href", '"#"', "/home/", '//a[@href="/home/"]'),
102-
("A", "tag", '"a"', "span", "//span"), # It's possible to change tags
103102
("Base", "href", '"#"', "/home/", '//base[@href="/home/"]'),
104103
("Blockquote", "cite", '"1"', "2", '//blockquote[@cite="2"]'),
105104
("Img", "width", '"100px"', "100%", '//img[@width="100%"]'),
@@ -127,15 +126,13 @@ def test_html(app, tag, attr, query):
127126
("P", "text", '"a"', "b", '//p[text()="b"]'),
128127
("P", "tail", '"a"', "b", '//body[text()="b"]'),
129128
("A", "attrs", {"data-x": "a"}, {"data-x": "b"}, '//a[@data-x="b"]'),
130-
("Button", "clickable", False, True, '//button[@clickable="true"]'),
131-
("Button", "draggable", True, False, '//button[@draggable="false"]'),
132-
("A", "onclick", '"a"', "b", '//a[@onclick="b"]'),
133-
("A", "ondragstart", '"a"', "b", '//a[@ondragstart="b"]'),
134-
("A", "ondragover", '"a"', "b", '//a[@ondragover="b"]'),
135-
("A", "ondragend", '"a"', "b", '//a[@ondragend="b"]'),
136-
("A", "ondragenter", '"a"', "b", '//a[@ondragenter="b"]'),
137-
("A", "ondragleave", '"a"', "b", '//a[@ondragleave="b"]'),
138-
("A", "ondrop", '"a"', "b", '//a[@ondrop="b"]'),
129+
(
130+
"A",
131+
"attrs",
132+
{"data-x": "a"},
133+
{"data-y": "b"},
134+
'//a[@data-y="b" and not(@data-x)]',
135+
),
139136
),
140137
)
141138
def test_html_change(app, tag, attr, default, change, query):
@@ -313,7 +310,7 @@ def test_markdown_proxy(app):
313310
"extensions_config",
314311
):
315312
with pytest.raises(NotImplementedError):
316-
getattr(proxy, "set_%s" % attr)(None)
313+
getattr(proxy, "set_%s" % attr)(None, None)
317314

318315

319316
def test_code(app):
@@ -358,7 +355,7 @@ def test_code_proxy(app):
358355
proxy = ProxyCode()
359356
for attr in ("language", "highlight_style"):
360357
with pytest.raises(NotImplementedError):
361-
getattr(proxy, "set_%s" % attr)(None)
358+
getattr(proxy, "set_%s" % attr)(None, None)
362359

363360

364361
@pytest.mark.skipif(SKIP_NBFORMAT, reason="nbformat is required")

tests/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
from web.components.html import Tag
44

55

6+
def test_intern_str():
7+
# Python normally does not intern anything that doesn't look like an attribute
8+
# Since cls names are likely to be reused they are automatically interned to save space
9+
t1 = Tag(cls="ms-2")
10+
assert t1.cls is "ms-2" # noqa: F632
11+
12+
613
def test_child_index():
714
with pytest.raises(ValueError):
815
lookup_child_index(None)

web/components/code.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class ProxyCode(ProxyRawNode):
1919
#: Reference to the declaration
2020
declaration = ForwardTyped(lambda: Code)
2121

22-
def set_language(self, language: str):
22+
def set_language(self, language: str, oldvalue: str):
2323
raise NotImplementedError
2424

25-
def set_highlight_style(self, style: str):
25+
def set_highlight_style(self, style: str, oldvalue: str):
2626
raise NotImplementedError
2727

2828

0 commit comments

Comments
 (0)