Skip to content

Commit

Permalink
Parse times without colon (dbrgn#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
frinkelpi committed Oct 22, 2016
1 parent 3fd1350 commit 77e990e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion fahrplan/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def _parse_time(timestring, keywords):

# Parse regular time strings
# regular_time_match = re.search(r'([0-2]?[0-9])[:\-\. ]([0-9]{2})', timestring)
regular_time_match = re.search(r'(\d{2}):(\d{2})', timestring)
regular_time_match = re.search(r'(?<!/)(\d{2})(?::*)(\d{2})', timestring)
if regular_time_match:
return ':'.join(regular_time_match.groups())
timestring = timestring.lower()
Expand Down
20 changes: 12 additions & 8 deletions fahrplan/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def testHelp(self):

class TestInputParsing(unittest.TestCase):

valid_expected_result = {'arrival': '19:00', 'departure': '18:00', 'from': 'Zürich', 'to': 'Locarno', 'via': 'Genève'}
valid_expected_result = {'arrival': '19:00', 'departure': '18:30', 'from': 'Zürich', 'to': 'Locarno', 'via': 'Genève'}

def testEmptyArguments(self):
tokens = []
Expand All @@ -79,19 +79,19 @@ def testOneInvalidArgument(self):
self.assertIsNone(language)

def testValidArgumentsEn(self):
tokens = 'from Zürich to Locarno via Genève departure 18:00 arrival 19:00'.split()
tokens = 'from Zürich to Locarno via Genève departure 18:30 arrival 19:00'.split()
data, language = parser._process_tokens(tokens, sloppy_validation=True)
self.assertEqual(self.valid_expected_result, data)
self.assertEqual('en', language)

def testValidArgumentsDe(self):
tokens = 'von Zürich nach Locarno via Genève ab 18:00 an 19:00'.split()
tokens = 'von Zürich nach Locarno via Genève ab 18:30 an 19:00'.split()
data, language = parser._process_tokens(tokens, sloppy_validation=True)
self.assertEqual(self.valid_expected_result, data)
self.assertEqual('de', language)

def testValidArgumentsFr(self):
tokens = 'de Zürich à Locarno via Genève départ 18:00 arrivée 19:00'.split()
def testValidArgumentsFr(self):
tokens = 'de Zürich à Locarno via Genève départ 18:30 arrivée 19:00'.split()
data, language = parser._process_tokens(tokens, sloppy_validation=True)
self.assertEqual(self.valid_expected_result, data)
self.assertEqual('fr', language)
Expand All @@ -101,8 +101,8 @@ def testNotEnoughArgument(self):
self.assertRaises(ValueError, parser.parse_input, tokens)

def testBasicDepartureTime(self):
tokens = 'von basel nach bern ab 18:00'.split()
expected = {'from': 'basel', 'time': '18:00', 'to': 'bern'}
tokens = 'von basel nach bern ab 1945'.split()
expected = {'from': 'basel', 'time': '19:45', 'to': 'bern'}
self.assertEqual(expected, parser.parse_input(tokens)[0])

def testBasicArrivalTime(self):
Expand Down Expand Up @@ -261,4 +261,8 @@ def testIss13(self):


if __name__ == '__main__':
unittest.main()
tokens = 'de Zürich à Locarno via Genève départ 1830 arrivée 19:00'.split()
data, language = parser._process_tokens(tokens, sloppy_validation=True)
print(data)
exit()
#unittest.main()

0 comments on commit 77e990e

Please sign in to comment.