paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Sequence operations. |
paul@6 | 5 | |
paul@224 | 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@6 | 22 | from native import _isinstance |
paul@6 | 23 | |
paul@224 | 24 | class sequence: |
paul@224 | 25 | |
paul@224 | 26 | "A common base class for sequence types." |
paul@6 | 27 | |
paul@224 | 28 | def __getitem__(self, index): |
paul@224 | 29 | |
paul@224 | 30 | "Return the item or slice specified by 'index'." |
paul@224 | 31 | |
paul@224 | 32 | # Normalise any integer indexes, converting negative indexes to positive |
paul@224 | 33 | # ones. |
paul@6 | 34 | |
paul@224 | 35 | if _isinstance(index, int): |
paul@224 | 36 | index = _normalise_index(index, self.__len__()) |
paul@224 | 37 | return self.__get_single_item__(index) |
paul@224 | 38 | |
paul@224 | 39 | # Handle slices separately. |
paul@6 | 40 | |
paul@224 | 41 | elif _isinstance(index, slice): |
paul@224 | 42 | return self.__getslice__(index.start, index.end) |
paul@224 | 43 | |
paul@224 | 44 | # No other kinds of objects are supported as indexes. |
paul@6 | 45 | |
paul@224 | 46 | else: |
paul@224 | 47 | raise TypeError |
paul@224 | 48 | |
paul@224 | 49 | def __getslice__(self, start, end=None): |
paul@6 | 50 | |
paul@224 | 51 | "Return a slice starting from 'start', with the optional 'end'." |
paul@224 | 52 | |
paul@224 | 53 | length = self.__len__() |
paul@6 | 54 | |
paul@224 | 55 | # Handle a null start as the first position, otherwise normalising any |
paul@224 | 56 | # start index. |
paul@224 | 57 | |
paul@224 | 58 | if start is None: |
paul@224 | 59 | start = 0 |
paul@224 | 60 | else: |
paul@224 | 61 | start = _normalise_index(start, length) |
paul@6 | 62 | |
paul@224 | 63 | # Handle a null end as the first position after the end of the sequence, |
paul@224 | 64 | # otherwise normalising any end index. |
paul@224 | 65 | |
paul@224 | 66 | if end is None: |
paul@224 | 67 | end = length |
paul@224 | 68 | else: |
paul@224 | 69 | end = _normalise_index(end, length) |
paul@6 | 70 | |
paul@224 | 71 | result = [] |
paul@224 | 72 | |
paul@224 | 73 | while start < end: |
paul@224 | 74 | result.append(self.__get_single_item__(start)) |
paul@224 | 75 | start += 1 |
paul@224 | 76 | |
paul@224 | 77 | return result |
paul@6 | 78 | |
paul@6 | 79 | def _get_absolute_index(index, length): |
paul@6 | 80 | |
paul@6 | 81 | """ |
paul@6 | 82 | Return the absolute index for 'index' given a collection having the |
paul@6 | 83 | specified 'length'. |
paul@6 | 84 | """ |
paul@6 | 85 | |
paul@6 | 86 | if index < 0: |
paul@6 | 87 | return length + index |
paul@6 | 88 | else: |
paul@6 | 89 | return index |
paul@6 | 90 | |
paul@6 | 91 | def _normalise_index(index, length): |
paul@6 | 92 | |
paul@6 | 93 | "Normalise 'index' for a collection having the specified 'length'." |
paul@6 | 94 | |
paul@6 | 95 | return _min(length, _max(0, _get_absolute_index(index, length))) |
paul@6 | 96 | |
paul@6 | 97 | def _max(x, y): |
paul@6 | 98 | |
paul@6 | 99 | "Return the maximum of 'x' and 'y'." |
paul@6 | 100 | |
paul@6 | 101 | if x >= y: |
paul@6 | 102 | return x |
paul@6 | 103 | else: |
paul@6 | 104 | return y |
paul@6 | 105 | |
paul@6 | 106 | def _min(x, y): |
paul@6 | 107 | |
paul@6 | 108 | "Return the minimum of 'x' and 'y'." |
paul@6 | 109 | |
paul@6 | 110 | if x <= y: |
paul@6 | 111 | return x |
paul@6 | 112 | else: |
paul@6 | 113 | return y |
paul@6 | 114 | |
paul@6 | 115 | def _tuple(l): pass |
paul@6 | 116 | |
paul@6 | 117 | # vim: tabstop=4 expandtab shiftwidth=4 |