Bullet proof passing numeric values from NMEA data stream.
steve at REMOVE.THIS.cybersource.com.au
Tue Mar 20 14:29:00 CET 2007
On Tue, 20 Mar 2007 12:09:29 +0000, Doug Gray wrote:
> 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, ...
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, ...)?
> For example, each of the following throw the exception so do not return
> the correct value:
All your examples include spurious whitespace. If that is the only
problem, here's a simple fix:
"""Remove whitespace from string s."""
"""Fix a GPS value string and return as a float."""
If only a few values are malformed, you might find this is faster:
Only measurement with actual realistic data will tell you which is faster.
If you expect to get random non-numeric characters, then here's another
# initialize some global data
table = string.maketrans("", "") # all 8 bit characters
keep = "1234567890.-+"
dontkeep = ''.join([c for c in table if c not in keep])
try: # a fast conversion first
except ValueError: # fall-back conversion
return float(string.translate(value, table, don'tkeep))
Once you've built the character tables, the translate function itself is
executed in C and is very fast.
> Also, why should I consider the string module? Is it faster/better?
Most of the time you should use string methods, e.g.:
The only time you should use the string module is when you need one of the
functions (or data objects) that don't exist as string methods (e.g.
More information about the Python-list