Skip to content

Commit b01d6e5

Browse files
committed
️ref: channel file.
* add documentation to all channel file. * add typing to all channel file. * rewrite send method. * change parameter skipusers to a Set to reduce the temporal complexity * add private method log message to increase readability and shorten the method (reduce the visual complexity)
1 parent 4072a12 commit b01d6e5

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

aioircd/channel.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import trio
33
from functools import partial
4-
from typing import Union, List
4+
from typing import Union, List, Set
55

66
import aioircd
77

@@ -10,28 +10,70 @@
1010

1111

1212
class Channel:
13-
def __init__(self, name):
13+
"""
14+
The Channel class represents a chat room.
15+
16+
Attributes:
17+
name: str
18+
the channel name.
19+
users: Set[User]
20+
a set of user who is subscribe to the channel.
21+
"""
22+
23+
def __init__(self, name: str) -> None:
24+
"""
25+
Parameters:
26+
name: str
27+
the channel name.
28+
"""
1429
self._name = name
1530
self.users = set()
1631

17-
def __str__(self):
32+
def __str__(self) -> str:
33+
"""
34+
We represents the channel object by his name.
35+
36+
Return: str
37+
the name of the channel.
38+
"""
1839
return self._name
1940

2041
@property
2142
def name(self):
43+
"""
44+
Getter for private attributes channel name.
45+
46+
Returns: str
47+
the name of the channel.
48+
"""
2249
return self._name
2350

24-
async def send(self, messages: Union[str, List[str]], skipusers=[]):
25-
""" Send many messages to each user subcribed in this channel. """
51+
def _log_messages(self, messages: List[str]) -> None:
52+
"""
53+
Log all messages sent to a client.
54+
55+
Parameters: List[str]
56+
A list of messages send to the client.
57+
"""
58+
for message in messages:
59+
logger.log(aioircd.IO, "send to %s: %s", self, message)
60+
61+
async def send(self, messages: Union[str, List[str]], skipusers: Set["aioircd.user.User"] = set()) -> None:
62+
""""
63+
Send many messages to each user subscribed to this channel.
64+
65+
Parameters:
66+
messages: Union[str, List[str]]
67+
Messages to send to the users.
68+
skipusers: Set[User]
69+
A set of users to skip sending messages to.
70+
"""
2671
if isinstance(messages, str):
2772
messages = [messages]
2873

2974
async with trio.open_nursery() as self._nursery:
30-
for message in messages:
31-
logger.log(aioircd.IO, "send to %s: %s", self, message)
32-
for user in self.users:
33-
if user in skipusers:
34-
continue
75+
self._log_messages(messages)
76+
for user in self.users.difference(skipusers):
3577
self._nursery.start_soon(partial(
3678
user.send,
3779
messages,

0 commit comments

Comments
 (0)