-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathCH_03_03.py
More file actions
51 lines (39 loc) · 1.68 KB
/
CH_03_03.py
File metadata and controls
51 lines (39 loc) · 1.68 KB
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
import json
import asyncio
import click
import aioredis
class Chat:
def __init__(self, room_name):
self.room_name = room_name
async def start_db(self):
self.redis = await aioredis.create_redis_pool("redis://localhost")
await self.redis.set("room_name", self.room_name)
async def save_message(self, message_dictionary):
room_name = await self.redis.get("room_name")
message_json = json.dumps(message_dictionary)
await self.redis.rpush(room_name, message_json)
async def clear_db(self):
await self.redis.flushall()
async def get_all_messages(self):
room_name = await self.redis.get("room_name")
message_jsons = await self.redis.lrange(room_name, 0, -1, encoding="utf-8")
messages = []
for message in message_jsons:
message_dictionary = json.loads(message)
messages.append(message_dictionary)
return messages
async def get_name(self):
return # your code here make sure you use encoding="utf-8"
async def main():
chat_db = Chat("messages")
await chat_db.start_db()
await chat_db.save_message({"handle": "first_user", "message": "hey"})
await chat_db.save_message({"handle": "first_user", "message": "hey"})
await chat_db.save_message({"handle": "second_user", "message": "What's up?"})
await chat_db.save_message({"handle": "first_user", "message": "all good!"})
chat_messages = await chat_db.get_all_messages()
click.secho(f" Chat ", fg="cyan", bold=True, bg="yellow")
for message in chat_messages:
click.secho(f' {message["handle"]} | {message["message"]} ', fg="cyan")
await chat_db.clear_db()
asyncio.run(main())