forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
44 lines (32 loc) · 1.09 KB
/
utils.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
import time
from typing import Any
class MockTimer:
def __init__(self, start_time=None):
if start_time is None:
start_time = time.time()
self._curr = start_time
def time(self):
return self._curr
def advance(self, by):
self._curr += by
def realistic_sleep(self, amt):
self._curr += amt + 0.001
class MockKVStore:
def __init__(self):
self.store = dict()
def put(self, key: str, val: Any) -> bool:
if not isinstance(key, str):
raise TypeError("key must be a string, got: {}.".format(type(key)))
self.store[key] = val
return True
def get(self, key: str) -> Any:
if not isinstance(key, str):
raise TypeError("key must be a string, got: {}.".format(type(key)))
return self.store.get(key, None)
def delete(self, key: str) -> bool:
if not isinstance(key, str):
raise TypeError("key must be a string, got: {}.".format(type(key)))
if key in self.store:
del self.store[key]
return True
return False