PHP style variable variables in Python?
Hello, First of all I am new to Python and Zope coming from Java/PHP. I am creating a simple Python script that displays a random image (image1, image2, image3....). All I want to do do is generate a random number which is concatenated to the end of the context attribute similar to this example in PHP: $randomNumber = ***some random number*** return context.{'image'.$randomNumber} Is this possible (and easy) in Python? Thanks, Michael LaPera
On Tue, 09 Apr 2002 23:39:50 -0400 "Michael LaPera" <mlapera@colorbyte.com> wrote:
All I want to do do is generate a random number which is concatenated to the end of the context attribute similar to this example in PHP:
try this in a python script and read the ZopeHelp/ApiReference/random. ############## from random import uniform,random # Generates a float random between 0 and 1 print random() # Generates an integer random between 5 and 50 print int(uniform(5,50)) # Generates titleOfCurrentContext_randomNumberBetween1-100 print "%s_%d" % (context.title, int(uniform(1,100))) return printed ############## greetings, maik. -- maik jablonski http://www.sachunterricht-online.de universitaet bielefeld http://www.zfl.uni-bielefeld.de zentrum fuer lehrerbildung tlph://+49.(0).521.106.4234
If all of the images are in one folder, then the choice function of the random module will work. For example: from random import choice random_id = choice(context.objectIds('Image')) return context[random_id] This assumes that the current context is a folder (or folderish object at least). I use objectIds above (instead of objectValues) to prevent every image in the folder from being loaded into memory when this is called. hth, -Casey Michael LaPera wrote:
Hello,
First of all I am new to Python and Zope coming from Java/PHP.
I am creating a simple Python script that displays a random image (image1, image2, image3....). All I want to do do is generate a random number which is concatenated to the end of the context attribute similar to this example in PHP:
$randomNumber = ***some random number*** return context.{'image'.$randomNumber}
Is this possible (and easy) in Python?
Thanks, Michael LaPera
_______________________________________________ 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 )
Michael LaPera writes:
I am creating a simple Python script that displays a random image (image1, image2, image3....). All I want to do do is generate a random number which is concatenated to the end of the context attribute similar to this example in PHP:
$randomNumber = ***some random number*** return context.{'image'.$randomNumber}
Is this possible (and easy) in Python? It is.
It does not use special syntax but a built in function: "getattr". It looks like: return getattr(context,'image' + `randomNumber`) "+" is concatenation for strings and "`...`" is string representation. Dieter
participants (4)
-
Casey Duncan -
Dieter Maurer -
Maik Jablonski -
Michael LaPera