Bullet proof passing numeric values from NMEA data stream.

Doug Gray doug at localhost.localdomain
Tue Mar 20 14:53:24 CET 2007

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.


More information about the Python-list mailing list