1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/__builtins__/span.py Tue Aug 30 21:55:58 2016 +0200
1.3 @@ -0,0 +1,71 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Span-related objects.
1.8 +
1.9 +Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +class xrange(object):
1.26 +
1.27 + "Implementation of xrange."
1.28 +
1.29 + NO_END = object()
1.30 +
1.31 + def __init__(self, start_or_end, end=NO_END, step=1):
1.32 +
1.33 + "Initialise the xrange with the given 'start_or_end', 'end' and 'step'."
1.34 +
1.35 + if end is xrange.NO_END:
1.36 + self.start = 0
1.37 + self.end = start_or_end
1.38 + else:
1.39 + self.start = start_or_end
1.40 + self.end = end
1.41 +
1.42 + self.step = step
1.43 + self.current = self.start
1.44 + self.limited = self.end is not xrange.NO_END
1.45 +
1.46 + def __iter__(self):
1.47 +
1.48 + "Return an iterator, currently self."
1.49 +
1.50 + return self
1.51 +
1.52 + def next(self):
1.53 +
1.54 + "Return the next item or raise a StopIteration exception."
1.55 +
1.56 + if self.limited:
1.57 + if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end:
1.58 + raise StopIteration()
1.59 +
1.60 + current = self.current
1.61 + self.current += self.step
1.62 + return current
1.63 +
1.64 +class slice(xrange):
1.65 +
1.66 + "Implementation of slice."
1.67 +
1.68 + def __init__(self, start_or_end=None, end=xrange.NO_END, step=1):
1.69 +
1.70 + "Initialise the slice with the given 'start_or_end', 'end' and 'step'."
1.71 +
1.72 + xrange.__init__(self, start_or_end, end, step)
1.73 +
1.74 +# vim: tabstop=4 expandtab shiftwidth=4