[PEAK] very elementary peak.model question

Doug Quale quale1 at charter.net
Wed Jul 14 17:35:24 EDT 2004

"Phillip J. Eby" <pje at telecommunity.com> writes:

> At 02:55 PM 7/13/04 -0500, Doug Quale wrote:
> >The lazy compute-once attributes provided by peak.binding are
> >invaluable.  What is the best way to get this behavior from
> >peak.model?
> And all of this is assuming lazy attributes are what you want in the
> first place... see below.

Thanks for your help.

Right, I wasn't very precise.  The attributes don't need to be lazy.
It would be just as good to bind to a list proxy.  I just want to
delay the query until the value is actually used.

> >Maybe I'm thinking about this backwards.  It looks like when using a
> >peak DM the DM would manage this as part of creating model objects,
> >possibly by using storage.QueryLink().  Would it be best to not have
> >model objects load themselves?  If so, what would that look like?
> (My answer is going to assume you've already studied the IntroToPeak
> tutorial on the Wiki for basic DM operations, which will answer most
> of what that would "look like":.)

I have gone through the IntroToPeak tutorial and it was very helpful.
This question was not very good since I realized that the tutorial
already answers it by giving an example of this organization.

> A DM's '_load' method can load actual attribute values into an object,
> *OR* it can load LazyLoader instances (see peak.storage.lazy_loader).
> LazyLoaders are objects with a 'load(ob,attrName)' method.
> model.Element objects already have logic to notice when one of their
> attributes are a LazyLoader instance, and invoke it.  The LazyLoader
> must then poke whatever values it needs to, into the object's
> dictionary.
> So in a sense, your hack is already part of the framework.  :)

If it's part of the framework, it isn't a hack :)

I tried LazyLoader with a model.Sequence feature and unless I'm doing
something wrong it doesn't work.  (It works fine with model.Attribute.)
This is a silly example, but it's short:

>>> from peak.api import *
>>> class bar(model.Element):
        class x(model.Sequence):

>>> class lazy(storage.LazyLoader):
        def load(self, ob, attr):
            setattr(ob, attr, (1, 2))

>>> b = bar()
>>> b.x = lazy()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.3/site-packages/peak/model/features.py", line 79, in __set__
  File "/usr/local/lib/python2.3/site-packages/peak/model/features.py", line 341, in set
    for v in val:
TypeError: iteration over non-sequence

At this point val is a lazy object rather than a sequence.

More information about the PEAK mailing list