Bullet proof passing numeric values from NMEA data stream.
Folks, I am looking for a fast but most importantly a bullet proof method to pass and NMEA data stream (GPS output) ascii numeric strings. The best I can offer is: def fint(a): try: return int(float(a)) except: return 0 The reason for this is the quality of the data from the huge variety of GPS units available varies considerably. Some units do not follow the standard and I want to pass the data as best I can without hanging the code for an oddball data value. Can anyone suggest better? For example, each of the following throw the exception so do not return the correct value: int('00.') int(' 00.') float('- 00') float(' - 00') float(' - 00') float(' - 00.') float('- 00.') float('- 10.') float('- 10.') float('- 10.') int('- 10.') int('- 10.') float('- 10.') int('1.0') Also, why should I consider the string module? Is it faster/better? TIA, Doug -- http://mail.python.org/mailman/listinfo/python-list
Re: Eureka moments in Python
On Tue, 13 Mar 2007 18:16:15 +1100, Steven D'Aprano wrote: > I'd be interested in hearing people's stories of Eureka moments in Python, > moments where you suddenly realise that some task which seemed like it > would be hard work was easy with Python. Mine was the discovery of the pybluez module. I had been sweating the task of interfacing a bluetooth GPS unit to my python applicaton. I googled for 'python bluetooth GPS' and found 20 lines of python code that had the interface up and running before breakfast. This included the package install with 'yum install pybluez' in Fedora Core 5. See: http://www.robertprice.co.uk/robblog/archive/2007/1/Using_A_Bluetooth_GPS_From_Python.shtml Doug Gray -- http://mail.python.org/mailman/listinfo/python-list
Re: Bullet proof passing numeric values from NMEA data stream.
On Wed, 21 Mar 2007 00:29:00 +1100, Steven D'Aprano wrote: > On Tue, 20 Mar 2007 12:09:29 +0000, Doug Gray wrote: > >> Folks, >> I am looking for a fast but most importantly a bullet proof method to pass >> and NMEA data stream (GPS output) ascii numeric strings. The best I can >> offer is: >> >> def fint(a): >> try: return int(float(a)) >> except: return 0 > > > Will your application calculate the wrong results if it starts getting a > whole lot of spurious zeroes? Wouldn't it better to signal "this value is > invalid" rather than a false zero? > > Do you actually want ints? It seems to me that if your data stream is > delivering floats, you're throwing away a lot of data. For example, if the > data stream is: > > 2.4, 5.7, 3.9, 5.1, ... > > you're getting: > > 2, 5, 3, 5, ... > > which is possibly not even the right way to convert to ints. Shouldn't you > be rounding to nearest (i.e. 2, 6, 4, 5, ...)? > > [snip] > Thanks, a very helpful response. I'll need some time to fully digest. Yes I will need a float variant, the int version was by way of example. I can deal with the rounding etc as necessary, but I was after an pythonistic view of the generic problem. Re the examples: whitespace and mal positioned signs and decimal point would be the obvious errors I might expect but of course this is speculation. The few GPS units I have tried have all tripped up my first cut less tolerant code. I am going to revise the interface to work around potential problems and my preliminary test efforts highlighted more exceptions than I expected. Doug -- http://mail.python.org/mailman/listinfo/python-list