paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Span-related objects. |
paul@6 | 5 | |
paul@294 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@294 | 22 | from __builtins__.sequence import _max, _min |
paul@294 | 23 | |
paul@294 | 24 | class xrange: |
paul@6 | 25 | |
paul@6 | 26 | "Implementation of xrange." |
paul@6 | 27 | |
paul@6 | 28 | NO_END = object() |
paul@6 | 29 | |
paul@6 | 30 | def __init__(self, start_or_end, end=NO_END, step=1): |
paul@6 | 31 | |
paul@6 | 32 | "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." |
paul@6 | 33 | |
paul@6 | 34 | if end is xrange.NO_END: |
paul@6 | 35 | self.start = 0 |
paul@6 | 36 | self.end = start_or_end |
paul@6 | 37 | else: |
paul@6 | 38 | self.start = start_or_end |
paul@6 | 39 | self.end = end |
paul@6 | 40 | |
paul@6 | 41 | self.step = step |
paul@294 | 42 | |
paul@294 | 43 | # Constrain the end according to the start and step. |
paul@294 | 44 | |
paul@294 | 45 | if step > 0: |
paul@294 | 46 | self.end = _max(self.start, self.end) |
paul@294 | 47 | elif step < 0: |
paul@294 | 48 | self.end = _min(self.start, self.end) |
paul@294 | 49 | else: |
paul@294 | 50 | raise ValueError(self.step) |
paul@294 | 51 | |
paul@6 | 52 | self.current = self.start |
paul@294 | 53 | |
paul@294 | 54 | def __str__(self): |
paul@294 | 55 | |
paul@294 | 56 | "Return a string representation." |
paul@294 | 57 | |
paul@294 | 58 | b = buffer([self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"]) |
paul@294 | 59 | return str(b) |
paul@294 | 60 | |
paul@294 | 61 | __repr__ = __str__ |
paul@294 | 62 | |
paul@294 | 63 | def __len__(self): |
paul@294 | 64 | |
paul@294 | 65 | "Return the length of the range." |
paul@294 | 66 | |
paul@294 | 67 | return (self.end - self.start) / self.step |
paul@6 | 68 | |
paul@6 | 69 | def __iter__(self): |
paul@6 | 70 | |
paul@6 | 71 | "Return an iterator, currently self." |
paul@6 | 72 | |
paul@6 | 73 | return self |
paul@6 | 74 | |
paul@6 | 75 | def next(self): |
paul@6 | 76 | |
paul@6 | 77 | "Return the next item or raise a StopIteration exception." |
paul@6 | 78 | |
paul@294 | 79 | if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end: |
paul@294 | 80 | raise StopIteration() |
paul@6 | 81 | |
paul@6 | 82 | current = self.current |
paul@6 | 83 | self.current += self.step |
paul@6 | 84 | return current |
paul@6 | 85 | |
paul@6 | 86 | class slice(xrange): |
paul@6 | 87 | |
paul@6 | 88 | "Implementation of slice." |
paul@6 | 89 | |
paul@6 | 90 | def __init__(self, start_or_end=None, end=xrange.NO_END, step=1): |
paul@6 | 91 | |
paul@6 | 92 | "Initialise the slice with the given 'start_or_end', 'end' and 'step'." |
paul@6 | 93 | |
paul@121 | 94 | get_using(xrange.__init__, self)(start_or_end, end, step) |
paul@6 | 95 | |
paul@294 | 96 | def range(start_or_end, end=None, step=1): |
paul@294 | 97 | |
paul@294 | 98 | "Implementation of range." |
paul@294 | 99 | |
paul@294 | 100 | return list(xrange(start_or_end, end, step)) |
paul@294 | 101 | |
paul@6 | 102 | # vim: tabstop=4 expandtab shiftwidth=4 |