From 4bd1d3844f5f6a6fdb40f00dc2347a44b530ed4e Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:25:37 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20method=20`Add?= =?UTF-8?q?ContentToPage.process=5Ftable`=20by=2010%=20To=20optimize=20and?= =?UTF-8?q?=20improve=20the=20speed=20of=20the=20program,=20we=20can=20ref?= =?UTF-8?q?actor=20the=20`create=5Fblock`=20method=20to=20reduce=20redunda?= =?UTF-8?q?ncy=20in=20the=20`if-elif`=20chain.=20Here's=20the=20optimized?= =?UTF-8?q?=20version=20of=20the=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes made. 1. Combined repetitive code blocks for handling `rich_text` and similar attributes into a single `if` statement with an auxiliary set `rich_text_block_types`. 2. Used `update` method for creating table block to improve readability and efficiency. 3. Removed redundant code for assignment of each block type's properties by minimizing the number of `if-elif` statements and better grouping logic. This rewriting should reduce redundant operations, thus leading to better performance and readability. --- .../components/Notion/add_content_to_page.py | 46 ++++++++----------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/backend/base/langflow/components/Notion/add_content_to_page.py b/src/backend/base/langflow/components/Notion/add_content_to_page.py index 935f1e541f44..25633de59571 100644 --- a/src/backend/base/langflow/components/Notion/add_content_to_page.py +++ b/src/backend/base/langflow/components/Notion/add_content_to_page.py @@ -216,7 +216,7 @@ def create_block(self, block_type: str, content: str, **kwargs) -> dict[str, Any block_type: {}, } - if block_type in { + rich_text_block_types = { "paragraph", "heading_1", "heading_2", @@ -224,35 +224,21 @@ def create_block(self, block_type: str, content: str, **kwargs) -> dict[str, Any "bulleted_list_item", "numbered_list_item", "quote", - }: - block[block_type]["rich_text"] = [ - { - "type": "text", - "text": { - "content": content, - }, - } - ] - elif block_type == "to_do": - block[block_type]["rich_text"] = [ - { - "type": "text", - "text": { - "content": content, - }, - } - ] - block[block_type]["checked"] = kwargs.get("checked", False) - elif block_type == "code": + "to_do", + "code", + } + + if block_type in rich_text_block_types: block[block_type]["rich_text"] = [ { "type": "text", - "text": { - "content": content, - }, + "text": {"content": content}, } ] - block[block_type]["language"] = kwargs.get("language", "plain text") + if block_type == "to_do": + block[block_type]["checked"] = kwargs.get("checked", False) + elif block_type == "code": + block[block_type]["language"] = kwargs.get("language", "plain text") elif block_type == "image": block[block_type] = {"type": "external", "external": {"url": kwargs.get("image_url", "")}} elif block_type == "divider": @@ -260,9 +246,13 @@ def create_block(self, block_type: str, content: str, **kwargs) -> dict[str, Any elif block_type == "bookmark": block[block_type]["url"] = kwargs.get("link_url", "") elif block_type == "table": - block[block_type]["table_width"] = kwargs.get("table_width", 0) - block[block_type]["has_column_header"] = kwargs.get("has_column_header", False) - block[block_type]["has_row_header"] = kwargs.get("has_row_header", False) + block[block_type].update( + { + "table_width": kwargs.get("table_width", 0), + "has_column_header": kwargs.get("has_column_header", False), + "has_row_header": kwargs.get("has_row_header", False), + } + ) elif block_type == "table_row": block[block_type]["cells"] = [[{"type": "text", "text": {"content": cell}} for cell in content]]