-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathslice_utils.py
More file actions
68 lines (59 loc) · 2.12 KB
/
Copy pathslice_utils.py
File metadata and controls
68 lines (59 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Custom slice implementation for PythonBox interpreter.
"""
class CustomSlice:
"""
A custom implementation of Python's slice object that formats its string representation
as 'Slice(start,stop,step)' without spaces after commas and with capital 'S'.
"""
def __init__(self, start, stop, step):
self.start = start
self.stop = stop
self.step = step
def __str__(self):
"""
Return string representation of the slice with custom formatting.
"""
return f"Slice({self.start},{self.stop},{self.step})"
def __repr__(self):
"""
Return formal representation of the slice with custom formatting.
"""
return self.__str__()
def __eq__(self, other):
"""
Compare this slice with another slice or CustomSlice.
"""
if isinstance(other, CustomSlice):
return (self.start == other.start and
self.stop == other.stop and
self.step == other.step)
elif isinstance(other, slice):
return (self.start == other.start and
self.stop == other.stop and
self.step == other.step)
return False
def indices(self, length):
"""
Return a tuple (start, stop, step) representing the slice adjusted for a sequence of the specified length.
"""
# This mimics the behavior of slice.indices()
start, stop, step = self.start, self.stop, self.step
# Handle None values
if start is None:
start = 0 if step > 0 else length - 1
elif start < 0:
start += length
if start < 0:
start = 0 if step > 0 else -1
elif start >= length:
start = length if step > 0 else length - 1
if stop is None:
stop = length if step > 0 else -1
elif stop < 0:
stop += length
if stop < 0:
stop = 0 if step > 0 else -1
elif stop >= length:
stop = length
return (start, stop, step)