-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexdict.py
27 lines (23 loc) · 1012 Bytes
/
indexdict.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
# ItemDict that store objects of same type
class IndexDict(dict):
def __init__(self):
super().__init__()
self._type = int
def append(self, key, value):
# Check item type
if isinstance(value, self._type):
if key in self:
# Ensure the existing value is a list, if not convert it to a list
if not isinstance(super().__getitem__(key), list):
# Convert existing value to a list
super().__setitem__(key, [super().__getitem__(key)])
# Append the new value to the list
super().__getitem__(key).append(value)
else:
# Set the key-value pair
super().__setitem__(key, value)
else:
raise ValueError(f"Item type mismatch: expected\
{self._type}, but got {type(value)}")
#def __setitem__(self, key, value):
# raise NotImplementedError("Use append instead of direct assignment!")