/
Timestamp.py
80 lines (61 loc) · 2.13 KB
/
Timestamp.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Timestamp:
def __init__(self, total_agents):
self._total_agents = total_agents
self._timestamp = [0] * total_agents
def get_timestamp(self):
return self._timestamp
# Increase Timestamp
def increase_timestamp(self, agent_id):
self._timestamp[agent_id] += 1
# Reset all future timestamp valus relative to current agent_id
def reset_timestamp(self, agent_id):
for x in range(agent_id + 1, self._total_agents):
self._timestamp[x] = 0
# Define size of Timestamp
def __len__(self):
return len(self._timestamp)
# Overload the operator Timestamp[]
def __getitem__(self, item):
return self._timestamp[item]
# Check if this timestamp is smaller than incoming
def __lt__(self, incoming):
# Check timestamps are of equal size
if len(self) != len(incoming):
return False
for index in range(1, len(incoming)):
if incoming[index] == self._timestamp[index]:
continue
elif incoming[index] < self._timestamp[index]:
return False
elif incoming[index] > self._timestamp[index]:
return True
return True
def __lte__(self, incoming):
return True
# Check if all positions are equal
def __eq__(self, incoming):
if len(self) is not len(incoming):
return False
for x in range(len(self)):
if self[x] is not incoming[x]:
return False
return True
def __ne__(self, incoming):
return not(self == incoming)
# Check if this timestamp is greater than incoming
def __gt__(self, incoming):
# Check timestamps are of equal size
if len(incoming) != len(self):
return False
return not self < incoming
def __str__(self):
return str(self._timestamp)
# Unit Test
if __name__ == "__main__":
timestamp1 = Timestamp(5)
timestamp2 = Timestamp(5)
timestamp1.increase_timestamp(1)
if timestamp1 > timestamp2 or timestamp1 is timestamp2:
print('Greater')
else:
print('Smaller')