# HG changeset patch # User Paul Boddie # Date 1481387758 -3600 # Node ID 853edfa770e31cfb5880c0f3e3d7597719bab967 # Parent 6d63ae9b8b7afcb265d0ed40aee620f664918112 Reorganised slice and xrange so that slices do not act as ranges, permitting the start and end of a slice to be None. Support reverse sequences in the __getslice__ sequence method. diff -r 6d63ae9b8b7a -r 853edfa770e3 lib/__builtins__/sequence.py --- a/lib/__builtins__/sequence.py Sat Dec 10 17:32:16 2016 +0100 +++ b/lib/__builtins__/sequence.py Sat Dec 10 17:35:58 2016 +0100 @@ -94,7 +94,10 @@ # start index. if start is None: - start = 0 + if step > 0: + start = 0 + else: + start = length - 1 else: start = _get_absolute_index(start, length) @@ -102,7 +105,10 @@ # otherwise normalising any end index. if end is None: - end = length + if step > 0: + end = length + else: + end = -1 else: end = _get_absolute_index(end, length) diff -r 6d63ae9b8b7a -r 853edfa770e3 lib/__builtins__/span.py --- a/lib/__builtins__/span.py Sat Dec 10 17:32:16 2016 +0100 +++ b/lib/__builtins__/span.py Sat Dec 10 17:35:58 2016 +0100 @@ -21,15 +21,15 @@ from __builtins__.sequence import _max, _min -class xrange: +class slice: - "Implementation of xrange." + "Implementation of slice." NO_END = object() - def __init__(self, start_or_end, end=NO_END, step=1): + def __init__(self, start_or_end=None, end=NO_END, step=1): - "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." + "Initialise the slice with the given 'start_or_end', 'end' and 'step'." if end is xrange.NO_END: self.start = 0 @@ -38,8 +38,30 @@ self.start = start_or_end self.end = end + if step == 0: + raise ValueError(self.step) + self.step = step + def __str__(self): + + "Return a string representation." + + b = buffer([self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"]) + return str(b) + + __repr__ = __str__ + +class xrange(slice): + + "Implementation of xrange." + + def __init__(self, start_or_end, end=slice.NO_END, step=1): + + "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." + + get_using(slice.__init__, self)(start_or_end, end, step) + # Constrain the end according to the start and step. if step > 0: @@ -51,15 +73,6 @@ self.current = self.start - def __str__(self): - - "Return a string representation." - - b = buffer([self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"]) - return str(b) - - __repr__ = __str__ - def __len__(self): "Return the length of the range." @@ -83,16 +96,6 @@ self.current += self.step return current -class slice(xrange): - - "Implementation of slice." - - def __init__(self, start_or_end=None, end=xrange.NO_END, step=1): - - "Initialise the slice with the given 'start_or_end', 'end' and 'step'." - - get_using(xrange.__init__, self)(start_or_end, end, step) - def range(start_or_end, end=None, step=1): "Implementation of range." diff -r 6d63ae9b8b7a -r 853edfa770e3 tests/slice.py --- a/tests/slice.py Sat Dec 10 17:32:16 2016 +0100 +++ b/tests/slice.py Sat Dec 10 17:35:58 2016 +0100 @@ -10,3 +10,4 @@ print l[5:2:-1] # [5, 4, 3] print l[1:9:2] # [1, 3, 5, 7] print l[9:1:-2] # [9, 7, 5, 3] +print l[::-1] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]