frange update
--HG-- branch : aspn
This commit is contained in:
@@ -1,6 +1,65 @@
|
|||||||
|
# http://code.activestate.com/recipes/577068-floating-point-range/
|
||||||
|
|
||||||
|
def frange(*args):
|
||||||
|
"""frange([start, ] end [, step [, mode]]) -> generator
|
||||||
|
|
||||||
|
A float range generator. If not specified, the default start is 0.0
|
||||||
|
and the default step is 1.0.
|
||||||
|
|
||||||
|
Optional argument mode sets whether frange outputs an open or closed
|
||||||
|
interval. mode must be an int. Bit zero of mode controls whether start is
|
||||||
|
included (on) or excluded (off); bit one does the same for end. Hence:
|
||||||
|
|
||||||
|
0 -> open interval (start and end both excluded)
|
||||||
|
1 -> half-open (start included, end excluded)
|
||||||
|
2 -> half open (start excluded, end included)
|
||||||
|
3 -> closed (start and end both included)
|
||||||
|
|
||||||
|
By default, mode=1 and only start is included in the output.
|
||||||
|
"""
|
||||||
|
mode = 1 # Default mode is half-open.
|
||||||
|
n = len(args)
|
||||||
|
if n == 1:
|
||||||
|
args = (0.0, args[0], 1.0)
|
||||||
|
elif n == 2:
|
||||||
|
args = args + (1.0,)
|
||||||
|
elif n == 4:
|
||||||
|
mode = args[3]
|
||||||
|
args = args[0:3]
|
||||||
|
elif n != 3:
|
||||||
|
raise TypeError('frange expects 1-4 arguments, got %d' % n)
|
||||||
|
assert len(args) == 3
|
||||||
|
try:
|
||||||
|
start, end, step = [a + 0.0 for a in args]
|
||||||
|
except TypeError:
|
||||||
|
raise TypeError('arguments must be numbers')
|
||||||
|
if step == 0.0:
|
||||||
|
raise ValueError('step must not be zero')
|
||||||
|
if not isinstance(mode, int):
|
||||||
|
raise TypeError('mode must be an int')
|
||||||
|
if mode & 1:
|
||||||
|
i, x = 0, start
|
||||||
|
else:
|
||||||
|
i, x = 1, start+step
|
||||||
|
if step > 0:
|
||||||
|
if mode & 2:
|
||||||
|
from operator import le as comp
|
||||||
|
else:
|
||||||
|
from operator import lt as comp
|
||||||
|
else:
|
||||||
|
if mode & 2:
|
||||||
|
from operator import ge as comp
|
||||||
|
else:
|
||||||
|
from operator import gt as comp
|
||||||
|
while comp(x, end):
|
||||||
|
yield x
|
||||||
|
i += 1
|
||||||
|
x = start + i*step
|
||||||
|
|
||||||
|
|
||||||
# http://code.activestate.com/recipes/66472/
|
# http://code.activestate.com/recipes/66472/
|
||||||
|
|
||||||
def frange(start, end=None, inc=None):
|
def frange_(start, end=None, inc=None):
|
||||||
"A range function, that does accept float increments..."
|
"A range function, that does accept float increments..."
|
||||||
|
|
||||||
if end == None:
|
if end == None:
|
||||||
|
|||||||
Reference in New Issue
Block a user