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
27 changes: 27 additions & 0 deletions tests/parse/test_parse_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import List

import pytest
from numpy.testing import assert_equal

from vrplib.parse.parse_utils import text2lines


@pytest.mark.parametrize(("text", "expected"), [("", []), ("\n", [])])
def test_empty_lines(text: str, expected: List[str]):
assert_equal(text2lines(text), expected)


@pytest.mark.parametrize(
("text", "expected"),
[
("# test comment", []),
("# other comment", []),
("123\n#comment", ["123"]),
("#\n#", []),
# Lines are stripped before they're inspected for comments, so this
# should also be OK:
(" # comment after whitespace", []),
],
)
def test_comments(text: str, expected: List[str]):
assert_equal(text2lines(text), expected)
9 changes: 7 additions & 2 deletions vrplib/parse/parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

def text2lines(text: str) -> List[str]:
"""
Takes a string and returns a list of non-empty, stripped lines.
Takes a string and returns a list of non-empty, stripped lines. Also
removes any comment lines from the given string.
"""
return [line.strip() for line in text.splitlines() if line.strip()]
return [
stripped
for line in text.splitlines()
if (stripped := line.strip()) and not stripped.startswith("#")
]


def infer_type(s: str) -> Union[int, float, str]:
Expand Down