Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new line to end of file when rendering #2

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion tests/fields/test_edge_data_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ def test_parse(field, text, value, exc):
with pytest.raises(exc):
field.parse(text)
else:
assert field.parse(text) == value
field.parse(text) == value
5 changes: 3 additions & 2 deletions tests/models/test_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class TestProblem(M.Problem):
@pytest.mark.parametrize('text,value,exc', [
('FOO: 42\nBAR: answer', {'foo': 42, 'bar': 'answer'}, None),
('FOO: 4.2\nBAR: answer', None, E.ParsingError),
('FOO: 42\nBAR: answer\nEOF\n', {'foo': 42, 'bar': 'answer'}, None)
])
def test_model_parse(TestProblem, text, value, exc):
if exc:
Expand All @@ -29,8 +30,8 @@ def test_model_parse(TestProblem, text, value, exc):


@pytest.mark.parametrize('value,text,exc', [
({'foo': 42, 'bar': 'answer'}, 'FOO: 42\nBAR: answer\nEOF', None),
({'foo': 42, 'bar': 'answer', 'baz': 'wat'}, 'FOO: 42\nBAR: answer\nEOF', None), # noqa: E501
({'foo': 42, 'bar': 'answer'}, 'FOO: 42\nBAR: answer\nEOF\n', None),
({'foo': 42, 'bar': 'answer', 'baz': 'wat'}, 'FOO: 42\nBAR: answer\nEOF\n', None), # noqa: E501
])
def test_model_render(TestProblem, value, text, exc):
problem = TestProblem(**value)
Expand Down
6 changes: 3 additions & 3 deletions tsplib95/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="\n",
sep=('-1', ' -1\n'),
Copy link
Owner Author

@ben-hudson ben-hudson Jan 31, 2024

Choose a reason for hiding this comment

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

I'm not sure what the implication is, but the conflict is on this line. I have

value=T.ListT(value=T.FuncT(func=int), terminal='-1'),
sep='\n',

and you have

value=T.ListT(value=T.FuncT(func=int)),
sep=('-1', ' -1\n'),

Subtle difference but I don't know what the implication is... I think I made the change so add a new line at the end of file when rendering

terminal='-1')


Expand All @@ -249,7 +249,7 @@ class EdgeListField(TransformerField):

@classmethod
def build_transformer(cls):
edge = T.TupleT(value=T.FuncT(func=int), size=2)
edge = T.ListT(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(edge_list, adj_list)
return T.UnionT(adj_list, edge_list)


class DepotsField(TransformerField):
Expand Down
4 changes: 2 additions & 2 deletions tsplib95/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def parse(cls, text, **options):
# parse into a dictionary
data = {}
for keyword, value in zip(field_keywords, field_values):
if keyword != 'EOF':
if keyword and keyword != 'EOF':
field = cls.fields_by_keyword[keyword]
name = cls.names_by_keyword[keyword]
data[name] = field.parse(value.strip())
Expand Down Expand Up @@ -228,7 +228,7 @@ def render(self):
for keyword, value in rendered.items():
sep = ':\n' if '\n' in value else ': '
kvpairs.append(f'{keyword}{sep}{value}')
kvpairs.append('EOF')
kvpairs.append('EOF\n')

# join and return the result
return '\n'.join(kvpairs)
Expand Down
8 changes: 0 additions & 8 deletions tsplib95/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ 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)

Choose a reason for hiding this comment

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

I can't remember the reason why I included this class, but it has something to do with this PR: rhgrant10#19


class MapT(ContainerT):
"""Transformer for a key-value mapping of items.
Expand Down