Sep-30-2019, 07:35 AM
Hi, I got this converter:
I will also parse the first parameter's correct form later.
Thanks,
def nmea_to_dd(coordinate, direction): if direction not in ['N', 'S', 'W', 'E']: raise TypeError('only N, S, E or W are valid directions') if coordinate.find('.') is 5: """ longitude in the DDDMM.MMMMM format """ dd = int(float(coordinate[:3].strip('0'))) ss = float(coordinate) - dd * 100 if direction == 'E': return round(dd + (ss / 60), 6) elif direction == 'W': return round(dd + (ss / 60), 6) * -1 else: return 0.0 if coordinate.find('.') is 4: """ latitude in the DDMM.MMMMM format """ dd = int(float(coordinate) / 100) ss = float(coordinate) - dd * 100 if direction == 'N': return round(dd + (ss / 60), 6) elif direction == 'S': return round(dd + (ss / 60), 6) * -1 else: return 0.0Here is the unittest file:
import unittest from converter import * class TestNmeaConverter(unittest.TestCase): def test_valid(self): self.assertAlmostEqual(nmea_to_dd('5132.0000', 'N'), 51.533333) self.assertAlmostEqual(nmea_to_dd('5132.0000', 'S'), -51.533333) self.assertAlmostEqual(nmea_to_dd('01323.629', 'E'), 13.393817) self.assertAlmostEqual(nmea_to_dd('01323.629', 'W'), -13.393817) def test_directions(self): self.assertRaises(TypeError, nmea_to_dd('5132.0000', 'A'))I dont know how to use assertRaises with 2 parameters in the 13th line. I would like to make sure that the function raises TypeError if it's given another letter as the second parameter.
I will also parse the first parameter's correct form later.
Thanks,