Skip to content

Commit

Permalink
Added a safe 'range' function to the _ namespace. Contributed by Mart…
Browse files Browse the repository at this point in the history
…ijn Pieters. This function is limited to ranges of 1000 items or less.
  • Loading branch information
latteier committed Apr 20, 1999
1 parent 8c26390 commit 7216ce5
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions DT_Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
# attributions are listed in the accompanying credits file.
#
##############################################################################
'''$Id: DT_Util.py,v 1.54 1999/04/05 13:14:01 jim Exp $'''
__version__='$Revision: 1.54 $'[11:-2]
'''$Id: DT_Util.py,v 1.55 1999/04/20 04:02:23 amos Exp $'''
__version__='$Revision: 1.55 $'[11:-2]

import regex, string, math, os
from string import strip, join, atoi, lower, split, find
Expand Down Expand Up @@ -173,6 +173,24 @@ def careful_getslice(md, seq, *indexes):

return v

def careful_range(md, iFirst, *args):
# limited range function from Martijn Pieters
RANGELIMIT = 1000
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, 'range() requires 1-3 int arguments'
if iStep == 0: raise ValueError, 'zero step for range()'
iLen = int((iEnd - iStart) / iStep)
if iLen < 0: iLen = 0
if iLen >= RANGELIMIT: raise ValueError, 'range() too large'
return range(iStart, iEnd, iStep)


import string, math, whrandom

try:
Expand Down Expand Up @@ -213,6 +231,7 @@ def obsolete_attr(self, inst, name, md):
d['attr']=obsolete_attr
d['getattr']=careful_getattr
d['hasattr']=careful_hasattr
d['range']=careful_range

class namespace_: pass

Expand Down

0 comments on commit 7216ce5

Please sign in to comment.