built-in 'property'

Bob.Cowdery at CGI-Europe.com Bob.Cowdery at CGI-Europe.com
Tue Dec 28 16:53:35 EST 2004


Thanks to everyone that has helped on this. What I am trying to do is create
a capability based api that I can build to order. This is as far as I get at
the moment.

Each of the first three classes represents some function I can do to a
radio, there would be many of these in practice. The next class is the API.
I figure I have to include all the possible radio function classes in this
as I can't figure out how to make the classes I include per instance. When I
create the API object I pass in a capability array as in the last class. I
use these classes as follows.

>>> import capability as c
>>> import radioapi as r
>>> caps = c.Capability()
>>> cap = caps.getCaps('SDR1000')
>>> cap
['mode', 'freq', 'blanker']
>>> api = r.API(cap)
>>> r.caps
['mode', 'freq', 'blanker']
>>> api.mode
Retrieving mode var "mode"
'lsb'
>>> api.freq
Retrieving freq var "freq"
7.0800000000000001
>>> api.blanker
Retrieving blanker var "blanker"
1
>>> hasattr(api, 'monitor')
0

This is the behaviour I want, each class responds if it is in the capability
array. The user can test attributes with hasattr(klass, 'att') to see if it
is supported. I can create another instance with a different radio and it
will respond accordingly. The problem of course is that in class API, caps
is global so as soon as I create another instance the first starts
responding to the new capability. If I try to make caps per instance I get
into all sorts of trouble with recursion. Does anyone have an idea how to
fix this or is it just impossible to make this work.

Regards
Bob
  
class Mode(object):

    def __init__(self, initval=None, name='var'):
        self.val = initval
        self.name = name

    def __get__(self, obj, objtype):
        print 'Retrieving mode', self.name
        return self.val

    def __set__(self, obj, val):
        print 'Updating mode' , self.name
        self.val = val
import capability

class Freq(object):

    def __init__(self, initval=None, name='var'):
        self.val = initval
        self.name = name

    def __get__(self, obj, objtype):
        print 'Retrieving freq', self.name
        return self.val

    def __set__(self, obj, val):
        print 'Updating freq' , self.name
        self.val = val
        
class Blanker(object):

    def __init__(self, initval=None, name='var'):
        self.val = initval
        self.name = name

    def __get__(self, obj, objtype):
        print 'Retrieving blanker', self.name
        return self.val

    def __set__(self, obj, val):
        print 'Updating blanker' , self.name
        self.val = val      

class API(object):
	
    # create all possible radio classes
    mode=Mode('lsb', 'var "mode"')
    freq=Freq(7.080, 'var "freq"')
    blanker=Blanker(True, 'var "blanker"')
    global caps
    caps = []
    
    def __init__(self, pcaps):
        global caps
        caps = pcaps 

    def __getattribute__(self, key):
        global caps
        if (key in caps) or (key == 'caps'):
            obj = object.__getattribute__(self, key)
            if hasattr(obj, "__get__"):
                return obj.__get__()
            return obj
        else:
            raise AttributeError, "unreadable attribute"

class Capability(object):
    
    m_cap = {
            'SDR1000': ['mode','freq','blanker'],
            'MyRig': ['mode','freq'],
            'YourRig': ['mode', 'blanker']
            }
    
    def getCaps(self, radio):
        return self.m_cap[radio]

*** Confidentiality Notice *** Proprietary/Confidential
Information belonging to CGI Group Inc. and its affiliates
may be contained in this message. If you are not a recipient
indicated or intended in this message (or responsible for
delivery of this message to such person), or you think for
any reason that this message may have been addressed to you
in error, you may not use or copy or deliver this message
to anyone else.  In such case, you should destroy this
message and are asked to notify the sender by reply email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041228/6eb17354/attachment.html>


More information about the Python-list mailing list