mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
test out the Python Fluent implementation
The parsing step is considerably slower in Python, but if parsing is moved out of the test function, Python wins at 45ms to Rust's 67ms on 10,000 rounds, presumably due to the overhead of serializing to Protobuf. Not enough of a difference to justify the inclusion of extra dependencies and duplicating the lookup code in any case.
This commit is contained in:
parent
c395003def
commit
8abb35372a
2 changed files with 35 additions and 4 deletions
|
@ -23,5 +23,6 @@ setuptools.setup(
|
||||||
"protobuf",
|
"protobuf",
|
||||||
'psutil; sys_platform == "win32"',
|
'psutil; sys_platform == "win32"',
|
||||||
'distro; sys_platform != "darwin" and sys_platform != "win32"',
|
'distro; sys_platform != "darwin" and sys_platform != "win32"',
|
||||||
|
"fluent.runtime",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
@ -158,7 +158,37 @@ def test_translate():
|
||||||
def no_uni(s: str) -> str:
|
def no_uni(s: str) -> str:
|
||||||
return s.replace("\u2068", "").replace("\u2069", "")
|
return s.replace("\u2068", "").replace("\u2069", "")
|
||||||
|
|
||||||
|
def test_backend():
|
||||||
assert tr(StringsGroup.TEST, "valid-key") == "a valid key"
|
assert tr(StringsGroup.TEST, "valid-key") == "a valid key"
|
||||||
assert "invalid-key" in tr(StringsGroup.TEST, "invalid-key")
|
assert "invalid-key" in tr(StringsGroup.TEST, "invalid-key")
|
||||||
assert no_uni(tr(StringsGroup.TEST, "plural", hats=1)) == "You have 1 hat."
|
assert no_uni(tr(StringsGroup.TEST, "plural", hats=1)) == "You have 1 hat."
|
||||||
assert no_uni(tr(StringsGroup.TEST, "plural", hats=2)) == "You have 2 hats."
|
assert no_uni(tr(StringsGroup.TEST, "plural", hats=2)) == "You have 2 hats."
|
||||||
|
|
||||||
|
import time
|
||||||
|
t = time.time()
|
||||||
|
test_backend()
|
||||||
|
print(time.time() - t)
|
||||||
|
|
||||||
|
from fluent.runtime import FluentBundle, FluentResource
|
||||||
|
|
||||||
|
test_path = os.path.join(
|
||||||
|
os.path.dirname(__file__), "../../rslib/tests/support/test.ftl"
|
||||||
|
)
|
||||||
|
|
||||||
|
def python_render(bundle, key, **kwargs):
|
||||||
|
try:
|
||||||
|
return bundle.format_pattern(bundle.get_message(key).value, kwargs)[0]
|
||||||
|
except:
|
||||||
|
return f"Missing key: {key}"
|
||||||
|
|
||||||
|
def test_python():
|
||||||
|
bundle = FluentBundle(["en-US"])
|
||||||
|
bundle.add_resource(FluentResource(open(test_path).read()))
|
||||||
|
assert python_render(bundle, "valid-key") == "a valid key"
|
||||||
|
assert "invalid-key" in python_render(bundle, "invalid-key")
|
||||||
|
assert no_uni(python_render(bundle, "plural", hats=1)) == "You have 1 hat."
|
||||||
|
assert no_uni(python_render(bundle, "plural", hats=2)) == "You have 2 hats."
|
||||||
|
|
||||||
|
t = time.time()
|
||||||
|
test_python()
|
||||||
|
print(time.time() - t)
|
||||||
|
|
Loading…
Reference in a new issue