[Zope] Returning a random image from a folder

J Cameron Cooper jccooper@jcameroncooper.com
Sat, 01 Mar 2003 12:51:23 -0600


>
>
>My randomImage Python script is as follows (where 'directory' is the parameter 
>being passed to the script):
>
>  import string
>  import random
>
>  directory_context = string.replace("/", ".", directory)
>  return directory + "/" + 
>random.choice(context.directory_context.objectValues('Image'))
>  
>
Passing in a string makes this trickier, as I guess you've already 
realized. What you're doing in

context.directory_context.objectValues('Image')

is trying to call a string (directory_context) like an ObjectManager. 
Not going to work. Python lets you get away with a lot, but not that much.

As suggested previously, you can turn the string into an object by 
restrictedTraverse(). In similar situations, involving non-traversable 
attributes, you might use Python's getattr() built-in.

            --jcc