Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions branca/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json
import math
import os
import re
import struct
import zlib

Expand Down Expand Up @@ -387,19 +388,22 @@ def _camelify(out):


def _parse_size(value):
try:
if isinstance(value, int) or isinstance(value, float):
value_type = "px"
value = float(value)
assert value > 0
if isinstance(value, (int, float)):
return float(value), "px"
elif isinstance(value, str):
# match digits or a point, possibly followed by a space,
# followed by a unit: either 1 to 5 letters or a percent sign
match = re.fullmatch(r"([\d.]+)\s?(\w{1,5}|%)", value.strip())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a regex expert but testing it with online tools seems to work as expect. The firs t is supposed to be the number and the second one the %, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right! I added a comment to clarify

if match:
return float(match.group(1)), match.group(2)
else:
value_type = "%"
value = float(value.strip("%"))
assert 0 <= value <= 100
except Exception:
msg = "Cannot parse value {!r} as {!r}".format
raise ValueError(msg(value, value_type))
return value, value_type
raise ValueError(
f"Cannot parse {value!r}, it should be a number followed by a unit.",
)
else:
raise TypeError(
f"Cannot parse {value!r}, it should be a number or a string containing a number and a unit.",
)


def _locations_mirror(x):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,31 @@ def test_color_avoid_unexpected_error():
for n in [str(color_brewer_minimum_n), float(color_brewer_minimum_n), "abc"]:
with pytest.raises(TypeError):
ut.color_brewer(sname, n)


@pytest.mark.parametrize(
"value,result",
[
(1, (1.0, "px")),
("1 px", (1.0, "px")),
("80 % ", (80.0, "%")),
("100% ", (100.0, "%")),
("3 vw", (3.0, "vw")),
("3.14 rem", (3.14, "rem")),
],
)
def test_parse_size(value, result):
assert ut._parse_size(value) == result


@pytest.mark.parametrize(
"value",
[
"what?",
"1.21 jigawatts",
ut._parse_size,
],
)
def test_parse_size_exceptions(value):
with pytest.raises((ValueError, TypeError)):
ut._parse_size(value)