Python 2.2 / Properties
Chris Liechti
cliechti at gmx.net
Thu Jan 3 09:15:27 EST 2002
[posted and mailed]
nikolai.kirsebom at siemens.no (Nikolai Kirsebom) wrote in
news:3c345e9d.108916173 at news.mch.sni.de:
> I have not downloaded the new version yet - but will do so in the new
> future.
>
> One question relating to the Properties.
>
> I'm currently developping a small "inspector" where I from a Python
> program will be able to read/write attributes of C++ objects in
> another process (where Python is not possible to integrate). I'll
> also be able to execute some methods. Anyhow, system is based on
> useing socket for communication and the protocol is very simple. Text
> string consisting of object name and attribute name separated by '.'.
> As an example:
>
> cmd="Object.Attr1"
> will send the command to read Attr1 of the object named "Object".
>
> cmd="Object.Attr2=345"
> will send the command to set Attr2 to the value 345 of the same
> object.
>
> (I'm using bison/flex to parse the commands).
>
> Now to the question: I would like to use the same property-get and
> property-set methods (functions) for the remote attributes (as seen
> from Python). The example code below illustrates this.
>
> --------------------------
> class RemoteObj(object):
> name = "abc"
> def remPropGet(self, x):
> # Send command to remote object to set attribute value
> resp = Cmd.Send("%s.%s" % (name, NAME_OF_THIS_PROPERTY(x)))
> if resp['Status'] == OK:
> return resp['Result']
> return resp['Default']
>
> def remPropSet(self, newVal):
> resp = Cmd.Send("%s.%s=%s" % (name, NAME_OF_THIS_PROPERTY(x),
> str(newVal)))
>
> RecordId = property(remPropGet, None, None, "RecordId")
> ItemNbr = property(remPropGet, remPropSet, None, "ItemNbr")
> --------------------------
>
> But is it possible in the methods to find out the name of the property
> being 'handled' - or maybe I somehow could get to the doc-string for
> the property.
>
> Thanks for any hints/help.
why not use __getattr__ and __setattr__ ?
>>> class A:
... def __getattr__(self, item):
... try:
... return self.__dict__[item]
... except:
... if item not in ('__members__',): #because of pythonwin
... print "get", item
... return "hello"
... def __setattr__(self, item, value):
... if item in self.__dict__:
... self.__dict__[item] = value
... else:
... print "set", item, value
...
>>> a = A()
>>> a.h
get h
'hello'
>>> a.v = 2
set v 2
the special attributes like __members__ etc are not yet handled correct by
the above code.
chris
> Nikolai Kirsebom
>
>
>
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list