Also available at

Also available at my website and on Twitter @toshafanasiev

Friday 10 September 2010

Python slice operator

I've no doubt that this is in the Python docs ( ) somewhere ...

Python's slice operator for sequence-like objects is invariant as follows:

s[:n] + s[n:] == s

where s is a sequence and n is in the range [ 0, len(s) )

Monday 6 September 2010

Python floating point range function

I recently noticed that Python's built in range( [start,] stop [,step] ) function does not support floating point values for the optional step parameter ( not 2.6.2 at least ), so until this is fixed, or in cases where you're stuck with it, here's a workaround:

def rangef( min, max, step=1.0 ):
 '''rangef( min, max, step=1.0 )\nlike range() but supports floats'''
 sf = 1.0 / step # scalefactor
 for i in range( int( min * sf ), int( max * sf ) ):
  yield i / sf