Skip to content

Commit

Permalink
Minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
rhgrant10 committed Mar 29, 2020
1 parent 743e57d commit 86b5541
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
8 changes: 7 additions & 1 deletion tests/fields/test_adjacency_list_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ def adj_list_field():

@pytest.fixture
def text():
return '0 1 2 3 -1\n1 0 2 3 -1\n2 0 1 3 -1\n3 0 1 2 -1\n-1'
return '\n'.join([
'0 1 2 3 -1',
'1 0 2 3 -1',
'2 0 1 3 -1',
'3 0 1 2 -1',
'-1',
])


@pytest.fixture
Expand Down
2 changes: 0 additions & 2 deletions tests/fields/test_matrix_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def test_field_parse(f):
0 0 0
1 3 4
2 -1 5
-1
''')
assert f.parse(text) == [
[0, 0.0, 0.0],
Expand All @@ -33,7 +32,6 @@ def test_field_render(f):
0 0.0 0.0
1 3.0 4.0
2 -1.0 5.0
-1
''').strip()


Expand Down
File renamed without changes.
23 changes: 17 additions & 6 deletions tests/transformers/test_container_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ def make(**kwargs):


@pytest.mark.parametrize('kwargs,correct', [
({'sep': ':'}, ['a b', 'c-d', '--e']),
({'sep': '-'}, ['a b:c', 'd:', 'e']),
({'sep': '-', 'filter_empty': False}, ['a b:c', 'd:', '', 'e']),
({'sep': ':', 'terminal': '--e'}, ['a b', 'c-d']),
({'sep': ':', 'size': 3}, ['a b', 'c-d', '--e']),
({'sep': ':'}, ['a b', 'c-d', '--', 'e']),
({'sep': '-'}, ['a b:c', 'd:', ':e']),
({'sep': '-', 'filter_empty': False}, ['a b:c', 'd:', '', ':e']),
({'sep': ':', 'terminal': 'e'}, ['a b', 'c-d', '--']),
({'sep': ':', 'size': 3, 'terminal': 'e'}, ['a b', 'c-d', '--']),
({'sep': ':', 'size': 4}, ['a b', 'c-d', '--', 'e']),
])
def test_container_tf_parse(container_tf, kwargs, correct):
tf = container_tf(**kwargs)
result = tf.parse('a b:c-d:--e')
result = tf.parse('a b:c-d:--:e')
assert result == correct


Expand All @@ -46,6 +46,17 @@ def test_container_tf_parse_no_terminal(container_tf):
tf.parse('a b')


def test_container_tf_parse_middle_terminal(container_tf):
tf = container_tf(terminal='-1')
with pytest.raises(exceptions.ParsingError):
tf.parse('a -1 b -1')


def test_container_tf_parse_near_middle_terminal(container_tf):
tf = container_tf(terminal='-1')
assert tf.parse('a -1b -1') == ['a', '-1b']


@pytest.mark.parametrize('kwargs,text', [
({}, 'a b c'),
({'terminal': '-1'}, 'a b c -1'),
Expand Down
6 changes: 1 addition & 5 deletions tsplib95/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,6 @@ def _create_wfunc(self, special=None):
if self.is_explicit():
matrix = self._create_explicit_matrix()
return lambda i, j: matrix[i, j]
else:
return self._create_distance_function(special=special)

def _create_distance_function(self, special=None):
# wrap a distance function so that it takes node indexes, not coords
if self.is_special():
if special is None:
raise Exception('missing needed special weight function')
Expand All @@ -360,6 +355,7 @@ def _create_distance_function(self, special=None):
else:
return lambda i, j: 1 # unweighted graphs

# wrap the distance function so that it takes node indexes, not coords
def adapter(i, j):
return wfunc(self.node_coords[i], self.node_coords[j])

Expand Down
9 changes: 4 additions & 5 deletions tsplib95/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ def parse(self, text):
if self.filter_empty:
texts = list(filter(None, texts))

# if specified, ensure the terminal terminates the list
# if specified, make sure there's no terminal somwhere in the middle
if self.terminal is not None:
try:
index = texts.index(self.terminal)
except ValueError:
error = f'items did not end with terminal: {repr(self.terminal)}'
raise exceptions.ParsingError(error)

if index < len(texts) - 1:
pass # happy path
else:
# uh-oh, found one, so raise an exception with the extra items
extra = texts[index + 1:]
error = (f'found {len(extra)} extra items after terminal '
f'{repr(self.terminal)}, first is {repr(extra[0])}')
Expand Down

0 comments on commit 86b5541

Please sign in to comment.