Skip to content

Commit a81d464

Browse files
committed
Cache parsing result
1 parent 9ad00d9 commit a81d464

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

datauri/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
from urllib.parse import quote, unquote
1515

16+
from cached_property import cached_property
17+
1618
from .exceptions import InvalidCharset, InvalidDataURI, InvalidMimeType
1719

1820
T = TypeVar("T")
@@ -131,7 +133,7 @@ def is_valid(self) -> bool:
131133
return False
132134
return True
133135

134-
@property
136+
@cached_property
135137
def _parse(
136138
self,
137139
) -> Tuple[Optional[str], Optional[str], Optional[str], bool, bytes]:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def read(fname):
3939
],
4040
test_suite="tests",
4141
install_requires=[
42+
"cached-property",
4243
"typing_extensions",
4344
],
4445
)

tests/test_parsing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from pathlib import Path
3+
from unittest.mock import patch
34

45
import pytest
56

@@ -14,6 +15,15 @@ def test_parse():
1415
DataURI(t)
1516

1617

18+
@patch("datauri.unquote", return_value="The quick brown fox jumped over the lazy dog.")
19+
def test_parse_cached(_parse):
20+
t = "data:text/plain;charset=utf-8,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu"
21+
parsed = DataURI(t)
22+
assert parsed.charset is not None
23+
assert parsed.data is not None
24+
_parse.assert_called_once()
25+
26+
1727
def test_parse_base64():
1828
t = "data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu"
1929
DataURI(t)

0 commit comments

Comments
 (0)