Skip to content

Commit

Permalink
correction initialize args and comprehention constructions (#527)
Browse files Browse the repository at this point in the history
1. create generator comprehension, lazy object
2. remove dangerous pass mut args in func initialize, removed overwriting args
3. create list comprehension, high performance
  • Loading branch information
ArtemIsmagilov authored Jul 26, 2024
1 parent cdcedb3 commit aab2de4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
45 changes: 22 additions & 23 deletions mmpy_bot/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_post(
message: str,
file_paths: Optional[Sequence[str]] = None,
root_id: str = "",
props: Dict = {},
props: Optional[Dict] = None,
ephemeral_user_id: Optional[str] = None,
):
"""Create a post in the specified channel with the specified text.
Expand All @@ -52,8 +52,8 @@ def create_post(
paths are specified, those files will be uploaded to mattermost first and then
attached.
"""
if file_paths is None:
file_paths = []
file_paths = file_paths or []
props = props or {}

file_ids = (
self.upload_files(file_paths, channel_id) if len(file_paths) > 0 else []
Expand Down Expand Up @@ -88,9 +88,9 @@ def get_post_thread(self, post_id: str):
duplicate and wrongly ordered entries in the ordered list."""
thread_info = self.posts.get_post_thread(post_id)

id_stamps = []
for id, post in thread_info["posts"].items():
id_stamps.append((id, int(post["create_at"])))
id_stamps = (
(id, int(post["create_at"])) for id, post in thread_info["posts"].items()
)
# Sort the posts by their timestamps
sorted_stamps = sorted(id_stamps, key=lambda x: x[-1])
# Overwrite the order with the sorted list
Expand All @@ -116,7 +116,7 @@ def reply_to(
message: Message,
response: str,
file_paths: Optional[Sequence[str]] = None,
props: Dict = {},
props: Optional[Dict] = None,
ephemeral: bool = False,
direct: bool = False,
):
Expand All @@ -127,8 +127,8 @@ def reply_to(
Also supports replying privately by setting direct=True.
"""
if file_paths is None:
file_paths = []
file_paths = file_paths or []
props = props or {}

if direct and not message.is_direct_message:
# NOTE we explicitly don't pass root_id as it would refer to a
Expand Down Expand Up @@ -160,14 +160,15 @@ def direct_message(
message: str,
file_paths: Optional[Sequence[str]] = None,
root_id: str = "",
props: Dict = {},
props: Optional[Dict] = None,
ephemeral_user_id: Optional[str] = None,
):
# Private/direct messages are sent to a special channel that
# includes the bot and the recipient
direct_id = self.channels.create_direct_channel([self.user_id, receiver_id])[
"id"
]
props = props or {}

return self.create_post(
channel_id=direct_id,
Expand Down Expand Up @@ -199,22 +200,20 @@ def upload_files(
) -> List[str]:
"""Given a list of file paths and the channel id, uploads the corresponding
files and returns a list their internal file IDs."""
file_list = []
for path in file_paths:
path = Path(path)
# Note: 'files' should be a name of an expected attribute in the body
# but seems to be ignored when simply uploading files to mattermost
file_list.append(
# Note: 'files' should be a name of an expected attribute in the body
# but seems to be ignored when simply uploading files to mattermost
file_list = [
(
"files",
(
"files",
(
path.name,
Path(path).read_bytes(),
),
)
Path(path).name,
Path(path).read_bytes(),
),
)
for path in file_paths
]

result = self.files.upload_file(
files=file_list, data={"channel_id": channel_id}
)
return list(info["id"] for info in result["file_infos"])
return [info["id"] for info in result["file_infos"]]
2 changes: 1 addition & 1 deletion tests/integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def expect_reply(driver: Driver, post: Dict, wait=RESPONSE_TIMEOUT, retries=1):
reply = thread_info["posts"][reply_id]
break

if not reply:
if reply is None:
raise ValueError("Expected a response, but didn't get any!")

return reply
Expand Down

0 comments on commit aab2de4

Please sign in to comment.