Python function recalling function?

-TNT-
-TNT- used Ask the Experts™
on
I have a simple temp sensor pushing data to Xively via my Raspberry Pi.. Ocassionally I will get readings like -1500 or 290. What I am trying to do is limit these readings and for some reason this code won't work... any ideas?

def read_temperature():
   tempfile = open("/sys/bus/w1/devices/28-000005aac776/w1_slave")
   thetext = tempfile.read()
   tempfile.close()
   tempdata = thetext.split("\n")[1].split(" ")[9]
   temperature = float(tempdata[2:])
   temperature = temperature / 1000
   temperature = 9.0/5.0 * temperature + 32
   temperature = "{0:.2f}".format(temperature)
   if temperature<0 and temperature>140:
     read_temperature()
   print('%s' % ( temperature )+'Degrees Fahrenheit')
   return temperature

Open in new window


So what I am trying to do on lines 10 and 11 is rerun the function if the value is out of bounds... doesn't work. The functions runs but the extreme values still get through.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if temperature<0 and temperature>140:

should be if temperature<0 or temperature>140:
+1 to brendanmeyer's answer. The reason is that the condition never holds and the function behaves as if the two lines were not there at all.

Anyway, if the reason for the errorneous readings holds longer than a moment, you should probably wait a bit before getting the temperature again. I also suggest to log the errorneous values, better to say the content of the read file, or of its second line to see what causes the problem. Knowing that,  the problem probably could be solved differently.

Also, if the file is longer than short, you can consider reading by lines and closing the file earlier. There is no reason to read more lines if you throw them away.

Author

Commented:
def read_temperature():
   tempfile = open("/sys/bus/w1/devices/28-000005aac776/w1_slave")
   thetext = tempfile.read()
   tempfile.close()
   tempdata = thetext.split("\n")[1].split(" ")[9]
   temperature = float(tempdata[2:])
   temperature = temperature / 1000
   temperature = 9.0/5.0 * temperature + 32
   temperature = "{0:.2f}".format(temperature)
   if temperature<0 or temperature>140:
     print ('void %s' % (temperature ))
     read_temperature()
   else:
     print('%s' % ( temperature )+'Degrees Fahrenheit')
     return temperature
:

Open in new window

ahhh good catch. But it still doesn't work:

void 76.44
void 76.44
void 76.44
void 76.44
void 76.44
void 76.44
void 76.44
void 76.44
void 76.55
void 76.55
void 76.55
That is because you convert the number to a string at the line 9. Remove it and do the conversion to the string only when it is written to output.

You apparently use Python 2 with `from __future__ import print_function`. In Python 2, apples can be compared with bananas in a boolean context. However the result is not always as expected. Have a look at the following lines from the interactive Python mode:
c:\>py
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> temperature = 76.44
>>> temperature
76.44
>>> temperature < 0
False
>>> temperature > 140
False
>>> temperature < 0 or temperature > 140
False
>>> s = "{0:.2f}".format(temperature)
>>> s
'76.44'
>>> s < 0
False
>>> s > 140
True
>>> s < 0 or s > 140
True
>>>

Open in new window

The `s > 140` is the problematic part. In Python 3, that cannot be done. It leads to exception:
c:\>py -3
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> temperature = 76.44
>>> temperature
76.44
>>> temperature < 0
False
>>> temperature > 140
False
>>> temperature < 0 or temperature > 140
False
>>> s = "{0:.2f}".format(temperature)
>>> s
'76.44'
>>> s < 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()
>>>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial