[Zope] Random images--but with a caveat

Andy McKay andy@agmweb.ca
Fri, 21 Jun 2002 16:53:12 -0700


Here you go...

# or whatever the path to your folder is...
l = context.portal_skins.Images.objectIds('Image')

# no such images at all
if not l: return None

for x in range(0, 100): # only try 100 times
    i = getattr(context, random.choice(l))
    if i.hasProperty('Cutline'):
        return i

# in 100 tries found no such image
return None

Of course you could cache the images you have tried to improve performance.
Depends upon the ratio of images with the property i guess...

----- Original Message -----
From: "Oliver Bleutgen" <myzope@gmx.net>
To: "Chris Muldrow" <cmuldrow@fredericksburg.com>
Cc: <zope@zope.org>
Sent: Friday, June 21, 2002 2:00 PM
Subject: Re: [Zope] Random images--but with a caveat


> Chris Muldrow wrote:
> > I'm trying to do random image generation on our site, and I've found
lots of
> > messages that suggest using whrandom inside a DTML tag like so:
> >
> > <dtml-var expr="_.whrandom.choice(objectValues('Image'))">
> I'd use (untested, but probably right)
> <dtml-var expr="_.getitem(_.whrandom.choice(objectIds('Image')))">
>
> for performace reasons. First, choose your image from the list of ids
> (objectIds), which is much faster to get, and the get the object with
> this id.
>
> > Or code to that effect...
> > But here's my catch: I need to check the image that is returned randomly
for
> > a Cutline property. If the property is not there, I need to kick that
image
> > out and randomly grab another that does have a Cutline. I'm thinking a
> > Python Script would be better suited for this, but I can't seem to get
the
> > syntax to work out the way it should. Any ideas?
> > Thanks! - Chris
>
> In light of what you say here, what I wrote above doesn't help you,
> because to get the property, you need the object.
> OTOH, in light of what I said above, you might reconsider what you are
> doing now ;-).
> Really, wouldn't it be possible to just put the images in two different
> folder instead of tacking a property to them?
>
> If you really want to do it your way, just build a list of images with
> the Cutline property and do a random choice on this list.
>
> cheers,
> oliver
>
>
>
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>