This may be a newbie question, but I've loked thru the doc and didn't spot any obvious way to do this, short of an External Method. What I want to do is given a limit number, iterate over a sequence of DTML commands. This would be an obvious use for #in (which I'm using elsewhere for other things) except I don't have a sequence - just the number of items in the sequence, as it were. In psuedo-code, what I'm after is something like this: for (i=0; i < <!--#var max_items-->; i++) { // some DTML/HTML code using "i" } Now if, given a #var, I could get a list - e.g.: <!--#var max_items--> --> list = [0, 1, 2, ... max_items] or [1, 2, ... max_items], it doesn't make much difference. then I could just use: <!--#in list--> // some stuff using <!--#var sequence-index--> <!--#/in--> Is there an easy way to do this? Is there *any* way to do it? -Bill Randle Central Oregon Internet billr@coinet.com
At 03:26 13-3-99 , Bill Randle wrote:
Now if, given a #var, I could get a list - e.g.: <!--#var max_items--> --> list = [0, 1, 2, ... max_items] or [1, 2, ... max_items], it doesn't make much difference. then I could just use: <!--#in list--> // some stuff using <!--#var sequence-index--> <!--#/in-->
Is there an easy way to do this? Is there *any* way to do it?
One way of doing this would be using an external method: def MyRange(iMax): return range(iMax) -- M.J. Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-6254545 Fax: +31-35-6254555 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
On Fri, Mar 12, 1999 at 06:26:42PM -0800, Bill Randle wrote:
This may be a newbie question, but I've loked thru the doc and didn't spot any obvious way to do this, short of an External Method.
I didn't find any other way, either. But here's an External Method you can use. --- begin loop_range.py --- LoopRangeError = 'LoopRangeError' # Name an exception class. loop_range_limit = 100 # Maximum allowable loop index. def loop_range(max): if max <= loop_range_limit: return range(max) else: raise LoopRangeError, max --- end --- Notice that it won't allow the "max" parameter to be bigger than 100. This prevents users from generating near-infinite amounts of HTML and freezing your server. You can edit this to any value you like, or even remove it if you trust all your DTML authors. Now, create an External method with the following properties: id: loop_range title: Loop Iteration Method method: loop_range file: loop_range To use this method in DTML, try the expr attribute of the #in construct: --- begin sample DTML --- <!--#in expr="loop_range(max=50)"--> <P>Index: <!--#var sequence-item--></P> <!--#/in--> --- end ---
Is there an easy way to do this? Is there *any* way to do it?
Yup. ;-) Zope has tons of power hiding under the hood, and a skilled Python programmer could make it do almost anything. Cheers, Eric
Eric Kidd wrote:
On Fri, Mar 12, 1999 at 06:26:42PM -0800, Bill Randle wrote:
This may be a newbie question, but I've loked thru the doc and didn't spot any obvious way to do this, short of an External Method.
I didn't find any other way, either. But here's an External Method you can use.
[snip external method] I'm wondering; is there a reason why 'range' isn't accessible from DTML in the '_' namespace? Is the reason as you indicated in your external method, i.e. that you don't want people to generate vast ranges from DTML? Just curious, Martijn
At 11:57 16/03/99 , Martijn Faassen wrote:
Eric Kidd wrote:
On Fri, Mar 12, 1999 at 06:26:42PM -0800, Bill Randle wrote:
This may be a newbie question, but I've loked thru the doc and didn't spot any obvious way to do this, short of an External Method.
I didn't find any other way, either. But here's an External Method you can use.
[snip external method]
I'm wondering; is there a reason why 'range' isn't accessible from DTML in the '_' namespace? Is the reason as you indicated in your external method, i.e. that you don't want people to generate vast ranges from DTML?
Just curious,
The reason if of course merory flooding attacks. A range that asks for a list of 3 billion items is a wee bit unpolite to the machine. A way of checking would be: RANGELIMIT = 1000 def SafeRange(iFirst, *args): if not len(args): iStart, iEnd, iStep = 0, iFirst, 1 elif len(args) == 1: iStart, iEnd, iStep = iFirst, args[0], 1 elif len(args) == 2: iStart, iEnd, iStep = iFirst, args[0], args[1] else: raise AttributeError, 'SafeRange() requires 1-3 int arguments' if iStep == 0: raise ValueError, 'zero step for SafeRange()' iLen = int((iEnd - iStart) / iStep) if iLen < 0: iLen = 0 if iLen >= RANGELIMIT: raise ValueError, 'SafeRange() too large' return range(iStart, iEnd, iStep) Maybe this should be part of the _ object. Can someone turn this into a patch and supply this to DC? I´m a bit short of time right now. -- M.J. Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-6254545 Fax: +31-35-6254555 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
participants (4)
-
Bill Randle -
Eric Kidd -
Martijn Faassen -
Martijn Pieters