-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
55 lines (46 loc) · 1.68 KB
/
database.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
from collections import defaultdict, deque
import pickle
from limit_order_book import LimitOrderBook
import constants as c
from typing import Dict
class User:
def __init__(self, uid: int, balance: int):
self.uid: int = uid
self.balance: int = balance
self.ticker_to_amount: Dict[str, int] = {}
self.filled_oids = deque()
def __eq__(self, other):
return self.uid == other.uid
# simply use this class like it is a dictionary, it will store data automatically
class Database():
def __init__(self, filename = './db.pkl') -> None:
self.db = None
self.filename = filename
self.load_data()
def turn_bytes_into_db(self, b: bytes):
self.db = pickle.loads(b)
def store_data(self):
"""
It opens db.pkl file in write binary mode, and then dumps our db to the file.
"""
with open(self.filename, 'wb') as dbfile:
pickle.dump(self.db, dbfile)
def load_data(self):
"""
Load the dictionary from the db.pkl file if it exists, otherwise create it.
Return a dictionary with two keys, "passwords" and "messages".
"""
try:
with open(self.filename, 'rb') as dbfile:
self.db = pickle.load(dbfile)
except:
self.db = {
"orderbooks" : defaultdict(LimitOrderBook),
"client_balance": {client: 0 for client in c.BROKER_KEYS},
"oid_count": 0,
"oid_to_ticker": {},
"uid_to_user_dict": {uid: User(uid, balance=0) for uid in c.BROKER_KEYS},
}
return self.db
def get_db(self):
return self.db