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@227 | 28 | def _str(self, opening, closing): |
paul@227 | 29 | |
paul@227 | 30 | "Serialise this object with the given 'opening' and 'closing' strings." |
paul@227 | 31 | |
paul@227 | 32 | b = buffer() |
paul@227 | 33 | i = 0 |
paul@227 | 34 | l = self.__len__() |
paul@227 | 35 | first = True |
paul@227 | 36 | |
paul@227 | 37 | b.append(opening) |
paul@227 | 38 | while i < l: |
paul@227 | 39 | if first: |
paul@227 | 40 | first = False |
paul@227 | 41 | else: |
paul@227 | 42 | b.append(", ") |
paul@227 | 43 | b.append(repr(self.__get_single_item__(i))) |
paul@227 | 44 | i += 1 |
paul@227 | 45 | b.append(closing) |
paul@227 | 46 | |
paul@227 | 47 | return str(b) |
paul@227 | 48 | |
paul@224 | 49 | def __getitem__(self, index): |
paul@224 | 50 | |
paul@224 | 51 | "Return the item or slice specified by 'index'." |
paul@224 | 52 | |
paul@224 | 53 | # Normalise any integer indexes, converting negative indexes to positive |
paul@224 | 54 | # ones. |
paul@6 | 55 | |
paul@224 | 56 | if _isinstance(index, int): |
paul@265 | 57 | index = _get_absolute_index(index, self.__len__()) |
paul@224 | 58 | return self.__get_single_item__(index) |
paul@224 | 59 | |
paul@224 | 60 | # Handle slices separately. |
paul@6 | 61 | |
paul@224 | 62 | elif _isinstance(index, slice): |
paul@224 | 63 | return self.__getslice__(index.start, index.end) |
paul@224 | 64 | |
paul@224 | 65 | # No other kinds of objects are supported as indexes. |
paul@6 | 66 | |
paul@224 | 67 | else: |
paul@224 | 68 | raise TypeError |
paul@224 | 69 | |
paul@227 | 70 | def __setitem__(self, index, value): |
paul@227 | 71 | |
paul@227 | 72 | "Set at 'index' the given 'value'." |
paul@227 | 73 | |
paul@227 | 74 | # Normalise any integer indexes, converting negative indexes to positive |
paul@227 | 75 | # ones. |
paul@227 | 76 | |
paul@227 | 77 | if _isinstance(index, int): |
paul@265 | 78 | index = _get_absolute_index(index, self.__len__()) |
paul@227 | 79 | return self.__set_single_item__(index, value) |
paul@227 | 80 | |
paul@227 | 81 | # Handle slices separately. |
paul@227 | 82 | |
paul@227 | 83 | elif _isinstance(index, slice): |
paul@227 | 84 | return self.__setslice__(index.start, index.end, value) |
paul@227 | 85 | |
paul@227 | 86 | # No other kinds of objects are supported as indexes. |
paul@227 | 87 | |
paul@227 | 88 | else: |
paul@227 | 89 | raise TypeError |
paul@227 | 90 | |
paul@224 | 91 | def __getslice__(self, start, end=None): |
paul@6 | 92 | |
paul@224 | 93 | "Return a slice starting from 'start', with the optional 'end'." |
paul@224 | 94 | |
paul@224 | 95 | length = self.__len__() |
paul@6 | 96 | |
paul@224 | 97 | # Handle a null start as the first position, otherwise normalising any |
paul@224 | 98 | # start index. |
paul@224 | 99 | |
paul@224 | 100 | if start is None: |
paul@224 | 101 | start = 0 |
paul@224 | 102 | else: |
paul@265 | 103 | start = _get_absolute_index(start, length) |
paul@6 | 104 | |
paul@224 | 105 | # Handle a null end as the first position after the end of the sequence, |
paul@224 | 106 | # otherwise normalising any end index. |
paul@224 | 107 | |
paul@224 | 108 | if end is None: |
paul@224 | 109 | end = length |
paul@224 | 110 | else: |
paul@265 | 111 | end = _get_absolute_index(end, length) |
paul@6 | 112 | |
paul@224 | 113 | result = [] |
paul@224 | 114 | |
paul@224 | 115 | while start < end: |
paul@224 | 116 | result.append(self.__get_single_item__(start)) |
paul@224 | 117 | start += 1 |
paul@224 | 118 | |
paul@224 | 119 | return result |
paul@6 | 120 | |
paul@265 | 121 | def _check_index(self, index): |
paul@265 | 122 | |
paul@265 | 123 | """ |
paul@265 | 124 | Check the given absolute 'index', raising an IndexError if out of |
paul@265 | 125 | bounds. |
paul@265 | 126 | """ |
paul@265 | 127 | |
paul@265 | 128 | if index < 0 or index >= len(self): |
paul@265 | 129 | raise IndexError(index) |
paul@265 | 130 | |
paul@6 | 131 | def _get_absolute_index(index, length): |
paul@6 | 132 | |
paul@6 | 133 | """ |
paul@6 | 134 | Return the absolute index for 'index' given a collection having the |
paul@6 | 135 | specified 'length'. |
paul@6 | 136 | """ |
paul@6 | 137 | |
paul@6 | 138 | if index < 0: |
paul@6 | 139 | return length + index |
paul@6 | 140 | else: |
paul@6 | 141 | return index |
paul@6 | 142 | |
paul@6 | 143 | def _max(x, y): |
paul@6 | 144 | |
paul@6 | 145 | "Return the maximum of 'x' and 'y'." |
paul@6 | 146 | |
paul@6 | 147 | if x >= y: |
paul@6 | 148 | return x |
paul@6 | 149 | else: |
paul@6 | 150 | return y |
paul@6 | 151 | |
paul@6 | 152 | def _min(x, y): |
paul@6 | 153 | |
paul@6 | 154 | "Return the minimum of 'x' and 'y'." |
paul@6 | 155 | |
paul@6 | 156 | if x <= y: |
paul@6 | 157 | return x |
paul@6 | 158 | else: |
paul@6 | 159 | return y |
paul@6 | 160 | |
paul@6 | 161 | def _tuple(l): pass |
paul@6 | 162 | |
paul@6 | 163 | # vim: tabstop=4 expandtab shiftwidth=4 |