Hi mick,

i am really sorry, but obviously your post scrolled out of my inbox
view ... and i just forgot it ;)

well, KeyError is the desired behaviour when you do a "get" on a
"dictionary" like object ... so your code should be prepared for it ;)

as i see aou alread catch the IOError - however i am very unsure why you
do this ... ?!?! pyowfs shouldnt raise an IOError at all ... 

but instead you should watch ot for KeyError whenever accessing an
attribute from a Sensor ... 

e.g.: 

    @property
    def present (self) :
        try:
            return self.sensor.get ("present")
        except IOError, KeyError:
            return -777

or 

    @property
    def present (self) :
        try:
            return self.sensor.get ("present")
        except IOError:
            return -777
        except KeyError : 
            print "key not found"
            return -888

or simply in your main loop :

s1 = Sens1 (connection)
s2 = Sens2 (connection)
while True : # repeat forever
    for s in [s1,s2] : 
        try : 
            print "reading %r" % s
            print 'temp    = ',  s.temp
            print 'id      = ',  s.id
            print 'present = ',  s.present
            print 'power   = ',  s.power
        except KeyError :
            print "something wrong with sensor %r" % s

hope this helps you out ;)

regards,
marcus.

Am Mittwoch, den 19.05.2010, 23:27 +0100 schrieb Mick Sulley:
> Hi Marcus,
> 
> Thanks for your fast response.  I just tested version 0.1.3, I now get 
> 
> Start of loop  21530
> S1 reading 
> temp =  26.5
> id =  522FA9010800
> present =  1
> power =  0
> S2 reading 
> temp =  something wrong with temp
> None
> id =  2054A9010800
> present = 
> Traceback (most recent call last):
>   File "./pyowfs_test1.py", line 72, in <module>
>     print 'present = ',  s2.present
>   File "./pyowfs_test1.py", line 36, in present
>     return self.sensor.get ("present")
>   File "/usr/local/lib/python2.6/dist-packages/pyowfs/owfs.py", line 95,
> in get
>     raise KeyError (key)
> KeyError: 'present'
> m...@mick-desktop:~/Documents/Controls$
> 
> 
> What I am trying to do is to read the data from all the sensor and if
> one goes missing flag it up somehow, maybe setting the 'present'
> attribute to zero, but it needs to continue reading the others and if
> the missing one come back it needs to start reading that one as well.
> 
> Here is the code that I am using to test, maybe there is a better way to
> achieve this?
> 
> 
> #! /usr/bin/env python
> # Filename pyowfs_test1.py
> # based up the code from Marcus Priesch
> 
> 
> from pyowfs import Connection
> import sys
> 
> class Sensor (object) :
>     id = None
>     def __init__ (self, connection):
>         self.conn   = connection
>         self.sensor = connection.find (id = self.id)[0]
>         self.cache = 0
> 
> """class TempSensor (Sensor) :
>     def temp (self) : 
>         return float (self.sensor.get ("temperature"))
>         """
> class TempSensor (Sensor) :
>     @property
>     def temp (self) : 
>         try:
>             return float (self.sensor.get ("temperature"))
>         except TypeError:
>             print 'something wrong with temp'
>     @property
>     def power (self) :
>         try:
>             return self.sensor.get ("power")
>         except IOError:
>             return -888
>     @property
>     def present (self) :
>         try:
>             return self.sensor.get ("present")
>         except IOError:
>             return -777
>     @property
>     def id (self) :
>         try:
>             return self.sensor.get ("id")
>         except IOError:
>             return "Cant find the id"
> 
> class Sens1 (TempSensor) :
>     id = "522FA9010800"
> 
> class Sens2 (TempSensor) :
>     id = "2054A9010800"
> 
> """ Start of the main program """
> connection = Connection("/dev/ttyD1")
> for s in connection.find () :
>     print s
> 
> loop = 0
> """ Need to read the sensor list and match the sensors found to the
> names on the list, mark each one as present or not """
>     
> s1 = Sens1 (connection)
> s2 = Sens2 (connection)
> while loop > -1:
>     print 'Start of loop ',  loop
>     print 'S1 reading '
>     print 'temp = ',  s1.temp
>     print 'id = ',  s1.id
>     print 'present = ',  s1.present
>     print 'power = ',  s1.power
>     print 'S2 reading '
>     print 'temp = ',  s2.temp
>     print 'id = ',  s2.id
>     print 'present = ',  s2.present
>     print 'power = ',  s2.power
>     loop = loop + 1
> 
> 
> ------------------------------------------------------------------------------
> 
> _______________________________________________
> Owfs-developers mailing list
> Owfs-developers@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/owfs-developers
> 



------------------------------------------------------------------------------

_______________________________________________
Owfs-developers mailing list
Owfs-developers@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/owfs-developers

Reply via email to