forked from amina196/OrderedDictionary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordereddict.py
More file actions
129 lines (104 loc) · 3.72 KB
/
ordereddict.py
File metadata and controls
129 lines (104 loc) · 3.72 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
""" Drop-in replacement for the Python 2.7 OrderedDict class. """
class OrderedDict:
def __init__(self, d={}, **data):
self._keys = []
self._values = []
if(type(d).__name__ not in ["dict", "org.python.core.PyDictionary"]): # Fix for Jython types
raise TypeError("the dictionary entered is not valid - either regular dictionary or OrderedDictionary object is expected")
for key in d.keys():
self[key] = d[key] # In the ord-dict add an entry at the key 'key' and give the value at 'key' in d
if data:
for key in data:
self[key] = data[key]
# ---------------------
# Overwrite `__getitem__`, `__setitem__`, `__delitem__` so that
# `dict[key] = value` correctly sets `self._keys` and `self._values`.
def __getitem__(self, key):
if key not in self._keys:
raise KeyError("The key %s isn't in the dictionary" % key)
index = self._keys.index(key)
return self._values[index]
def __setitem__(self, key, value):
if key in self._keys:
ind = self._keys.index(key)
self._values[ind] = value
else:
self._keys.append(key)
self._values.append(value)
def __delitem__(self, key):
if key not in self._keys:
raise KeyError("The key %s isn't in the dictionary" % key)
else:
ind = self._keys.index(key)
del self._keys[ind]
del self._values[ind]
# ---------------------
"""
Functions `repr` and `str` return the same results:
>>> d = collections.OrderedDict({"test":True})
>>> repr(d)
"OrderedDict([('test', True)])"
>>> str(d)
"OrderedDict([('test', True)])"
"""
def __repr__(self):
string = "OrderedDict({"
first_time = 1 == 1 # Fix for Python < 2.3
for key, value in self.items():
if not first_time:
string += ", " # Add comma to separate entries
else:
first_time = 1 == 0
string += repr(key) + ": " + repr(value)
string += "})"
return string
def __str__(self):
return repr(self)
# ---------------------
def __len__(self):
return len(self._keys)
def __contains__(self, key):
return key in self._keys
def __eq__(self, other):
true = 1 == 1 # Fix for Python v. < 2.3
false = 1 == 0 # Fix for Python v. < 2.3
# if isinstance(other, OrderedDict):
# print("Comparing the same object...")
# else:
# print("Comparing two different objects...")
if len(self) != len(other):
return false
for p, q in zip(self.items(), other.items()):
if p != q:
return false
return true
def __ne__(self, other):
return not self == other
def __iter__(self):
return iter(self._keys)
# ---> Methods
def items(self):
items = []
for i in range(len(self._keys)):
key = self._keys[i]
val = self._values[i]
items.append((key, val))
return items
def keys(self):
return list(self._keys)
def values(self):
return list(self._values)
def clear(self):
self._keys = []
self._values = {}
return None
def get(self, key, default):
if key not in self._keys:
raise KeyError("The key %s isn't in the dictionary" % key)
index = self._keys.index(key)
return self._values[index]
def copy(self):
new_dict = OrderedDict()
for key,value in self.items():
new_dict[key] = value
return new_dict