gnumed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gnumed-devel] proposal for dbapi base class


From: Horst Herb
Subject: Re: [Gnumed-devel] proposal for dbapi base class
Date: Tue, 21 Jan 2003 00:59:37 +1100
User-agent: KMail/1.4.3

On Tue, 21 Jan 2003 00:27, Karsten Hilbert wrote:
> What I mean is that this suggests that the entire record is
> fetched at every invocation (subject to caching, of course).
> No way to specify fields, no way to specify conditions apart
> from the primary key. But that may be intentional.

Ah, now I understand what you mean.
No, not a problem, since all attribute access is implemented "lazy". Lazy 
attributes in combination with backend messenging is a good caching strategy 
anyway.

Lets suppose you have a class A, with attributes a,b,c.
when I access attributes like myvar =A.a, in reality Python calls the method 
A.__getattr__('a') - which you can override. In this overridden method you 
watch for a key error exception and catch it by fetching the attribute from 
the backend, else -if no exception- you simply return what has already been 
fetched.

Example:

        class lazytable:
                def __getattr__(self, key):
                        try:
                                return self.__dict__[key]
                        except:
                                value = fetch_from_backend(key)
                                self.__dict__[key]= value
                                return value

Python makes it so easy it almost gets boring.

Horst




reply via email to

[Prev in Thread] Current Thread [Next in Thread]