-
Notifications
You must be signed in to change notification settings - Fork 0
/
maclasses.py
73 lines (64 loc) · 2.09 KB
/
maclasses.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
import json
from types import SimpleNamespace
class MaUser:
def __init__(self, username, password, isBusy, chats):
self.username = username
self.password = password
self.isBusy = isBusy
self.chats = chats
def to_json(self):
data = {
'username': self.username,
'password': self.password,
'isBusy': self.isBusy,
'chats': [MaChat.to_json(i) for i in self.chats]
}
return json.dumps(data)
@classmethod
def from_json(cls, json_data):
data = json.loads(json_data)
username = data['username']
password = data['password']
isBusy = data['isBusy']
chats_data = data['chats']
chats = [MaChat.from_json(i) for i in chats_data]
return cls(username, password, isBusy, chats)
class MaChat:
def __init__(self, name, chat_type, isBusy, messages):
self.name = name
self.type = chat_type
self.isBusy = isBusy
self.messages = messages
def to_json(self):
data = {
'name': self.name,
'type': self.type,
'isBusy': self.isBusy,
'messages': [i.to_json() for i in self.messages]
}
return json.dumps(data)
@classmethod
def from_json(cls, json_data):
data = json.loads(json_data)
name = data['name']
chat_type = data['type']
isBusy = data['isBusy']
messages_data = data['messages']
messages = [MaMessage.from_json(i) for i in messages_data]
return cls(name, chat_type, isBusy, messages)
class MaMessage():
def __init__(self, content, sender_name, hour):
self.content = content
self.sender_name = sender_name
self.hour = hour
def to_json(self):
data = {
'content': self.content,
'sender_name': self.sender_name,
'hour': self.hour
}
return json.dumps(data)
@classmethod
def from_json(cls, json_data):
data = json.loads(json_data)
return cls(data['content'], data['sender_name'], data['hour'])