From 3ab8709e32c932797ee8b4ce14819de653df2a24 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Mon, 15 Jul 2024 22:18:25 +0400 Subject: [PATCH 1/4] 1. create generator comprehention, lazy object 2. remove dangerous pass mut args in func initialize, removed overwriting args 3. create list comprehention, high perfomance --- mmpy_bot/driver.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/mmpy_bot/driver.py b/mmpy_bot/driver.py index cb78bc17..dca4dc28 100644 --- a/mmpy_bot/driver.py +++ b/mmpy_bot/driver.py @@ -43,7 +43,7 @@ def create_post( message: str, file_paths: Optional[Sequence[str]] = None, root_id: str = "", - props: Dict = {}, + props: Dict = None, ephemeral_user_id: Optional[str] = None, ): """Create a post in the specified channel with the specified text. @@ -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 [] @@ -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 @@ -116,7 +116,7 @@ def reply_to( message: Message, response: str, file_paths: Optional[Sequence[str]] = None, - props: Dict = {}, + props: Dict = None, ephemeral: bool = False, direct: bool = False, ): @@ -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 @@ -160,7 +160,7 @@ def direct_message( message: str, file_paths: Optional[Sequence[str]] = None, root_id: str = "", - props: Dict = {}, + props: Dict = None, ephemeral_user_id: Optional[str] = None, ): # Private/direct messages are sent to a special channel that @@ -168,6 +168,7 @@ def direct_message( direct_id = self.channels.create_direct_channel([self.user_id, receiver_id])[ "id" ] + props = props or {} return self.create_post( channel_id=direct_id, @@ -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"]] From a37903a11ce313401a6bd30cc3c30344664bb1df Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Fri, 19 Jul 2024 22:59:24 +0400 Subject: [PATCH 2/4] correction static typing None | dict --- mmpy_bot/driver.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mmpy_bot/driver.py b/mmpy_bot/driver.py index dca4dc28..fa5f7eec 100644 --- a/mmpy_bot/driver.py +++ b/mmpy_bot/driver.py @@ -43,7 +43,7 @@ def create_post( message: str, file_paths: Optional[Sequence[str]] = None, root_id: str = "", - props: Dict = None, + props: Optional[Dict] = None, ephemeral_user_id: Optional[str] = None, ): """Create a post in the specified channel with the specified text. @@ -116,7 +116,7 @@ def reply_to( message: Message, response: str, file_paths: Optional[Sequence[str]] = None, - props: Dict = None, + props: Optional[Dict] = None, ephemeral: bool = False, direct: bool = False, ): @@ -160,7 +160,7 @@ def direct_message( message: str, file_paths: Optional[Sequence[str]] = None, root_id: str = "", - props: Dict = None, + props: Optional[Dict] = None, ephemeral_user_id: Optional[str] = None, ): # Private/direct messages are sent to a special channel that From b35d3b2bada7c40f23c9c139371418d291f7cd16 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Fri, 19 Jul 2024 23:07:42 +0400 Subject: [PATCH 3/4] assert reply on `not None` --- tests/integration_tests/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/utils.py b/tests/integration_tests/utils.py index 589bce96..37f9d89f 100644 --- a/tests/integration_tests/utils.py +++ b/tests/integration_tests/utils.py @@ -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 not None: raise ValueError("Expected a response, but didn't get any!") return reply From 78b613e6a2585c616f856d00702cd367f44540ff Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Fri, 19 Jul 2024 23:11:36 +0400 Subject: [PATCH 4/4] small fix check tests on `is None` --- tests/integration_tests/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/utils.py b/tests/integration_tests/utils.py index 37f9d89f..fe8a589c 100644 --- a/tests/integration_tests/utils.py +++ b/tests/integration_tests/utils.py @@ -34,7 +34,7 @@ def expect_reply(driver: Driver, post: Dict, wait=RESPONSE_TIMEOUT, retries=1): reply = thread_info["posts"][reply_id] break - if reply is not None: + if reply is None: raise ValueError("Expected a response, but didn't get any!") return reply