Lichen

Change of lib/__builtins__/sequence.py

6:f551873980e5
lib/__builtins__/sequence.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/__builtins__/sequence.py	Tue Aug 30 21:55:58 2016 +0200
     1.3 @@ -0,0 +1,96 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +"""
     1.7 +Sequence operations.
     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 +from native import _isinstance
    1.26 +
    1.27 +def _getitem(seq, index):
    1.28 +
    1.29 +    "Return the item or slice specified by 'index'."
    1.30 +
    1.31 +    if _isinstance(index, int):
    1.32 +        index = _normalise_index(index, seq.__len__())
    1.33 +        return seq.__get_single_item__(index)
    1.34 +    elif _isinstance(index, slice):
    1.35 +        return seq.__getslice__(index.start, index.end)
    1.36 +    else:
    1.37 +        raise TypeError
    1.38 +
    1.39 +def _getslice(seq, start, end=None):
    1.40 +
    1.41 +    "Return a slice starting from 'start', with the optional 'end'."
    1.42 +
    1.43 +    length = seq.__len__()
    1.44 +
    1.45 +    if start is None:
    1.46 +        start = 0
    1.47 +    else:
    1.48 +        start = _normalise_index(start, length)
    1.49 +
    1.50 +    if end is None:
    1.51 +        end = length
    1.52 +    else:
    1.53 +        end = _normalise_index(end, length)
    1.54 +
    1.55 +    result = []
    1.56 +    while start < end:
    1.57 +        result.append(seq.__get_single_item__(start))
    1.58 +        start += 1
    1.59 +    return result
    1.60 +
    1.61 +def _get_absolute_index(index, length):
    1.62 +
    1.63 +    """
    1.64 +    Return the absolute index for 'index' given a collection having the
    1.65 +    specified 'length'.
    1.66 +    """
    1.67 +
    1.68 +    if index < 0:
    1.69 +        return length + index
    1.70 +    else:
    1.71 +        return index
    1.72 +
    1.73 +def _normalise_index(index, length):
    1.74 +
    1.75 +    "Normalise 'index' for a collection having the specified 'length'."
    1.76 +
    1.77 +    return _min(length, _max(0, _get_absolute_index(index, length)))
    1.78 +
    1.79 +def _max(x, y):
    1.80 +
    1.81 +    "Return the maximum of 'x' and 'y'."
    1.82 +
    1.83 +    if x >= y:
    1.84 +        return x
    1.85 +    else:
    1.86 +        return y
    1.87 +
    1.88 +def _min(x, y):
    1.89 +
    1.90 +    "Return the minimum of 'x' and 'y'."
    1.91 +
    1.92 +    if x <= y:
    1.93 +        return x
    1.94 +    else:
    1.95 +        return y
    1.96 +
    1.97 +def _tuple(l): pass
    1.98 +
    1.99 +# vim: tabstop=4 expandtab shiftwidth=4