# HG changeset patch # User Paul Boddie # Date 1479912517 -3600 # Node ID a9df849fa0afe5945868c8c8608a8f6b284c783b # Parent 5bf57fa80a2f4505961ae4291a2eecc998da3e9d Put sequence functionality into a common base class for list and tuple. diff -r 5bf57fa80a2f -r a9df849fa0af lib/__builtins__/list.py --- a/lib/__builtins__/list.py Wed Nov 23 15:47:45 2016 +0100 +++ b/lib/__builtins__/list.py Wed Nov 23 15:48:37 2016 +0100 @@ -20,10 +20,10 @@ """ from __builtins__.iterator import listiterator -from __builtins__.sequence import _getitem, _getslice +from __builtins__.sequence import sequence import native -class list(object): +class list(sequence): "Implementation of list." @@ -39,22 +39,9 @@ if args is not None: self.extend(args) - def __getitem__(self, index): - - "Return the item or slice specified by 'index'." - - return _getitem(self, index) - def __contains__(self, value): pass def __setitem__(self, index, value): pass def __delitem__(self, index): pass - - def __getslice__(self, start, end=None): - - "Return a slice starting from 'start', with the optional 'end'." - - return _getslice(self, start, end) - def __setslice__(self, start, end, slice): pass def __delslice__(self, start, end): pass diff -r 5bf57fa80a2f -r a9df849fa0af lib/__builtins__/sequence.py --- a/lib/__builtins__/sequence.py Wed Nov 23 15:47:45 2016 +0100 +++ b/lib/__builtins__/sequence.py Wed Nov 23 15:48:37 2016 +0100 @@ -3,7 +3,7 @@ """ Sequence operations. -Copyright (C) 2015 Paul Boddie +Copyright (C) 2015, 2016 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -21,39 +21,60 @@ from native import _isinstance -def _getitem(seq, index): +class sequence: + + "A common base class for sequence types." - "Return the item or slice specified by 'index'." + def __getitem__(self, index): + + "Return the item or slice specified by 'index'." + + # Normalise any integer indexes, converting negative indexes to positive + # ones. - if _isinstance(index, int): - index = _normalise_index(index, seq.__len__()) - return seq.__get_single_item__(index) - elif _isinstance(index, slice): - return seq.__getslice__(index.start, index.end) - else: - raise TypeError + if _isinstance(index, int): + index = _normalise_index(index, self.__len__()) + return self.__get_single_item__(index) + + # Handle slices separately. -def _getslice(seq, start, end=None): + elif _isinstance(index, slice): + return self.__getslice__(index.start, index.end) + + # No other kinds of objects are supported as indexes. - "Return a slice starting from 'start', with the optional 'end'." + else: + raise TypeError + + def __getslice__(self, start, end=None): - length = seq.__len__() + "Return a slice starting from 'start', with the optional 'end'." + + length = self.__len__() - if start is None: - start = 0 - else: - start = _normalise_index(start, length) + # Handle a null start as the first position, otherwise normalising any + # start index. + + if start is None: + start = 0 + else: + start = _normalise_index(start, length) - if end is None: - end = length - else: - end = _normalise_index(end, length) + # Handle a null end as the first position after the end of the sequence, + # otherwise normalising any end index. + + if end is None: + end = length + else: + end = _normalise_index(end, length) - result = [] - while start < end: - result.append(seq.__get_single_item__(start)) - start += 1 - return result + result = [] + + while start < end: + result.append(self.__get_single_item__(start)) + start += 1 + + return result def _get_absolute_index(index, length): diff -r 5bf57fa80a2f -r a9df849fa0af lib/__builtins__/tuple.py --- a/lib/__builtins__/tuple.py Wed Nov 23 15:47:45 2016 +0100 +++ b/lib/__builtins__/tuple.py Wed Nov 23 15:48:37 2016 +0100 @@ -20,10 +20,10 @@ """ from __builtins__.iterator import listiterator -from __builtins__.sequence import _getitem, _getslice +from __builtins__.sequence import sequence import native -class tuple(object): +class tuple(sequence): "Implementation of tuple." @@ -33,17 +33,11 @@ self.__data__ = native._tuple_init(args, len(args)) # allocate and copy elements - def __getitem__(self, index): - - "Return the item or slice specified by 'index'." - - return _getitem(self, index) - def __getslice__(self, start, end=None): "Return a slice starting from 'start', with the optional 'end'." - return native._list_to_tuple(_getslice(self, start, end)) + return native._list_to_tuple(get_using(sequence.__getslice__, self)(start, end)) def __len__(self): @@ -55,6 +49,8 @@ def __str__(self): pass + __repr__ = __str__ + def __bool__(self): "Tuples are true if non-empty."