1 #!/usr/bin/env python 2 3 """ 4 Sequence operations. 5 6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from native import _isinstance 23 24 class sequence: 25 26 "A common base class for sequence types." 27 28 def _str(self, opening, closing): 29 30 "Serialise this object with the given 'opening' and 'closing' strings." 31 32 b = buffer() 33 i = 0 34 l = self.__len__() 35 first = True 36 37 b.append(opening) 38 while i < l: 39 if first: 40 first = False 41 else: 42 b.append(", ") 43 b.append(repr(self.__get_single_item__(i))) 44 i += 1 45 b.append(closing) 46 47 return str(b) 48 49 def __contains__(self, value): 50 51 "Return whether the list contains 'value'." 52 53 # Perform a linear search of the sequence contents. 54 55 for v in self: 56 57 # Return True if the current value is equal to the specified one. 58 # Note that this is not an identity test, but an equality test. 59 60 if v == value: 61 return True 62 63 return False 64 65 def __getitem__(self, index): 66 67 "Return the item or slice specified by 'index'." 68 69 # Normalise any integer indexes, converting negative indexes to positive 70 # ones. 71 72 if _isinstance(index, int): 73 index = _get_absolute_index(index, self.__len__()) 74 return self.__get_single_item__(index) 75 76 # Handle slices separately. 77 78 elif _isinstance(index, slice): 79 return self.__getslice__(index.start, index.end) 80 81 # No other kinds of objects are supported as indexes. 82 83 else: 84 raise TypeError 85 86 def __setitem__(self, index, value): 87 88 "Set at 'index' the given 'value'." 89 90 # Normalise any integer indexes, converting negative indexes to positive 91 # ones. 92 93 if _isinstance(index, int): 94 index = _get_absolute_index(index, self.__len__()) 95 return self.__set_single_item__(index, value) 96 97 # Handle slices separately. 98 99 elif _isinstance(index, slice): 100 return self.__setslice__(index.start, index.end, value) 101 102 # No other kinds of objects are supported as indexes. 103 104 else: 105 raise TypeError 106 107 def __getslice__(self, start, end=None): 108 109 "Return a slice starting from 'start', with the optional 'end'." 110 111 length = self.__len__() 112 113 # Handle a null start as the first position, otherwise normalising any 114 # start index. 115 116 if start is None: 117 start = 0 118 else: 119 start = _get_absolute_index(start, length) 120 121 # Handle a null end as the first position after the end of the sequence, 122 # otherwise normalising any end index. 123 124 if end is None: 125 end = length 126 else: 127 end = _get_absolute_index(end, length) 128 129 result = [] 130 131 while start < end: 132 result.append(self.__get_single_item__(start)) 133 start += 1 134 135 return result 136 137 def _check_index(self, index): 138 139 """ 140 Check the given absolute 'index', raising an IndexError if out of 141 bounds. 142 """ 143 144 if index < 0 or index >= len(self): 145 raise IndexError(index) 146 147 def _get_absolute_index(index, length): 148 149 """ 150 Return the absolute index for 'index' given a collection having the 151 specified 'length'. 152 """ 153 154 if index < 0: 155 return length + index 156 else: 157 return index 158 159 def _max(x, y): 160 161 "Return the maximum of 'x' and 'y'." 162 163 if x >= y: 164 return x 165 else: 166 return y 167 168 def _min(x, y): 169 170 "Return the minimum of 'x' and 'y'." 171 172 if x <= y: 173 return x 174 else: 175 return y 176 177 def _tuple(l): pass 178 179 # vim: tabstop=4 expandtab shiftwidth=4