I don't think this will catch float('nan'), which is how I was bit this 
time.  

val = float('nan')
>>> isinstance(val, (float, int))
True
>>>
>>> type(val)
<type 'float'>


May I recommend as an alternative, slightly more robust solution:

def addSum(self, val, weight=1):
         """Add a scalar value to my running sum and count."""
         if val is not None:
+            if (not isinstance(val, (float, int))) or (math.isnan(val)):
+                raise ValueError("accum: Expected float or int, got %s" % 
val)
             self.sum     += val
             self.count   += 1
             self.wsum    += val * weight


On Monday, November 20, 2017 at 4:16:56 PM UTC-7, Tom Keffer wrote:
>
> Implemented at commit 40d7a2c 
> <https://github.com/weewx/weewx/commit/40d7a2c4cd921489664e3110c29f6256e26c7ae0>
> .
>
> -tk
>
> On Mon, Nov 20, 2017 at 4:45 PM, gjr80 <[email protected] <javascript:>> 
> wrote:
>
>> Well I am embarrased to say that to say I did not know about nan/NaN and 
>> python, today's lesson I guess.
>>
>> Gary
>>
>> On Tuesday, 21 November 2017 01:30:57 UTC+10, Thomas Carlin wrote:
>>>
>>> Tom,
>>> Your interpretation is exactly correct, and yes it is a coding error on 
>>> my side.  My thought was just a single sentence there about the Weewx 
>>> engine not doing any sanitation, and that being the responsibility of the 
>>> driver.  If not, no worries, just a thought.
>>>
>>> Gary,
>>> No worries on the delay.  You got me going in the right direction, and 
>>> we got it figured out! It's not like you get paid for doing this.  Until 
>>> yesterday, the extent of the sanitation that I was doing on the incoming 
>>> data was running float(data), and catching any ValueError exception.  Now, 
>>> because nan is a special 'string', when you run float('nan'), it returns 
>>> NaN, but now it is the Python 'number' NaN.  When you add a number to NaN, 
>>> your result is always NaN.  When this is stuffed into the database, 
>>> apparently it is interpreted as SQL NULL.  Next time the loop runs, Python 
>>> queries the database, and interprets SQL NULL as None, and then we get the 
>>> type mismatch.  
>>>
>>> I have added some more sanitation for this, and we are back up and 
>>> running, and it *should* conform to expected behavior.  
>>>
>>> Does that make sense?
>>>
>>> Thank you for the input on the driver.  I have a laundry list of things 
>>> that I would like to add to make it more robust, and I'll add the loop 
>>> output to that.  I may be converting to a queue as well, simply due to the 
>>> number of sensors that are being polled, and the "couple of seconds" note 
>>> in the Second Datasource section of the docs.  But that is a project for 
>>> another day!
>>>
>>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "weewx-user" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to