[Zope] How to get 4 Random DTML Documents

Dylan Reinhardt zope at dylanreinhardt.com
Sat Nov 8 14:43:44 EST 2003


On Sat, 2003-11-08 at 11:32, Dylan Reinhardt wrote:

> -----
> my_pages = {}
> while 1:
>     rnd_page = context.other_script()
>     if not my_pages.has_key(rnd_page):
>         my_pages[rnd_page] = 1
>     if len(my_pages) > 3:
>         break
> return my_pages.keys()
> -----

Here's a better version:

---
my_pages = {}
i = 0
while len(my_pages) < 4  and i < 100:
    i += 1
    rnd_page = context.other_script()
    if not my_pages.has_key(rnd_page):
        my_pages[rnd_page] = 1
return my_pages.keys()
---

This eliminates the risk of huge loop problems caused by several
predicable problems and is better style besides.

HTH,

Dylan





More information about the Zope mailing list