Skip to content

Commit

Permalink
Merge pull request #1 from PatrickOHara/edge-list-fix
Browse files Browse the repository at this point in the history
Edge list fix
  • Loading branch information
ben-hudson authored Sep 23, 2023
2 parents 57e7347 + 8a2d4e7 commit f9d7b76
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tests/fields/test_edge_data_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ 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):
if exc:
with pytest.raises(exc):
field.parse(text)
else:
field.parse(text) == value
assert field.parse(text) == value
2 changes: 1 addition & 1 deletion tests/fields/test_edge_list_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions tsplib95/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=('-1', ' -1\n'),
value=T.ListT(value=T.FuncT(func=int), terminal='-1'),
sep='\n',
terminal='-1')


Expand All @@ -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')


Expand All @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions tsplib95/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f9d7b76

Please sign in to comment.