From a251643e2205bb2130fa5a37a557115cd16a5cbf Mon Sep 17 00:00:00 2001 From: Addison Hanrattie <40609224+supersimple33@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:10:46 -0500 Subject: [PATCH 1/3] fix: EOF matching problems --- tsplib95/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tsplib95/models.py b/tsplib95/models.py index 1eb7223..04cb236 100644 --- a/tsplib95/models.py +++ b/tsplib95/models.py @@ -103,6 +103,7 @@ def parse(cls, text, **options): # split the whole text by known keys regex = re.compile(pattern, re.M) + text += '\n' # ensure EOF is matched __, *results = regex.split(text) # pair keys and values From fd538d44ba66a81f9203234b28b8898c3fd5ae4b Mon Sep 17 00:00:00 2001 From: Addison Hanrattie <40609224+supersimple33@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:19:38 -0500 Subject: [PATCH 2/3] nit: No need for extra -1 In the tour files from ALL_tsp they do not add the extra -1 --- tsplib95/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsplib95/fields.py b/tsplib95/fields.py index 7de9b88..8b1f7eb 100644 --- a/tsplib95/fields.py +++ b/tsplib95/fields.py @@ -369,7 +369,7 @@ def render(self, tours): tour_string = ' '.join(str(i) for i in tour) tour_strings.append(f'{tour_string} -1') - if tour_strings: + if len(tour_strings) > 1: tour_strings += ['-1'] return '\n'.join(tour_strings) From da4af4daf4bda418eb8f39189e4855004ef14bf7 Mon Sep 17 00:00:00 2001 From: Addison Hanrattie <40609224+supersimple33@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:15:48 -0500 Subject: [PATCH 3/3] fix: Using regex instead of new line --- tsplib95/models.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tsplib95/models.py b/tsplib95/models.py index 04cb236..a560671 100644 --- a/tsplib95/models.py +++ b/tsplib95/models.py @@ -99,11 +99,10 @@ def parse(cls, text, **options): # prepare the regex for all known keys keywords = '|'.join(cls.fields_by_keyword) sep = r'''\s*:\s*|\s*\n''' - pattern = f'({keywords}|EOF)(?:{sep})' + pattern = f'({keywords}|EOF)(?:{sep})|(?:EOF$)' # split the whole text by known keys regex = re.compile(pattern, re.M) - text += '\n' # ensure EOF is matched __, *results = regex.split(text) # pair keys and values @@ -113,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 != 'EOF' and keyword is not None: field = cls.fields_by_keyword[keyword] name = cls.names_by_keyword[keyword] data[name] = field.parse(value.strip())