MCPcopy Create free account
hub / github.com/ActiveState/code / xrange

Class xrange

recipes/Python/521885_pythonic/recipe-521885.py:1–64  ·  view source on GitHub ↗

A pure-python implementation of xrange. Can handle float/long start/stop/step arguments and slice indexing

Source from the content-addressed store, hash-verified

1class xrange(object):
2 """A pure-python implementation of xrange.
3
4 Can handle float/long start/stop/step arguments and slice indexing"""
5
6 __slots__ = ['_slice']
7 def __init__(self, *args):
8 self._slice = slice(*args)
9 if self._slice.stop is None:
10 # slice(*args) will never put None in stop unless it was
11 # given as None explicitly.
12 raise TypeError("xrange stop must not be None")
13
14 @property
15 def start(self):
16 if self._slice.start is not None:
17 return self._slice.start
18 return 0
19 @property
20 def stop(self):
21 return self._slice.stop
22 @property
23 def step(self):
24 if self._slice.step is not None:
25 return self._slice.step
26 return 1
27
28 def __hash__(self):
29 return hash(self._slice)
30
31 def __cmp__(self, other):
32 return (cmp(type(self), type(other)) or
33 cmp(self._slice, other._slice))
34
35 def __repr__(self):
36 return '%s(%r, %r, %r)' % (self.__class__.__name__,
37 self.start, self.stop, self.step)
38
39 def __len__(self):
40 return self._len()
41
42 def _len(self):
43 return max(0, int((self.stop - self.start) / self.step))
44
45 def __getitem__(self, index):
46 if isinstance(index, slice):
47 start, stop, step = index.indices(self._len())
48 return xrange(self._index(start),
49 self._index(stop), step*self.step)
50 elif isinstance(index, (int, long)):
51 if index < 0:
52 fixed_index = index + self._len()
53 else:
54 fixed_index = index
55
56 if not 0 <= fixed_index < self._len():
57 raise IndexError("Index %d out of %r" % (index, self))
58
59 return self._index(fixed_index)
60 else:

Callers 15

_isPointInPolygonFunction · 0.85
_makeRandomDataFunction · 0.85
intersperseMethod · 0.85
__getlinkMethod · 0.85
__iter__Method · 0.85
__str__Method · 0.85
stable_sorted_copyFunction · 0.85
initialiseFunction · 0.85
build_heap_maxFunction · 0.85
VersionFileFunction · 0.85
selectMethod · 0.85
recipe-577698.pyFile · 0.85

Calls

no outgoing calls

Tested by 4

test_tourFunction · 0.68
test_dataFunction · 0.68
test_spreadMethod · 0.68