Re: wrapping problem with old-style class

2006-12-19 Thread Gerard Flanagan

manstey wrote:

> The ipo class has three basic methods, namely, get, set, and
> run_obj_method (which takes the Cache class method, as its first
> argument, and the list of its arguments as the second argument)
>
> e.g.
> >>> CacheClass=ipo()
> >>> CacheClass.set('Name','John')
> >>> valName = CacheClass.get('Name')
> >>> print valName
> 'John'
>
> I want to add functionality to ipo, so I have wrapped a new style class
> (MyWrapper class, very basic) around it.
>
> PythonCacheClass=MyWrapper(CacheClass)
>
> Now, I want to make PythonCacheClass more pythonic, so I have a
> dictionary of all the properties and values of each ipo. Eg
>
> dicCacheProperties = {'Name':'John', 'Address':'The Strand'} etc
> for key, val in dicCacheProperties.iteritems():
>setattr(PythonCacheClass, key, val)
>
> So now I have:
> >>>print PythonCacheClass.Name
> 'John'
>
> My problem is this: how do I link the set and get methods of the ipo
> class with the set and get methods in the wrapper class
> PythonCacheClass?
>
> So, I would like the following code:
> >>> PythonCacheClass.Name='Alexander'
> to execute automatically
> CacheClass.set('Name','Alexander')etc
>

Maybe try:

class WrapperClass(BaseClass, object):

get = object.__getattribute__
set = object.__setattr_

Gerard

-- 
http://mail.python.org/mailman/listinfo/python-list


wrapping problem with old-style class

2006-12-19 Thread manstey
I have a problem I would like some advice on.

We have a Python binding to the Intersystems Cache OO database. It
provides an old style class in Python as an in memory instance of a
Cache class, and this is a intersystems.pythonbind.object type (= ipo).

The ipo class has three basic methods, namely, get, set, and
run_obj_method (which takes the Cache class method, as its first
argument, and the list of its arguments as the second argument)

e.g.
>>> CacheClass=ipo()
>>> CacheClass.set('Name','John')
>>> valName = CacheClass.get('Name')
>>> print valName
'John'

I want to add functionality to ipo, so I have wrapped a new style class
(MyWrapper class, very basic) around it.

PythonCacheClass=MyWrapper(CacheClass)

Now, I want to make PythonCacheClass more pythonic, so I have a
dictionary of all the properties and values of each ipo. Eg

dicCacheProperties = {'Name':'John', 'Address':'The Strand'} etc
for key, val in dicCacheProperties.iteritems():
   setattr(PythonCacheClass, key, val)

So now I have:
>>>print PythonCacheClass.Name
'John'

My problem is this: how do I link the set and get methods of the ipo
class with the set and get methods in the wrapper class
PythonCacheClass?

So, I would like the following code:
>>> PythonCacheClass.Name='Alexander'
to execute automatically
CacheClass.set('Name','Alexander')etc

My thinking is that inside the PythonCacheClass, I can reference the
ipo class by self.get() - is this right? If so, how then do I set the
set attribute of the PythonCacheClass to automatically run

self.get().set('Name','Alexander')

I hope this is sufficiently clear. Am I tackling the problem the right
way?

-- 
http://mail.python.org/mailman/listinfo/python-list