-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderedfrozenset.py
More file actions
84 lines (66 loc) · 1.93 KB
/
orderedfrozenset.py
File metadata and controls
84 lines (66 loc) · 1.93 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
There is nothing much to say about this module.
It wraps around the frozenset and keeps it ordered.
Besides, it includes some additional custom-made
methods which may or may not be useful.
"""
class OrderedFrozenSet(frozenset):
def __init__(self, elements=[]):
self = frozenset(elements)
def __getitem__(self, idx):
return sorted(super().__iter__()).__getitem__(idx)
def __len__(self):
return super().__len__()
def __iter__(self):
return sorted(super().__iter__()).__iter__()
def __contains__(self, item):
return super().__contains__(item)
def __eq__(self, other):
return super().__eq__(other)
def __repr__(self):
return super().__repr__()
def main():
"""Testing OrderedFrozenSet datatype"""
# Testing Frozen Dictionary creation
ofset = OrderedFrozenSet({1, 2, 3})
if ofset:
print("Test 0 passed")
else:
print("Test 0 failed")
# Testing magic method '__getitem__'
if ofset[1] == 2:
print("Test 1 passed")
else:
print("Test 1 failed")
# Testing magic method '__len__'
if len(ofset) == 3:
print("Test 2 passed")
else:
print("Test 2 failed")
# Testing magic method '__iter__'
lst = [1, 2, 3]
res = True
for i in range(len(ofset)):
if ofset[i] != lst[i]:
res = False
if res:
print("Test 3 passed")
else:
print("Test 3 failed")
# Testing magic method '__contains__'
if (2 in ofset) is True:
print("Test 4 passed")
else:
print("Test 4 failed")
# Testing magic method '__eq__'
if ofset == OrderedFrozenSet({1, 2, 3}):
print("Test 5 passed")
else:
print("Test 5 failed")
# Testing magic method '__repr__'
if str(ofset) == "OrderedFrozenSet({1, 2, 3})":
print("Test 6 passed")
else:
print("Test 6 failed")
if __name__ == "__main__":
main()