From c83151544100664cb66d5067b336256c7bf53115 Mon Sep 17 00:00:00 2001 From: Patrick O'Hara Date: Mon, 11 Oct 2021 17:42:17 +0100 Subject: [PATCH 1/2] Fix for edge list, adj list --- tests/fields/test_edge_data_field.py | 2 +- tsplib95/fields.py | 6 +++--- tsplib95/transformers.py | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/fields/test_edge_data_field.py b/tests/fields/test_edge_data_field.py index 6863203..7fe6174 100644 --- a/tests/fields/test_edge_data_field.py +++ b/tests/fields/test_edge_data_field.py @@ -20,4 +20,4 @@ def test_parse(field, text, value, exc): with pytest.raises(exc): field.parse(text) else: - field.parse(text) == value + assert field.parse(text) == value diff --git a/tsplib95/fields.py b/tsplib95/fields.py index 7de9b88..db9b41b 100644 --- a/tsplib95/fields.py +++ b/tsplib95/fields.py @@ -238,7 +238,7 @@ class AdjacencyListField(TransformerField): def build_transformer(cls): return T.MapT(key=T.FuncT(func=int), value=T.ListT(value=T.FuncT(func=int)), - sep=('-1', ' -1\n'), + sep="\n", terminal='-1') @@ -249,7 +249,7 @@ class EdgeListField(TransformerField): @classmethod def build_transformer(cls): - edge = T.ListT(value=T.FuncT(func=int), size=2) + edge = T.TupleT(value=T.FuncT(func=int), size=2) return T.ListT(value=edge, terminal='-1', sep='\n') @@ -273,7 +273,7 @@ class EdgeDataField(TransformerField): def build_transformer(cls): adj_list = AdjacencyListField.build_transformer() edge_list = EdgeListField.build_transformer() - return T.UnionT(adj_list, edge_list) + return T.UnionT(edge_list, adj_list) class DepotsField(TransformerField): diff --git a/tsplib95/transformers.py b/tsplib95/transformers.py index 7536349..a2dc33d 100644 --- a/tsplib95/transformers.py +++ b/tsplib95/transformers.py @@ -244,6 +244,14 @@ def pack(self, items): def unpack(self, container): return list(container) +class TupleT(ContainerT): + """Transformer for tuples""" + + def pack(self, items): + return tuple(items) + + def unpack(self, container): + return tuple(container) class MapT(ContainerT): """Transformer for a key-value mapping of items. From 8a2d4e7145cecbe121ce6755ce4486bb530cbacb Mon Sep 17 00:00:00 2001 From: Patrick O'Hara Date: Mon, 11 Oct 2021 18:10:59 +0100 Subject: [PATCH 2/2] Fix tests --- tests/fields/test_edge_data_field.py | 2 +- tests/fields/test_edge_list_field.py | 2 +- tsplib95/fields.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/fields/test_edge_data_field.py b/tests/fields/test_edge_data_field.py index 7fe6174..090fe20 100644 --- a/tests/fields/test_edge_data_field.py +++ b/tests/fields/test_edge_data_field.py @@ -12,7 +12,7 @@ def field(): @pytest.mark.parametrize('text,value,exc', [ ('1 2\n2 3\n-1', [(1, 2), (2, 3)], None), ('1 2\n2 x\n-1', [(1, 2), (2, 3)], E.ParsingError), - ('1 2 3\n2 3\n-1', {1: [2, 3], 2: [3]}, None), + ('1 2 3 -1\n2 3 -1\n-1', {1: [2, 3], 2: [3]}, None), ('1 x 3\n2 3\n-1', {1: [2, 3], 2: [3]}, E.ParsingError), ]) def test_parse(field, text, value, exc): diff --git a/tests/fields/test_edge_list_field.py b/tests/fields/test_edge_list_field.py index 215ce8a..cbd823a 100644 --- a/tests/fields/test_edge_list_field.py +++ b/tests/fields/test_edge_list_field.py @@ -9,7 +9,7 @@ def edge_list_field(): def test_parse(edge_list_field): - assert edge_list_field.parse('1 2\n3 4\n-1') == [[1, 2], [3, 4]] + assert edge_list_field.parse('1 2\n3 4\n-1') == [(1, 2), (3, 4)] def test_render(edge_list_field): diff --git a/tsplib95/fields.py b/tsplib95/fields.py index db9b41b..6f3e084 100644 --- a/tsplib95/fields.py +++ b/tsplib95/fields.py @@ -237,8 +237,8 @@ class AdjacencyListField(TransformerField): @classmethod def build_transformer(cls): return T.MapT(key=T.FuncT(func=int), - value=T.ListT(value=T.FuncT(func=int)), - sep="\n", + value=T.ListT(value=T.FuncT(func=int), terminal='-1'), + sep='\n', terminal='-1')