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@367 | 24 | class slice: |
paul@6 | 25 | |
paul@367 | 26 | "Implementation of slice." |
paul@6 | 27 | |
paul@6 | 28 | NO_END = object() |
paul@6 | 29 | |
paul@367 | 30 | def __init__(self, start_or_end=None, end=NO_END, step=1): |
paul@6 | 31 | |
paul@367 | 32 | "Initialise the slice 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@367 | 41 | if step == 0: |
paul@367 | 42 | raise ValueError(self.step) |
paul@367 | 43 | |
paul@6 | 44 | self.step = step |
paul@294 | 45 | |
paul@367 | 46 | def __str__(self): |
paul@367 | 47 | |
paul@367 | 48 | "Return a string representation." |
paul@367 | 49 | |
paul@367 | 50 | b = buffer([self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"]) |
paul@367 | 51 | return str(b) |
paul@367 | 52 | |
paul@367 | 53 | __repr__ = __str__ |
paul@367 | 54 | |
paul@367 | 55 | class xrange(slice): |
paul@367 | 56 | |
paul@367 | 57 | "Implementation of xrange." |
paul@367 | 58 | |
paul@367 | 59 | def __init__(self, start_or_end, end=slice.NO_END, step=1): |
paul@367 | 60 | |
paul@367 | 61 | "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." |
paul@367 | 62 | |
paul@367 | 63 | get_using(slice.__init__, self)(start_or_end, end, step) |
paul@367 | 64 | |
paul@294 | 65 | # Constrain the end according to the start and step. |
paul@294 | 66 | |
paul@294 | 67 | if step > 0: |
paul@294 | 68 | self.end = _max(self.start, self.end) |
paul@294 | 69 | elif step < 0: |
paul@294 | 70 | self.end = _min(self.start, self.end) |
paul@294 | 71 | else: |
paul@294 | 72 | raise ValueError(self.step) |
paul@294 | 73 | |
paul@294 | 74 | def __len__(self): |
paul@294 | 75 | |
paul@294 | 76 | "Return the length of the range." |
paul@294 | 77 | |
paul@294 | 78 | return (self.end - self.start) / self.step |
paul@6 | 79 | |
paul@6 | 80 | def __iter__(self): |
paul@6 | 81 | |
paul@6 | 82 | "Return an iterator, currently self." |
paul@6 | 83 | |
paul@369 | 84 | return xrangeiterator(self) |
paul@369 | 85 | |
paul@369 | 86 | class xrangeiterator: |
paul@369 | 87 | |
paul@369 | 88 | "An iterator over an xrange." |
paul@369 | 89 | |
paul@369 | 90 | def __init__(self, obj): |
paul@369 | 91 | |
paul@369 | 92 | "Initialise the iterator with the given 'obj'." |
paul@369 | 93 | |
paul@369 | 94 | self.start = obj.start |
paul@369 | 95 | self.end = obj.end |
paul@369 | 96 | self.step = obj.step |
paul@369 | 97 | self.current = obj.start |
paul@6 | 98 | |
paul@6 | 99 | def next(self): |
paul@6 | 100 | |
paul@6 | 101 | "Return the next item or raise a StopIteration exception." |
paul@6 | 102 | |
paul@294 | 103 | if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end: |
paul@369 | 104 | raise StopIteration |
paul@6 | 105 | |
paul@6 | 106 | current = self.current |
paul@6 | 107 | self.current += self.step |
paul@6 | 108 | return current |
paul@6 | 109 | |
paul@294 | 110 | def range(start_or_end, end=None, step=1): |
paul@294 | 111 | |
paul@294 | 112 | "Implementation of range." |
paul@294 | 113 | |
paul@294 | 114 | return list(xrange(start_or_end, end, step)) |
paul@294 | 115 | |
paul@6 | 116 | # vim: tabstop=4 expandtab shiftwidth=4 |