Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

recursion error using setattr and getattr


Hi,

I m trying to implement an object which contains lazy" variables. My
idea is to alter the getattr and the setattr methods. However I keep
on getting a recursion error.

My idea is that the lazy variable can be stored in a variety of
places, Database, PyTables etc. The lazy variable is a large variable
and so I dont want to hold it in memory all of the time, I d rather
just get it when needed and then store it for future work. Most of the
work will be done using var1 and var2

So ......

class LazyBase(object):
        self.var1 = 0
        self.var2 = None
        def __init__(self, name):
                self.name = name
        def __str__(self):
                return self.name
        def __setattr__(self, attr, k):
                if attr == "var1":            
                        if self.var1 != k
                                self.var1 = k
                                if self.lazy != None
                                        self.lazy = None
                elif attr == "var2"           :
                        if self.var2 != k
                                self.var2 = k
                                if self.lazy != None
                                        self.lazy = None
                else:
                        raise Exception("Attribute")

class SQLLazy(LazyBase):        
        def __init__(self, name, table="test_table"):
                self.table = table
                LazyBase.__init__(name)
                self.lazy = None
        def __getattr__(self, attr):
                if attr == "lazy":
                        if self.lazy == None: # problem line I think
                                print "Hit Database"
                                # use sqlalchemy to get the variable
                                # set self.lazy = lazy
                                return self.lazy
                        else:
                                return self.lazy
                else:
                        try:
                                return Lazy.__getattr__(attr)
                        except:
                                raise Exception("Attribute Exception")
        def __setattr__(self, attr, k):
                if attr == "lazy"
                        raise Exception("Cannot alter lazy")
                else:
                        try:
                                Lazy.__setattr__(attr, k)
                        except:
                                raise Exception("Attribute Exception")

if __name__=='__main__':
        spam = SQLLazy("Test")
        spam.var2 = 10
        print spam.lazy
        print spam.lazy
        spam.var2 = 5
        print spam.lazy

I m expecting the output:
        Hit Database
        Lazy Variable
        Lazy Variable
        Hit Database
        Lazy Variable  

However I just get recursion errors. Does anyone have any ideas how I
can implement this sort of thing through my current method, or is
there a better way to accomplish this.

THanks

Nathan

On 6/7/07, Nathan Harmston <ratchetg@googlemail.com> wrote:

> Hi,

> I m trying to implement an object which contains lazy" variables. My
> idea is to alter the getattr and the setattr methods. However I keep
> on getting a recursion error.

Simplifying radically, you are doing:

    def __setattr__(self, attr, k):
        if attr == "var1":
            self.var1 = k

The last line here sets the var1 attribute, and that goes through your
__setattr__() method again; henct the recursion error. You need to
bypass the  __setattr__() method with something like:

self.__dict__['var1'] = k

--
Cheers,
Simon B.
s@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues

En Thu, 07 Jun 2007 08:23:48 -0300, Nathan Harmston  
<ratchetg@googlemail.com> escribi:

> I m trying to implement an object which contains lazy" variables. My
> idea is to alter the getattr and the setattr methods. However I keep
> on getting a recursion error.

Instead of setattr/getattr, use a property, and you'll get much cleaner  
code.

--
Gabriel Genellina

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc