-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperation.py
43 lines (31 loc) · 1.12 KB
/
Operation.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
class Operation:
def __init__(self, operationType, **kwargs):
self.type = operationType
self.key = None
self.value = None
if 'key' in kwargs:
self.key = kwargs['key']
if 'value' in kwargs:
self.value = kwargs['value']
def __hash__(self):
return hash( (self.type, self.key, self.value) ) # Note: If self.value is None for type=="get", it should always be hashed to the same number
def __eq__(self, other):
if self.type == other.type:
if self.type == "get":
return self.key == other.key
elif self.type == "put":
return self.key == other.key and self.value == other.value
return False
@classmethod
def Put(cls, key, value):
return cls("put", key=key, value=value)
@classmethod
def Get(cls, key):
return cls("get", key=key)
def __repr__(self):
rep = f"Operation({repr(self.type)}"
for k, v in vars(self).items():
if k != "type":
rep += f", {k}={repr(v)}"
rep += ")"
return rep