-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcircularlist.py
executable file
·58 lines (47 loc) · 1.19 KB
/
circularlist.py
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
import unittest
class CircularList(list):
'''
A list that wraps around instead of throwing an index error.
Works like a regular list:
>>> cl = CircularList([1,2,3])
>>> cl
[1, 2, 3]
>>> cl[0]
1
>>> cl[-1]
3
>>> cl[2]
3
Except wraps around:
>>> cl[3]
1
>>> cl[-4]
3
Slices work
>>> cl[0:2]
[1, 2]
but only in range.
'''
def __getitem__(self, key):
# try normal list behavior
try:
return super(CircularList, self).__getitem__(key)
except IndexError:
pass
# key can be either integer or slice object,
# only implementing int now.
try:
index = int(key)
index = index % self.__len__()
return super(CircularList, self).__getitem__(index)
except ValueError:
raise TypeError
class TestCircularList(unittest.TestCase):
def test_cicular_list_wrap_forward(self):
cl = CircularList([1,2,3])
#make sure it wraps properly
self.assertEqual(cl[3], 1)
def test_cicular_list_wrap_backward(self):
cl = CircularList([1,2,3])
#make sure it wraps properly
self.assertEqual(cl[-1], 3)