-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better readability for numbers in output (#89)
- Loading branch information
Showing
5 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Tuple, Union | ||
|
||
|
||
def _fmt_diff_part(s, d): | ||
return f"[numDiff]{s[d:]}[/numDiff]" if d < len(s) else "" | ||
|
||
|
||
def format_difference( | ||
n1: Union[float, int], n2: Union[float, int], decimal_separator: bool = True | ||
) -> Tuple[str, str]: | ||
""" | ||
Given two numbers, n1 and n2, return a tuple of two strings, | ||
each representing one of the input numbers with the differing part highlighted. | ||
Highlighting is done using BBCode-like tags, which are replaced by the formatter. | ||
Examples: | ||
123, 123.0 | ||
-> 123, 123[numDiff].0[/numDiff] | ||
122593859432, 122593859432347 | ||
-> 122593859432, 122593859432[numDiff]347[/numDiff] | ||
Args: | ||
- n1: The first number to compare. | ||
- n2: The second number to compare. | ||
- decimal_separator: Whether to separate the decimal part of the numbers with commas. | ||
Returns: | ||
- A tuple of two strings, each representing one of the input numbers with the differing part highlighted. | ||
""" | ||
if decimal_separator: | ||
s1, s2 = f"{n1:,}", f"{n2:,}" | ||
else: | ||
s1, s2 = str(n1), str(n2) | ||
|
||
min_len = min(len(s1), len(s2)) | ||
diff_idx = next( | ||
(i for i in range(min_len) if s1[i] != s2[i]), | ||
min_len, | ||
) | ||
|
||
return ( | ||
f"{s1[:diff_idx]}{_fmt_diff_part(s1, diff_idx)}", | ||
f"{s2[:diff_idx]}{_fmt_diff_part(s2, diff_idx)}", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
|
||
from datajudge.utils import format_difference | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"n1, n2", | ||
[ | ||
(123, 123.0), | ||
(122593859432347, 122593859432347 // 1000), # one group less | ||
(1.2, 1234567), | ||
(1.2, 1.3), | ||
], | ||
) | ||
@pytest.mark.xfail | ||
def test_print_diff_color(n1, n2): | ||
format_n1, format_n2 = format_difference(n1, n2) | ||
|
||
assert True, f"{format_n1} vs {format_n2}" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"n1, n2, sep_decimal, expected_n1, expected_n2", | ||
[ | ||
(123, 123.0, False, "123", "123[numDiff].0[/numDiff]"), | ||
( | ||
122593859432, | ||
122593859432347, | ||
False, | ||
"122593859432", | ||
"122593859432[numDiff]347[/numDiff]", | ||
), | ||
( | ||
122593859432, | ||
122593859432347, | ||
True, | ||
"122,593,859,432", | ||
"122,593,859,432[numDiff],347[/numDiff]", | ||
), | ||
(0, 0, False, "0", "0"), | ||
(1, 2, False, "[numDiff]1[/numDiff]", "[numDiff]2[/numDiff]"), | ||
( | ||
123456789, | ||
987654321, | ||
False, | ||
"[numDiff]123456789[/numDiff]", | ||
"[numDiff]987654321[/numDiff]", | ||
), | ||
], | ||
) | ||
def test_diff_color(n1, n2, sep_decimal, expected_n1, expected_n2): | ||
assert format_difference(n1, n2, decimal_separator=sep_decimal) == ( | ||
expected_n1, | ||
expected_n2, | ||
) |