Python 2.2 / Properties 
Author Message
 Python 2.2 / Properties

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.

Nikolai Kirsebom



Mon, 21 Jun 2004 21:48:10 GMT  
 Python 2.2 / Properties
[posted and mailed]



Quote:
> 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__ ?

Quote:
>>> 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
...
Quote:
>>> a = A()
>>> a.h

get h
'hello'
Quote:
>>> a.v = 2

set v 2

the special attributes like __members__ etc are not yet handled correct by
the above code.

chris

Quote:
> Nikolai Kirsebom

--



Mon, 21 Jun 2004 22:15:27 GMT  
 Python 2.2 / Properties

Hi Nikolai,

In 2.2 properties are subclassable. So this works (note the nested
scopes):

class altproperty(property):
    def __init__(self, name, docstr):
        def getter(inst):
            print 'Get %s of %s' % (name, inst)
            return getattr(inst, '_'+name)
        def setter(inst, val):
            print 'Set %s of %s to  %s' % (name, inst, val)
            setattr(inst, '_'+name, val)
        property.__init__(self, getter, setter, None, docstr)

class A(object):
    a = altproperty('a', 'a doc')
    b = altproperty('b', 'b doc')

a = A()

print a
a.a = 10
print a.a
a.b = 20
print a.b

giving

Set a of <__main__.A object at 0x814894c> to  10
Get a of <__main__.A object at 0x814894c>
10
Set b of <__main__.A object at 0x814894c> to  20
Get b of <__main__.A object at 0x814894c>
20

Replace the getter with your repProp* and Presto!

Quote:

> 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.

> Nikolai Kirsebom

Hope this helps,

Roeland
--

"Half of what I say is nonsense. Unfortunately I don't know which half"



Tue, 22 Jun 2004 02:31:31 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Semantic of Python 2.2 properties

2. Python 2.2 properties

3. properties in Python 2.2

4. Properties fun with 2.2

5. 2.2 properties and subclasses

6. Proper usage of properties in 2.2

7. Python ActiveX Scripting Engine (0x80020009) Error - KeyboardInterrupt - python 2.2

8. Python 2.2 + Win32All 146 = Python/ASP Failures

9. Python 2.0 / Python 2.2

10. Python 2.1 == Jython 2.1 != Python 2.2?

11. VIM - python errors after upgrading to Python 2.2 (Windows OLE)

12. ANN: empy 2.2 -- A templating system for Python

 

 
Powered by phpBB® Forum Software